-
Notifications
You must be signed in to change notification settings - Fork 870
/
TokenActivity.java
423 lines (370 loc) · 16.3 KB
/
TokenActivity.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
* Copyright 2015 The AppAuth for Android Authors. All Rights Reserved.
*
* 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 net.openid.appauthdemo;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.MainThread;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.design.widget.Snackbar;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
import net.openid.appauth.AuthState;
import net.openid.appauth.AuthorizationException;
import net.openid.appauth.AuthorizationRequest;
import net.openid.appauth.AuthorizationResponse;
import net.openid.appauth.AuthorizationService;
import net.openid.appauth.AuthorizationServiceDiscovery;
import net.openid.appauth.ClientAuthentication;
import net.openid.appauth.TokenRequest;
import net.openid.appauth.TokenResponse;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.util.Date;
/**
* A sample activity to serve as a client to the Native Oauth library.
*/
public class TokenActivity extends AppCompatActivity {
private static final String TAG = "TokenActivity";
private static final String KEY_AUTH_STATE = "authState";
private static final String KEY_USER_INFO = "userInfo";
private static final String EXTRA_AUTH_SERVICE_DISCOVERY = "authServiceDiscovery";
private static final String EXTRA_AUTH_STATE = "authState";
private static final int BUFFER_SIZE = 1024;
private AuthState mAuthState;
private AuthorizationService mAuthService;
private JSONObject mUserInfoJson;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_token);
mAuthService = new AuthorizationService(this);
if (savedInstanceState != null) {
if (savedInstanceState.containsKey(KEY_AUTH_STATE)) {
try {
mAuthState = AuthState.jsonDeserialize(
savedInstanceState.getString(KEY_AUTH_STATE));
} catch (JSONException ex) {
Log.e(TAG, "Malformed authorization JSON saved", ex);
}
}
if (savedInstanceState.containsKey(KEY_USER_INFO)) {
try {
mUserInfoJson = new JSONObject(savedInstanceState.getString(KEY_USER_INFO));
} catch (JSONException ex) {
Log.e(TAG, "Failed to parse saved user info JSON", ex);
}
}
}
if (mAuthState == null) {
mAuthState = getAuthStateFromIntent(getIntent());
AuthorizationResponse response = AuthorizationResponse.fromIntent(getIntent());
AuthorizationException ex = AuthorizationException.fromIntent(getIntent());
mAuthState.update(response, ex);
if (response != null) {
Log.d(TAG, "Received AuthorizationResponse.");
showSnackbar(R.string.exchange_notification);
exchangeAuthorizationCode(response);
} else {
Log.i(TAG, "Authorization failed: " + ex);
showSnackbar(R.string.authorization_failed);
}
}
refreshUi();
}
@Override
protected void onSaveInstanceState(Bundle state) {
if (mAuthState != null) {
state.putString(KEY_AUTH_STATE, mAuthState.jsonSerializeString());
}
if (mUserInfoJson != null) {
state.putString(KEY_USER_INFO, mUserInfoJson.toString());
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mAuthService.dispose();
}
private void receivedTokenResponse(
@Nullable TokenResponse tokenResponse,
@Nullable AuthorizationException authException) {
Log.d(TAG, "Token request complete");
mAuthState.update(tokenResponse, authException);
showSnackbar((tokenResponse != null)
? R.string.exchange_complete
: R.string.refresh_failed);
refreshUi();
}
private void refreshUi() {
TextView refreshTokenInfoView = (TextView) findViewById(R.id.refresh_token_info);
TextView accessTokenInfoView = (TextView) findViewById(R.id.access_token_info);
TextView idTokenInfoView = (TextView) findViewById(R.id.id_token_info);
Button refreshTokenButton = (Button) findViewById(R.id.refresh_token);
if (mAuthState.isAuthorized()) {
refreshTokenInfoView.setText((mAuthState.getRefreshToken() == null)
? R.string.no_refresh_token_returned
: R.string.refresh_token_returned);
idTokenInfoView.setText((mAuthState.getIdToken()) == null
? R.string.no_id_token_returned
: R.string.id_token_returned);
if (mAuthState.getAccessToken() == null) {
accessTokenInfoView.setText(R.string.no_access_token_returned);
} else {
Long expiresAt = mAuthState.getAccessTokenExpirationTime();
String expiryStr;
if (expiresAt == null) {
expiryStr = getResources().getString(R.string.unknown_expiry);
} else {
expiryStr = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL)
.format(new Date(expiresAt));
}
String tokenInfo = String.format(
getResources().getString(R.string.access_token_expires_at),
expiryStr);
accessTokenInfoView.setText(tokenInfo);
}
}
refreshTokenButton.setVisibility(mAuthState.getRefreshToken() != null
? View.VISIBLE
: View.GONE);
refreshTokenButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
refreshAccessToken();
}
});
Button viewProfileButton = (Button) findViewById(R.id.view_profile);
AuthorizationServiceDiscovery discoveryDoc = getDiscoveryDocFromIntent(getIntent());
if (!mAuthState.isAuthorized()
|| discoveryDoc == null
|| discoveryDoc.getUserinfoEndpoint() == null) {
viewProfileButton.setVisibility(View.GONE);
} else {
viewProfileButton.setVisibility(View.VISIBLE);
viewProfileButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
fetchUserInfo();
return null;
}
}.execute();
}
});
}
View userInfoCard = findViewById(R.id.userinfo_card);
if (mUserInfoJson == null) {
userInfoCard.setVisibility(View.INVISIBLE);
} else {
try {
String name = "???";
if (mUserInfoJson.has("name")) {
name = mUserInfoJson.getString("name");
}
final TextView userHeader = ((TextView) findViewById(R.id.userinfo_name));
userHeader.setText(name);
if (mUserInfoJson.has("picture")) {
int profilePictureSize =
getResources().getDimensionPixelSize(R.dimen.profile_pic_size);
Picasso.with(TokenActivity.this)
.load(Uri.parse(mUserInfoJson.getString("picture")))
.resize(profilePictureSize, profilePictureSize)
.into(new UserProfilePictureTarget());
}
((TextView) findViewById(R.id.userinfo_json)).setText(mUserInfoJson.toString(2));
userInfoCard.setVisibility(View.VISIBLE);
} catch (JSONException ex) {
Log.e(TAG, "Failed to read userinfo JSON", ex);
}
}
}
private void refreshAccessToken() {
performTokenRequest(mAuthState.createTokenRefreshRequest());
}
private void exchangeAuthorizationCode(AuthorizationResponse authorizationResponse) {
performTokenRequest(authorizationResponse.createTokenExchangeRequest());
}
private void performTokenRequest(TokenRequest request) {
ClientAuthentication clientAuthentication = null;
try {
clientAuthentication = mAuthState.getClientAuthentication();
} catch (ClientAuthentication.UnsupportedAuthenticationMethod ex) {
Log.d(TAG, "Token request cannot be made, client authentication for the token "
+ "endpoint could not be constructed (%s)", ex);
return;
}
mAuthService.performTokenRequest(
request,
clientAuthentication,
new AuthorizationService.TokenResponseCallback() {
@Override
public void onTokenRequestCompleted(
@Nullable TokenResponse tokenResponse,
@Nullable AuthorizationException ex) {
receivedTokenResponse(tokenResponse, ex);
}
});
}
private void fetchUserInfo() {
if (mAuthState.getAuthorizationServiceConfiguration() == null) {
Log.e(TAG, "Cannot make userInfo request without service configuration");
}
mAuthState.performActionWithFreshTokens(mAuthService, new AuthState.AuthStateAction() {
@Override
public void execute(String accessToken, String idToken, AuthorizationException ex) {
if (ex != null) {
Log.e(TAG, "Token refresh failed when fetching user info");
return;
}
AuthorizationServiceDiscovery discoveryDoc = getDiscoveryDocFromIntent(getIntent());
if (discoveryDoc == null) {
throw new IllegalStateException("no available discovery doc");
}
URL userInfoEndpoint;
try {
userInfoEndpoint = new URL(discoveryDoc.getUserinfoEndpoint().toString());
} catch (MalformedURLException urlEx) {
Log.e(TAG, "Failed to construct user info endpoint URL", urlEx);
return;
}
InputStream userInfoResponse = null;
try {
HttpURLConnection conn = (HttpURLConnection) userInfoEndpoint.openConnection();
conn.setRequestProperty("Authorization", "Bearer " + accessToken);
conn.setInstanceFollowRedirects(false);
userInfoResponse = conn.getInputStream();
String response = readStream(userInfoResponse);
updateUserInfo(new JSONObject(response));
} catch (IOException ioEx) {
Log.e(TAG, "Network error when querying userinfo endpoint", ioEx);
} catch (JSONException jsonEx) {
Log.e(TAG, "Failed to parse userinfo response");
} finally {
if (userInfoResponse != null) {
try {
userInfoResponse.close();
} catch (IOException ioEx) {
Log.e(TAG, "Failed to close userinfo response stream", ioEx);
}
}
}
}
});
}
private void updateUserInfo(final JSONObject jsonObject) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
mUserInfoJson = jsonObject;
refreshUi();
}
});
}
@MainThread
private void showSnackbar(@StringRes int messageId) {
Snackbar.make(findViewById(R.id.coordinator),
getResources().getString(messageId),
Snackbar.LENGTH_SHORT)
.show();
}
private static String readStream(InputStream stream) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(stream));
char[] buffer = new char[BUFFER_SIZE];
StringBuilder sb = new StringBuilder();
int readCount;
while ((readCount = br.read(buffer)) != -1) {
sb.append(buffer, 0, readCount);
}
return sb.toString();
}
static PendingIntent createPostAuthorizationIntent(
@NonNull Context context,
@NonNull AuthorizationRequest request,
@Nullable AuthorizationServiceDiscovery discoveryDoc,
@Nullable AuthState authState) {
Intent intent = new Intent(context, TokenActivity.class);
if (discoveryDoc != null) {
intent.putExtra(EXTRA_AUTH_SERVICE_DISCOVERY, discoveryDoc.docJson.toString());
}
if (authState != null) {
intent.putExtra(EXTRA_AUTH_STATE, authState.jsonSerializeString());
}
return PendingIntent.getActivity(context, request.hashCode(), intent, 0);
}
static AuthorizationServiceDiscovery getDiscoveryDocFromIntent(Intent intent) {
if (!intent.hasExtra(EXTRA_AUTH_SERVICE_DISCOVERY)) {
return null;
}
String discoveryJson = intent.getStringExtra(EXTRA_AUTH_SERVICE_DISCOVERY);
try {
return new AuthorizationServiceDiscovery(new JSONObject(discoveryJson));
} catch (JSONException | AuthorizationServiceDiscovery.MissingArgumentException ex) {
throw new IllegalStateException("Malformed JSON in discovery doc");
}
}
static AuthState getAuthStateFromIntent(Intent intent) {
if (!intent.hasExtra(EXTRA_AUTH_STATE)) {
return null;
}
try {
return AuthState.jsonDeserialize(intent.getStringExtra(EXTRA_AUTH_STATE));
} catch (JSONException ex) {
Log.e(TAG, "Malformed AuthState JSON saved", ex);
return null;
}
}
private class UserProfilePictureTarget implements Target {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
BitmapDrawable image = new BitmapDrawable(getResources(), bitmap);
TextView userNameView = ((TextView)findViewById(R.id.userinfo_name));
if (ViewCompat.getLayoutDirection(userNameView) == ViewCompat.LAYOUT_DIRECTION_LTR) {
userNameView.setCompoundDrawablesWithIntrinsicBounds(image, null, null, null);
} else {
userNameView.setCompoundDrawablesWithIntrinsicBounds(null, null, image, null);
}
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {}
}
}