mobile/android/base/browserid/verifier/BrowserIDRemoteVerifierClient.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial