/
AuthorizedActivity.java
286 lines (251 loc) · 11.7 KB
/
AuthorizedActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* Copyright 2012 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.textuality.authorized;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.Random;
import android.accounts.AccountAuthenticatorActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import com.google.android.gms.auth.GoogleAuthException;
import com.google.android.gms.auth.GoogleAuthUtil;
import com.google.android.gms.auth.UserRecoverableAuthException;
/**
* Provides GET (and eventually POST) methods that are authorized using an OAuth2 token requested on
* behalf of a specified Google account.
* To use it you have to subclass it, which gives you a get() method, see below. It's asynchronous,
* so you also have to pass in a ResponseHandler object.
* The OAuth processes may require firing off one or more Activities to get user approval, so this class
* needs its OnActivityResult to be called. So if you implement OnActivityResult, you have to start with
* @Override
* protected void onActivityResult(int requestCode, int resultCode, Intent data) {
* super.onActivityResult(requestCode, resultCode, data);
*/
public class AuthorizedActivity extends AccountAuthenticatorActivity {
private static final int REQUEST_CODE = new Random().nextInt(1000000000);
public static final String TAG = "AuthorizedActivity";
// This is required because you might have to dispatch off to another activity and get a token back in
// onActivityResult, and you can�t serialize the ResponseHandler argument in Intent extras.
// It means that you can�t really have more than one request at a time in play.
private Request mRequest;
private boolean mSecondTry = false;
private String mAuthToken;
/**
* Perform an HTTP GET on a resource with OAuth2 authorization.
* This is asynchronous; results will be returned via the handler.
*
* @param uriString The URI to GET, as a string.
* @param email The email address associated with a Google account on the device, for authorization
* @param scope The scope which identifies the resource, for authorization
* @param headers Any HTTP headers which are to be transmitted with the GET request
* @param handler This object's "handle" method will be called when the GET completes, succesfully or not
*/
public void get(String uriString, String email, String scope, Map<String, List<String>> headers, ResponseHandler handler) {
try {
URL uri = new URL(uriString);
get(uri, email, scope, headers, handler);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
/**
* Perform an HTTP GET on a resource with OAuth2 authorization.
* This is asynchronous; results will be returned via the handler.
*
* @param uri The URI to GET
* @param email The email address associated with a Google account on the device, for authorization
* @param scope The scope which identifies the resource, for authorization
* @param headers Any HTTP headers which are to be transmitted with the GET request
* @param handler This object's "handle" method will be called when the GET completes, succesfully or not
*/
public void get(URL uri, String email, String scope, Map<String, List<String>> headers, ResponseHandler handler) {
mRequest = Request.create(uri, headers, email, scope, handler);
new Runner().execute();
}
// has to be done in the background because there are multiple network round-trips involved
private class Runner extends AsyncTask<Void, Void, Response> {
@Override
protected Response doInBackground(Void... params) {
return authenticateAndGo(new Backoff());
}
@Override
protected void onPostExecute(Response result) {
super.onPostExecute(result);
// if the result is null, that means we're off in another activity doing oauth stuff.
// When that finishes, it'll eventually end up calling the handler
if (result != null) {
mRequest.handler().handle(result);
}
}
}
private Response authenticateAndGo(Backoff backoff) {
Response response;
try {
mAuthToken = GoogleAuthUtil.getToken(this, mRequest.email(), mRequest.scope());
response = doGet(mAuthToken, this);
} catch (UserRecoverableAuthException userAuthEx) {
// This means that the app hasn't been authorized by the user for access to the scope, so we're going to have
// to fire off the (provided) Intent to arrange for that
// But we only want to do this once. Multiple attempts probably mean the user said no.
if (!mSecondTry) {
startActivityForResult(userAuthEx.getIntent(), REQUEST_CODE);
response = null;
} else {
response = new Response(-1, null, "Multiple approval attempts");
}
} catch (IOException ioEx) {
// Something is stressed out; the auth servers are by definition high-traffic and you can't count on
// 100% success. But it would be bad to retry instantly, so back off
if (backoff.shouldRetry()) {
backoff.backoff();
response = authenticateAndGo(backoff);
} else {
response = new Response(-1, null, "No response from authorization server.");
}
} catch (GoogleAuthException fatalAuthEx) {
Log.d(TAG, "Fatal Authorization Exception");
response = new Response(-1, null, "Fatal authorization exception: " + fatalAuthEx.getLocalizedMessage());
}
return response;
}
// Come here if user intervention was required for authentication
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE) {
if (resultCode == RESULT_OK) {
mSecondTry = true;
new Runner().execute();
} else {
mRequest.handler().handle(new Response(-1, null, "Authorization denied by user"));
}
}
}
private void addHeaders(HttpURLConnection connection, String token, Map<String, List<String>> headers) {
connection.addRequestProperty("Authorization", "OAuth " + token);
if (headers != null) {
for (String header : headers.keySet()) {
for (String value : headers.get(header)) {
connection.addRequestProperty(header, value);
}
}
}
}
private Response doGet(String token, Activity activity) {
HttpURLConnection conn = null;
Response response = new Response(-1, null, "Unknown problem");
try {
conn = (HttpURLConnection) mRequest.uri().openConnection();
addHeaders(conn, token, mRequest.headers());
BufferedInputStream in = new BufferedInputStream(conn.getInputStream());
int status = conn.getResponseCode();
String lastRedirect = null;
while (status == 302) {
String redirect = conn.getHeaderFields().get("Location").get(0);
if (!redirect.equals(lastRedirect)) {
lastRedirect = redirect;
URL redirectURI = null;
try {
redirectURI = new URL(redirect);
conn.disconnect();
conn = (HttpURLConnection) redirectURI.openConnection();
addHeaders(conn, token, mRequest.headers());
in = new BufferedInputStream(conn.getInputStream());
status = conn.getResponseCode();
} catch (MalformedURLException e) {
response = new Response(-1, null, "Malformed redirect URI: " + redirect);
}
}
}
if ((status / 100) == 2) {
response = new Response(status, conn.getHeaderFields(), readStream(in));
} else {
// apparently 401/403 never come here
BufferedInputStream err = new BufferedInputStream(conn.getErrorStream());
response = new Response(status, conn.getHeaderFields(), readStream(err));
}
} catch (Exception e) {
try {
GoogleAuthUtil.invalidateToken(activity, token);
int status = conn.getResponseCode();
if (status == 401) {
// they gave us a new token, but it 401'ed. This could be getting unlucky with the
// expiry, or it could be that access was actually revoked. So we'll recurse. If
// it was just expiry, everything should work fine. If the token was revoked,
// the getToken will fail next time around.
response = authenticateAndGo(new Backoff());
} else {
BufferedInputStream err = new BufferedInputStream(conn.getErrorStream());
response = new Response(status, conn.getHeaderFields(), readStream(err));
}
} catch (IOException ee) {
response = new Response(-1, null, ee.getLocalizedMessage().getBytes());
}
} finally {
if (conn != null) {
conn.disconnect();
}
}
return response;
}
private static byte[] readStream(InputStream in)
throws IOException {
final byte[] buf = new byte[1024];
int count = 0;
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
while ((count = in.read(buf)) != -1) {
out.write(buf, 0, count);
}
in.close();
return out.toByteArray();
}
public String getAuthToken() {
return mAuthToken;
}
static class Backoff {
private static final long INITIAL_WAIT = 1000 + new Random().nextInt(1000);
private static final long MAX_BACKOFF = 1800 * 1000;
private long mWaitInterval = INITIAL_WAIT;
private boolean mBackingOff = true;
public boolean shouldRetry() {
return mBackingOff;
}
private void noRetry() {
mBackingOff = false;
}
public void backoff() {
if (mWaitInterval > MAX_BACKOFF) {
noRetry();
} else if (mWaitInterval > 0) {
try {
Thread.sleep(mWaitInterval);
} catch (InterruptedException e) {
// life's a bitch, then you die
}
}
mWaitInterval = (mWaitInterval == 0) ? INITIAL_WAIT : mWaitInterval * 2;
}
}
}