-
Notifications
You must be signed in to change notification settings - Fork 870
/
AuthState.java
799 lines (694 loc) · 29.5 KB
/
AuthState.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
/*
* 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.appauth;
import static net.openid.appauth.AuthorizationException.AuthorizationRequestErrors;
import static net.openid.appauth.Preconditions.checkArgument;
import static net.openid.appauth.Preconditions.checkNotEmpty;
import static net.openid.appauth.Preconditions.checkNotNull;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import net.openid.appauth.internal.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Collects authorization state from authorization requests and responses. This facilitates
* the creation of subsequent requests based on this state, and allows for this state to be
* persisted easily.
*/
public class AuthState {
/**
* Tokens which have less time than this value left before expiry will be considered to be
* expired for the purposes of calls to
* {@link #performActionWithFreshTokens(AuthorizationService, AuthStateAction)
* performActionWithFreshTokens}.
*/
public static final int EXPIRY_TIME_TOLERANCE_MS = 60000;
private static final String KEY_CONFIG = "config";
private static final String KEY_REFRESH_TOKEN = "refreshToken";
private static final String KEY_SCOPE = "scope";
private static final String KEY_LAST_AUTHORIZATION_RESPONSE = "lastAuthorizationResponse";
private static final String KEY_LAST_TOKEN_RESPONSE = "mLastTokenResponse";
private static final String KEY_AUTHORIZATION_EXCEPTION = "mAuthorizationException";
private static final String KEY_LAST_REGISTRATION_RESPONSE = "lastRegistrationResponse";
@Nullable
private String mRefreshToken;
@Nullable
private String mScope;
@Nullable
private AuthorizationServiceConfiguration mConfig;
@Nullable
private AuthorizationResponse mLastAuthorizationResponse;
@Nullable
private TokenResponse mLastTokenResponse;
@Nullable
private RegistrationResponse mLastRegistrationResponse;
@Nullable
private AuthorizationException mAuthorizationException;
private final Object mPendingActionsSyncObject = new Object();
private List<AuthStateAction> mPendingActions;
private boolean mNeedsTokenRefreshOverride;
/**
* Creates an empty, unauthenticated {@link AuthState}.
*/
public AuthState() {}
/**
* Creates an unauthenticated {@link AuthState}, with the service configuration retained
* for convenience.
*/
public AuthState(@NonNull AuthorizationServiceConfiguration config) {
mConfig = config;
}
/**
* Creates an {@link AuthState} based on an authorization exchange.
*/
public AuthState(@Nullable AuthorizationResponse authResponse,
@Nullable AuthorizationException authError) {
checkArgument(authResponse != null ^ authError != null,
"exactly one of authResponse or authError should be non-null");
mPendingActions = null;
update(authResponse, authError);
}
/**
* Creates an {@link AuthState} based on a dynamic registration client registration request.
*/
public AuthState(@NonNull RegistrationResponse regResponse) {
update(regResponse);
}
/**
* Creates an {@link AuthState} based on an authorization exchange and subsequent token
* exchange.
*/
public AuthState(
@NonNull AuthorizationResponse authResponse,
@Nullable TokenResponse tokenResponse,
@Nullable AuthorizationException authException) {
this(authResponse, null);
update(tokenResponse, authException);
}
/**
* The most recent refresh token received from the server, if available. Rather than using
* this property directly as part of any request depending on authorization state, it is
* recommended to call {@link #performActionWithFreshTokens(AuthorizationService,
* AuthStateAction) performActionWithFreshTokens} to ensure that fresh tokens are available.
*/
@Nullable
public String getRefreshToken() {
return mRefreshToken;
}
/**
* The scope of the current authorization grant. This represents the latest scope returned by
* the server and may be a subset of the scope that was initially granted.
*/
@Nullable
public String getScope() {
return mScope;
}
/**
* A set representation of {@link #getScope()}, for convenience.
*/
@Nullable
public Set<String> getScopeSet() {
return AsciiStringListUtil.stringToSet(mScope);
}
/**
* The most recent authorization response used to update the authorization state. For the
* implicit flow, this will contain the latest access token. It is rarely necessary to
* directly use the response; instead convenience methods are provided to retrieve the
* {@link #getAccessToken() access token},
* {@link #getAccessTokenExpirationTime() access token expiration},
* {@link #getIdToken() ID token}
* and {@link #getScopeSet() scope} regardless of the flow used to retrieve them.
*/
@Nullable
public AuthorizationResponse getLastAuthorizationResponse() {
return mLastAuthorizationResponse;
}
/**
* The most recent token response used to update this authorization state. For the
* authorization code flow, this will contain the latest access token. It is rarely necessary
* to directly use the response; instead convenience methods are provided to retrieve the
* {@link #getAccessToken() access token},
* {@link #getAccessTokenExpirationTime() access token expiration},
* {@link #getIdToken() ID token}
* and {@link #getScopeSet() scope} regardless of the flow used to retrieve them.
*/
@Nullable
public TokenResponse getLastTokenResponse() {
return mLastTokenResponse;
}
/**
* The most recent client registration response used to update this authorization state.
*
* <p>
* It is rarely necessary to directly use the response; instead convenience methods are provided
* to retrieve the {@link #getClientSecret() client secret} and
* {@link #getClientSecretExpirationTime() client secret expiration}.
* </p>
*/
@Nullable
public RegistrationResponse getLastRegistrationResponse() {
return mLastRegistrationResponse;
}
/**
* The configuration of the authorization service associated with this authorization state.
*/
@Nullable
public AuthorizationServiceConfiguration getAuthorizationServiceConfiguration() {
if (mLastAuthorizationResponse != null) {
return mLastAuthorizationResponse.request.configuration;
}
return mConfig;
}
/**
* The current access token, if available. Rather than using
* this property directly as part of any request depending on authorization state, it s
* recommended to call {@link #performActionWithFreshTokens(AuthorizationService,
* AuthStateAction) performActionWithFreshTokens} to ensure that fresh tokens are available.
*/
@Nullable
public String getAccessToken() {
if (mAuthorizationException != null) {
return null;
}
if (mLastTokenResponse != null && mLastTokenResponse.accessToken != null) {
return mLastTokenResponse.accessToken;
}
if (mLastAuthorizationResponse != null) {
return mLastAuthorizationResponse.accessToken;
}
return null;
}
/**
* The expiration time of the current access token (if available), as milliseconds from the
* UNIX epoch (consistent with {@link System#currentTimeMillis()}).
*/
@Nullable
public Long getAccessTokenExpirationTime() {
if (mAuthorizationException != null) {
return null;
}
if (mLastTokenResponse != null && mLastTokenResponse.accessToken != null) {
return mLastTokenResponse.accessTokenExpirationTime;
}
if (mLastAuthorizationResponse != null && mLastAuthorizationResponse.accessToken != null) {
return mLastAuthorizationResponse.accessTokenExpirationTime;
}
return null;
}
/**
* The current ID token, if available.
*/
@Nullable
public String getIdToken() {
if (mAuthorizationException != null) {
return null;
}
if (mLastTokenResponse != null && mLastTokenResponse.idToken != null) {
return mLastTokenResponse.idToken;
}
if (mLastAuthorizationResponse != null) {
return mLastAuthorizationResponse.idToken;
}
return null;
}
/**
* The current parsed ID token, if available.
*/
@Nullable
public IdToken getParsedIdToken() {
String stringToken = getIdToken();
IdToken token;
if (stringToken != null) {
try {
token = IdToken.from(stringToken);
} catch (JSONException | IdToken.IdTokenException ex) {
token = null;
}
} else {
token = null;
}
return token;
}
/**
* The current client secret, if available.
*/
public String getClientSecret() {
if (mLastRegistrationResponse != null) {
return mLastRegistrationResponse.clientSecret;
}
return null;
}
/**
* The expiration time of the current client credentials (if available), as milliseconds from
* the UNIX epoch (consistent with {@link System#currentTimeMillis()}). If the value is 0, the
* client credentials will not expire.
*/
@Nullable
public Long getClientSecretExpirationTime() {
if (mLastRegistrationResponse != null) {
return mLastRegistrationResponse.clientSecretExpiresAt;
}
return null;
}
/**
* Determines whether the current state represents a successful authorization,
* from which at least either an access token or an ID token have been retrieved.
*/
public boolean isAuthorized() {
return mAuthorizationException == null
&& (getAccessToken() != null || getIdToken() != null);
}
/**
* If the last response was an OAuth related failure, this returns the exception describing
* the failure.
*/
@Nullable
public AuthorizationException getAuthorizationException() {
return mAuthorizationException;
}
/**
* Determines whether the access token is considered to have expired. If no refresh token
* has been acquired, then this method will always return `false`. A token refresh
* can be forced, regardless of the validity of any currently acquired access token, by
* calling {@link #setNeedsTokenRefresh(boolean) setNeedsTokenRefresh(true)}.
*/
public boolean getNeedsTokenRefresh() {
return getNeedsTokenRefresh(SystemClock.INSTANCE);
}
@VisibleForTesting
boolean getNeedsTokenRefresh(Clock clock) {
if (mNeedsTokenRefreshOverride) {
return true;
}
if (getAccessTokenExpirationTime() == null) {
// if there is no expiration but we have an access token, it is assumed
// to never expire.
return getAccessToken() == null;
}
return getAccessTokenExpirationTime()
<= clock.getCurrentTimeMillis() + EXPIRY_TIME_TOLERANCE_MS;
}
/**
* Sets whether to force an access token refresh, regardless of the current access token's
* expiration time.
*/
public void setNeedsTokenRefresh(boolean needsTokenRefresh) {
mNeedsTokenRefreshOverride = needsTokenRefresh;
}
/**
* Determines whether the client credentials is considered to have expired. If no client
* credentials have been acquired, then this method will always return `false`
*/
public boolean hasClientSecretExpired() {
return hasClientSecretExpired(SystemClock.INSTANCE);
}
@VisibleForTesting
boolean hasClientSecretExpired(Clock clock) {
if (getClientSecretExpirationTime() == null || getClientSecretExpirationTime() == 0) {
// no explicit expiration time, and 0 means it will not expire
return false;
}
return getClientSecretExpirationTime() <= clock.getCurrentTimeMillis();
}
/**
* Updates the authorization state based on a new authorization response.
*/
public void update(
@Nullable AuthorizationResponse authResponse,
@Nullable AuthorizationException authException) {
checkArgument(authResponse != null ^ authException != null,
"exactly one of authResponse or authException should be non-null");
if (authException != null) {
if (authException.type == AuthorizationException.TYPE_OAUTH_AUTHORIZATION_ERROR) {
mAuthorizationException = authException;
}
return;
}
// the last token response and refresh token are now stale, as they are associated with
// any previous authorization response
mLastAuthorizationResponse = authResponse;
mConfig = null;
mLastTokenResponse = null;
mRefreshToken = null;
mAuthorizationException = null;
// if the response's mScope is null, it means that it equals that of the request
// see: https://tools.ietf.org/html/rfc6749#section-5.1
mScope = (authResponse.scope != null) ? authResponse.scope : authResponse.request.scope;
}
/**
* Updates the authorization state based on a new token response.
*/
public void update(
@Nullable TokenResponse tokenResponse,
@Nullable AuthorizationException authException) {
checkArgument(tokenResponse != null ^ authException != null,
"exactly one of tokenResponse or authException should be non-null");
if (mAuthorizationException != null) {
// Calling updateFromTokenResponse while in an error state probably means the developer
// obtained a new token and did the exchange without also calling
// updateFromAuthorizationResponse. Attempt to handle this gracefully, but warn the
// developer that this is unexpected.
Logger.warn(
"AuthState.update should not be called in an error state (%s), call update"
+ "with the result of the fresh authorization response first",
mAuthorizationException);
mAuthorizationException = null;
}
if (authException != null) {
if (authException.type == AuthorizationException.TYPE_OAUTH_TOKEN_ERROR) {
mAuthorizationException = authException;
}
return;
}
mLastTokenResponse = tokenResponse;
if (tokenResponse.scope != null) {
mScope = tokenResponse.scope;
}
if (tokenResponse.refreshToken != null) {
mRefreshToken = tokenResponse.refreshToken;
}
}
/**
* Updates the authorization state based on a new client registration response.
*/
public void update(@Nullable RegistrationResponse regResponse) {
mLastRegistrationResponse = regResponse;
// a new client registration will have a new client id, so invalidate the current session.
// Note however that we do not discard the configuration; this is likely still applicable.
mConfig = getAuthorizationServiceConfiguration();
mRefreshToken = null;
mScope = null;
mLastAuthorizationResponse = null;
mLastTokenResponse = null;
mAuthorizationException = null;
}
/**
* Ensures that a non-expired access token is available before invoking the provided action.
*/
public void performActionWithFreshTokens(
@NonNull AuthorizationService service,
@NonNull AuthStateAction action) {
performActionWithFreshTokens(
service,
NoClientAuthentication.INSTANCE,
Collections.<String, String>emptyMap(),
SystemClock.INSTANCE,
action);
}
/**
* Ensures that a non-expired access token is available before invoking the provided action.
*/
public void performActionWithFreshTokens(
@NonNull AuthorizationService service,
@NonNull ClientAuthentication clientAuth,
@NonNull AuthStateAction action) {
performActionWithFreshTokens(
service,
clientAuth,
Collections.<String, String>emptyMap(),
SystemClock.INSTANCE,
action);
}
/**
* Ensures that a non-expired access token is available before invoking the provided action.
* If a token refresh is required, the provided additional parameters will be included in this
* refresh request.
*/
public void performActionWithFreshTokens(
@NonNull AuthorizationService service,
@NonNull Map<String, String> refreshTokenAdditionalParams,
@NonNull AuthStateAction action) {
try {
performActionWithFreshTokens(
service,
getClientAuthentication(),
refreshTokenAdditionalParams,
SystemClock.INSTANCE,
action);
} catch (ClientAuthentication.UnsupportedAuthenticationMethod ex) {
action.execute(null, null,
AuthorizationException.fromTemplate(
AuthorizationException.TokenRequestErrors.CLIENT_ERROR, ex));
}
}
/**
* Ensures that a non-expired access token is available before invoking the provided action.
* If a token refresh is required, the provided additional parameters will be included in this
* refresh request.
*/
public void performActionWithFreshTokens(
@NonNull AuthorizationService service,
@NonNull ClientAuthentication clientAuth,
@NonNull Map<String, String> refreshTokenAdditionalParams,
@NonNull AuthStateAction action) {
performActionWithFreshTokens(
service,
clientAuth,
refreshTokenAdditionalParams,
SystemClock.INSTANCE,
action);
}
@VisibleForTesting
void performActionWithFreshTokens(
@NonNull final AuthorizationService service,
@NonNull final ClientAuthentication clientAuth,
@NonNull final Map<String, String> refreshTokenAdditionalParams,
@NonNull final Clock clock,
@NonNull final AuthStateAction action) {
checkNotNull(service, "service cannot be null");
checkNotNull(clientAuth, "client authentication cannot be null");
checkNotNull(refreshTokenAdditionalParams,
"additional params cannot be null");
checkNotNull(clock, "clock cannot be null");
checkNotNull(action, "action cannot be null");
if (!getNeedsTokenRefresh(clock)) {
action.execute(getAccessToken(), getIdToken(), null);
return;
}
if (mRefreshToken == null) {
AuthorizationException ex = AuthorizationException.fromTemplate(
AuthorizationRequestErrors.CLIENT_ERROR,
new IllegalStateException("No refresh token available and token have expired"));
action.execute(null, null, ex);
return;
}
checkNotNull(mPendingActionsSyncObject, "pending actions sync object cannot be null");
synchronized (mPendingActionsSyncObject) {
//if a token request is currently executing, queue the actions instead
if (mPendingActions != null) {
mPendingActions.add(action);
return;
}
//creates a list of pending actions, starting with the current action
mPendingActions = new ArrayList<>();
mPendingActions.add(action);
}
service.performTokenRequest(
createTokenRefreshRequest(refreshTokenAdditionalParams),
clientAuth,
new AuthorizationService.TokenResponseCallback() {
@Override
public void onTokenRequestCompleted(
@Nullable TokenResponse response,
@Nullable AuthorizationException ex) {
update(response, ex);
String accessToken = null;
String idToken = null;
AuthorizationException exception = null;
if (ex == null) {
mNeedsTokenRefreshOverride = false;
accessToken = getAccessToken();
idToken = getIdToken();
} else {
exception = ex;
}
//sets pending queue to null and processes all actions in the queue
List<AuthStateAction> actionsToProcess;
synchronized (mPendingActionsSyncObject) {
actionsToProcess = mPendingActions;
mPendingActions = null;
}
for (AuthStateAction action : actionsToProcess) {
action.execute(accessToken, idToken, exception);
}
}
});
}
/**
* Creates a token request for new tokens using the current refresh token.
*/
@NonNull
public TokenRequest createTokenRefreshRequest() {
return createTokenRefreshRequest(Collections.<String, String>emptyMap());
}
/**
* Creates a token request for new tokens using the current refresh token, adding the
* specified additional parameters.
*/
@NonNull
public TokenRequest createTokenRefreshRequest(
@NonNull Map<String, String> additionalParameters) {
if (mRefreshToken == null) {
throw new IllegalStateException("No refresh token available for refresh request");
}
if (mLastAuthorizationResponse == null) {
throw new IllegalStateException(
"No authorization configuration available for refresh request");
}
return new TokenRequest.Builder(
mLastAuthorizationResponse.request.configuration,
mLastAuthorizationResponse.request.clientId)
.setGrantType(GrantTypeValues.REFRESH_TOKEN)
.setScope(null)
.setRefreshToken(mRefreshToken)
.setAdditionalParameters(additionalParameters)
.build();
}
/**
* Produces a JSON representation of the authorization state for persistent storage or local
* transmission (e.g. between activities).
*/
public JSONObject jsonSerialize() {
JSONObject json = new JSONObject();
JsonUtil.putIfNotNull(json, KEY_REFRESH_TOKEN, mRefreshToken);
JsonUtil.putIfNotNull(json, KEY_SCOPE, mScope);
if (mConfig != null) {
JsonUtil.put(json, KEY_CONFIG, mConfig.toJson());
}
if (mAuthorizationException != null) {
JsonUtil.put(json, KEY_AUTHORIZATION_EXCEPTION, mAuthorizationException.toJson());
}
if (mLastAuthorizationResponse != null) {
JsonUtil.put(
json,
KEY_LAST_AUTHORIZATION_RESPONSE,
mLastAuthorizationResponse.jsonSerialize());
}
if (mLastTokenResponse != null) {
JsonUtil.put(
json,
KEY_LAST_TOKEN_RESPONSE,
mLastTokenResponse.jsonSerialize());
}
if (mLastRegistrationResponse != null) {
JsonUtil.put(
json,
KEY_LAST_REGISTRATION_RESPONSE,
mLastRegistrationResponse.jsonSerialize());
}
return json;
}
/**
* Produces a JSON string representation of the authorization state for persistent storage or
* local transmission (e.g. between activities). This method is just a convenience wrapper
* for {@link #jsonSerialize()}, converting the JSON object to its string form.
*/
public String jsonSerializeString() {
return jsonSerialize().toString();
}
/**
* Reads an authorization state instance from a JSON string representation produced by
* {@link #jsonSerialize()}.
* @throws JSONException if the provided JSON does not match the expected structure.
*/
public static AuthState jsonDeserialize(@NonNull JSONObject json) throws JSONException {
checkNotNull(json, "json cannot be null");
AuthState state = new AuthState();
state.mRefreshToken = JsonUtil.getStringIfDefined(json, KEY_REFRESH_TOKEN);
state.mScope = JsonUtil.getStringIfDefined(json, KEY_SCOPE);
if (json.has(KEY_CONFIG)) {
state.mConfig = AuthorizationServiceConfiguration.fromJson(
json.getJSONObject(KEY_CONFIG));
}
if (json.has(KEY_AUTHORIZATION_EXCEPTION)) {
state.mAuthorizationException = AuthorizationException.fromJson(
json.getJSONObject(KEY_AUTHORIZATION_EXCEPTION));
}
if (json.has(KEY_LAST_AUTHORIZATION_RESPONSE)) {
state.mLastAuthorizationResponse = AuthorizationResponse.jsonDeserialize(
json.getJSONObject(KEY_LAST_AUTHORIZATION_RESPONSE));
}
if (json.has(KEY_LAST_TOKEN_RESPONSE)) {
state.mLastTokenResponse = TokenResponse.jsonDeserialize(
json.getJSONObject(KEY_LAST_TOKEN_RESPONSE));
}
if (json.has(KEY_LAST_REGISTRATION_RESPONSE)) {
state.mLastRegistrationResponse = RegistrationResponse.jsonDeserialize(
json.getJSONObject(KEY_LAST_REGISTRATION_RESPONSE));
}
return state;
}
/**
* Reads an authorization state instance from a JSON string representation produced by
* {@link #jsonSerializeString()}. This method is just a convenience wrapper for
* {@link #jsonDeserialize(JSONObject)}, converting the JSON string to its JSON object form.
* @throws JSONException if the provided JSON does not match the expected structure.
*/
public static AuthState jsonDeserialize(@NonNull String jsonStr) throws JSONException {
checkNotEmpty(jsonStr, "jsonStr cannot be null or empty");
return jsonDeserialize(new JSONObject(jsonStr));
}
/**
* Interface for actions executed in the context of fresh (non-expired) tokens.
* @see #performActionWithFreshTokens(AuthorizationService, AuthStateAction)
*/
public interface AuthStateAction {
/**
* Executed in the context of fresh (non-expired) tokens. If new tokens were
* required to execute the action and could not be acquired, an authorization
* exception is provided instead. One or both of the access token and ID token will be
* provided, dependent upon the token types previously negotiated.
*/
void execute(
@Nullable String accessToken,
@Nullable String idToken,
@Nullable AuthorizationException ex);
}
/**
* Creates the required client authentication for the token endpoint based on information
* in the most recent registration response (if it is set).
*
* @throws ClientAuthentication.UnsupportedAuthenticationMethod if the expected client
* authentication method is unsupported by this client library.
*/
public ClientAuthentication getClientAuthentication() throws
ClientAuthentication.UnsupportedAuthenticationMethod {
if (getClientSecret() == null) {
/* Without client credentials, or unspecified 'token_endpoint_auth_method',
* we can never authenticate */
return NoClientAuthentication.INSTANCE;
} else if (mLastRegistrationResponse.tokenEndpointAuthMethod == null) {
/* 'token_endpoint_auth_method': "If omitted, the default is client_secret_basic",
* "OpenID Connect Dynamic Client Registration 1.0", Section 2 */
return new ClientSecretBasic(getClientSecret());
}
switch (mLastRegistrationResponse.tokenEndpointAuthMethod) {
case ClientSecretBasic.NAME:
return new ClientSecretBasic(getClientSecret());
case ClientSecretPost.NAME:
return new ClientSecretPost(getClientSecret());
case "none":
return NoClientAuthentication.INSTANCE;
default:
throw new ClientAuthentication.UnsupportedAuthenticationMethod(
mLastRegistrationResponse.tokenEndpointAuthMethod);
}
}
}