|
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/. */ |
|
4 |
|
5 package org.mozilla.gecko.browserid.verifier; |
|
6 |
|
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; |
|
14 |
|
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; |
|
23 |
|
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; |
|
29 |
|
30 public class BrowserIDRemoteVerifierClient implements BrowserIDVerifierClient { |
|
31 protected static class RemoteVerifierResourceDelegate extends BaseResourceDelegate { |
|
32 private final BrowserIDVerifierDelegate delegate; |
|
33 |
|
34 protected RemoteVerifierResourceDelegate(Resource resource, BrowserIDVerifierDelegate delegate) { |
|
35 super(resource); |
|
36 this.delegate = delegate; |
|
37 } |
|
38 |
|
39 @Override |
|
40 public String getUserAgent() { |
|
41 return null; |
|
42 } |
|
43 |
|
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 + "."); |
|
49 |
|
50 if (statusCode != 200) { |
|
51 delegate.handleError(new BrowserIDVerifierErrorResponseException("Expected status code 200.")); |
|
52 return; |
|
53 } |
|
54 |
|
55 ExtendedJSONObject o = null; |
|
56 try { |
|
57 o = res.jsonObjectBody(); |
|
58 } catch (Exception e) { |
|
59 delegate.handleError(new BrowserIDVerifierMalformedResponseException(e)); |
|
60 return; |
|
61 } |
|
62 |
|
63 String status = o.getString("status"); |
|
64 if ("failure".equals(status)) { |
|
65 delegate.handleFailure(o); |
|
66 return; |
|
67 } |
|
68 |
|
69 if (!("okay".equals(status))) { |
|
70 delegate.handleError(new BrowserIDVerifierMalformedResponseException("Expected status okay, got '" + status + "'.")); |
|
71 return; |
|
72 } |
|
73 |
|
74 delegate.handleSuccess(o); |
|
75 } |
|
76 |
|
77 @Override |
|
78 public void handleTransportException(GeneralSecurityException e) { |
|
79 Logger.warn(LOG_TAG, "Got transport exception.", e); |
|
80 delegate.handleError(e); |
|
81 } |
|
82 |
|
83 @Override |
|
84 public void handleHttpProtocolException(ClientProtocolException e) { |
|
85 Logger.warn(LOG_TAG, "Got protocol exception.", e); |
|
86 delegate.handleError(e); |
|
87 } |
|
88 |
|
89 @Override |
|
90 public void handleHttpIOException(IOException e) { |
|
91 Logger.warn(LOG_TAG, "Got IO exception.", e); |
|
92 delegate.handleError(e); |
|
93 } |
|
94 } |
|
95 |
|
96 public static final String LOG_TAG = "BrowserIDRemoteVerifierClient"; |
|
97 |
|
98 public static final String DEFAULT_VERIFIER_URL = "https://verifier.login.persona.org/verify"; |
|
99 |
|
100 protected final URI verifierUri; |
|
101 |
|
102 public BrowserIDRemoteVerifierClient(URI verifierUri) { |
|
103 this.verifierUri = verifierUri; |
|
104 } |
|
105 |
|
106 public BrowserIDRemoteVerifierClient() throws URISyntaxException { |
|
107 this.verifierUri = new URI(DEFAULT_VERIFIER_URL); |
|
108 } |
|
109 |
|
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 } |
|
121 |
|
122 BaseResource r = new BaseResource(verifierUri); |
|
123 |
|
124 r.delegate = new RemoteVerifierResourceDelegate(r, delegate); |
|
125 |
|
126 List<NameValuePair> nvps = Arrays.asList(new NameValuePair[] { |
|
127 new BasicNameValuePair("audience", audience), |
|
128 new BasicNameValuePair("assertion", assertion) }); |
|
129 |
|
130 try { |
|
131 r.post(new UrlEncodedFormEntity(nvps, "UTF-8")); |
|
132 } catch (UnsupportedEncodingException e) { |
|
133 delegate.handleError(e); |
|
134 } |
|
135 } |
|
136 } |