Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko.browserid.verifier; |
michael@0 | 6 | |
michael@0 | 7 | import java.io.IOException; |
michael@0 | 8 | import java.io.UnsupportedEncodingException; |
michael@0 | 9 | import java.net.URI; |
michael@0 | 10 | import java.net.URISyntaxException; |
michael@0 | 11 | import java.security.GeneralSecurityException; |
michael@0 | 12 | import java.util.Arrays; |
michael@0 | 13 | import java.util.List; |
michael@0 | 14 | |
michael@0 | 15 | import org.mozilla.gecko.background.common.log.Logger; |
michael@0 | 16 | import org.mozilla.gecko.browserid.verifier.BrowserIDVerifierException.BrowserIDVerifierErrorResponseException; |
michael@0 | 17 | import org.mozilla.gecko.browserid.verifier.BrowserIDVerifierException.BrowserIDVerifierMalformedResponseException; |
michael@0 | 18 | import org.mozilla.gecko.sync.ExtendedJSONObject; |
michael@0 | 19 | import org.mozilla.gecko.sync.net.BaseResource; |
michael@0 | 20 | import org.mozilla.gecko.sync.net.BaseResourceDelegate; |
michael@0 | 21 | import org.mozilla.gecko.sync.net.Resource; |
michael@0 | 22 | import org.mozilla.gecko.sync.net.SyncResponse; |
michael@0 | 23 | |
michael@0 | 24 | import ch.boye.httpclientandroidlib.HttpResponse; |
michael@0 | 25 | import ch.boye.httpclientandroidlib.NameValuePair; |
michael@0 | 26 | import ch.boye.httpclientandroidlib.client.ClientProtocolException; |
michael@0 | 27 | import ch.boye.httpclientandroidlib.client.entity.UrlEncodedFormEntity; |
michael@0 | 28 | import ch.boye.httpclientandroidlib.message.BasicNameValuePair; |
michael@0 | 29 | |
michael@0 | 30 | public class BrowserIDRemoteVerifierClient implements BrowserIDVerifierClient { |
michael@0 | 31 | protected static class RemoteVerifierResourceDelegate extends BaseResourceDelegate { |
michael@0 | 32 | private final BrowserIDVerifierDelegate delegate; |
michael@0 | 33 | |
michael@0 | 34 | protected RemoteVerifierResourceDelegate(Resource resource, BrowserIDVerifierDelegate delegate) { |
michael@0 | 35 | super(resource); |
michael@0 | 36 | this.delegate = delegate; |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | @Override |
michael@0 | 40 | public String getUserAgent() { |
michael@0 | 41 | return null; |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | @Override |
michael@0 | 45 | public void handleHttpResponse(HttpResponse response) { |
michael@0 | 46 | SyncResponse res = new SyncResponse(response); |
michael@0 | 47 | int statusCode = res.getStatusCode(); |
michael@0 | 48 | Logger.debug(LOG_TAG, "Got response with status code " + statusCode + "."); |
michael@0 | 49 | |
michael@0 | 50 | if (statusCode != 200) { |
michael@0 | 51 | delegate.handleError(new BrowserIDVerifierErrorResponseException("Expected status code 200.")); |
michael@0 | 52 | return; |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | ExtendedJSONObject o = null; |
michael@0 | 56 | try { |
michael@0 | 57 | o = res.jsonObjectBody(); |
michael@0 | 58 | } catch (Exception e) { |
michael@0 | 59 | delegate.handleError(new BrowserIDVerifierMalformedResponseException(e)); |
michael@0 | 60 | return; |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | String status = o.getString("status"); |
michael@0 | 64 | if ("failure".equals(status)) { |
michael@0 | 65 | delegate.handleFailure(o); |
michael@0 | 66 | return; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | if (!("okay".equals(status))) { |
michael@0 | 70 | delegate.handleError(new BrowserIDVerifierMalformedResponseException("Expected status okay, got '" + status + "'.")); |
michael@0 | 71 | return; |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | delegate.handleSuccess(o); |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | @Override |
michael@0 | 78 | public void handleTransportException(GeneralSecurityException e) { |
michael@0 | 79 | Logger.warn(LOG_TAG, "Got transport exception.", e); |
michael@0 | 80 | delegate.handleError(e); |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | @Override |
michael@0 | 84 | public void handleHttpProtocolException(ClientProtocolException e) { |
michael@0 | 85 | Logger.warn(LOG_TAG, "Got protocol exception.", e); |
michael@0 | 86 | delegate.handleError(e); |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | @Override |
michael@0 | 90 | public void handleHttpIOException(IOException e) { |
michael@0 | 91 | Logger.warn(LOG_TAG, "Got IO exception.", e); |
michael@0 | 92 | delegate.handleError(e); |
michael@0 | 93 | } |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | public static final String LOG_TAG = "BrowserIDRemoteVerifierClient"; |
michael@0 | 97 | |
michael@0 | 98 | public static final String DEFAULT_VERIFIER_URL = "https://verifier.login.persona.org/verify"; |
michael@0 | 99 | |
michael@0 | 100 | protected final URI verifierUri; |
michael@0 | 101 | |
michael@0 | 102 | public BrowserIDRemoteVerifierClient(URI verifierUri) { |
michael@0 | 103 | this.verifierUri = verifierUri; |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | public BrowserIDRemoteVerifierClient() throws URISyntaxException { |
michael@0 | 107 | this.verifierUri = new URI(DEFAULT_VERIFIER_URL); |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | @Override |
michael@0 | 111 | public void verify(String audience, String assertion, final BrowserIDVerifierDelegate delegate) { |
michael@0 | 112 | if (audience == null) { |
michael@0 | 113 | throw new IllegalArgumentException("audience cannot be null."); |
michael@0 | 114 | } |
michael@0 | 115 | if (assertion == null) { |
michael@0 | 116 | throw new IllegalArgumentException("assertion cannot be null."); |
michael@0 | 117 | } |
michael@0 | 118 | if (delegate == null) { |
michael@0 | 119 | throw new IllegalArgumentException("delegate cannot be null."); |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | BaseResource r = new BaseResource(verifierUri); |
michael@0 | 123 | |
michael@0 | 124 | r.delegate = new RemoteVerifierResourceDelegate(r, delegate); |
michael@0 | 125 | |
michael@0 | 126 | List<NameValuePair> nvps = Arrays.asList(new NameValuePair[] { |
michael@0 | 127 | new BasicNameValuePair("audience", audience), |
michael@0 | 128 | new BasicNameValuePair("assertion", assertion) }); |
michael@0 | 129 | |
michael@0 | 130 | try { |
michael@0 | 131 | r.post(new UrlEncodedFormEntity(nvps, "UTF-8")); |
michael@0 | 132 | } catch (UnsupportedEncodingException e) { |
michael@0 | 133 | delegate.handleError(e); |
michael@0 | 134 | } |
michael@0 | 135 | } |
michael@0 | 136 | } |