-
Notifications
You must be signed in to change notification settings - Fork 840
Expand file tree
/
Copy pathOIDAuthState.m
More file actions
615 lines (523 loc) · 23.7 KB
/
OIDAuthState.m
File metadata and controls
615 lines (523 loc) · 23.7 KB
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
/*! @file OIDAuthState.m
@brief AppAuth iOS SDK
@copyright
Copyright 2015 Google Inc. All Rights Reserved.
@copydetails
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.
*/
#import "OIDAuthState.h"
#import "OIDAuthStateChangeDelegate.h"
#import "OIDAuthStateErrorDelegate.h"
#import "OIDAuthorizationRequest.h"
#import "OIDAuthorizationResponse.h"
#import "OIDAuthorizationService.h"
#import "OIDDefines.h"
#import "OIDError.h"
#import "OIDErrorUtilities.h"
#import "OIDRegistrationResponse.h"
#import "OIDTokenRequest.h"
#import "OIDTokenResponse.h"
#import "OIDTokenUtilities.h"
/*! @brief Key used to encode the @c refreshToken property for @c NSSecureCoding.
*/
static NSString *const kRefreshTokenKey = @"refreshToken";
/*! @brief Key used to encode the @c needsTokenRefresh property for @c NSSecureCoding.
*/
static NSString *const kNeedsTokenRefreshKey = @"needsTokenRefresh";
/*! @brief Key used to encode the @c scope property for @c NSSecureCoding.
*/
static NSString *const kScopeKey = @"scope";
/*! @brief Key used to encode the @c lastAuthorizationResponse property for @c NSSecureCoding.
*/
static NSString *const kLastAuthorizationResponseKey = @"lastAuthorizationResponse";
/*! @brief Key used to encode the @c lastTokenResponse property for @c NSSecureCoding.
*/
static NSString *const kLastTokenResponseKey = @"lastTokenResponse";
/*! @brief Key used to encode the @c lastOAuthError property for @c NSSecureCoding.
*/
static NSString *const kAuthorizationErrorKey = @"authorizationError";
/*! @brief Number of seconds the access token is refreshed before it actually expires.
*/
static const NSUInteger kExpiryTimeTolerance = 60;
/*! @brief Object to hold OIDAuthState pending actions.
*/
@interface OIDAuthStatePendingAction : NSObject
@property(nonatomic, readonly, nullable) OIDAuthStateAction action;
@property(nonatomic, readonly, nullable) dispatch_queue_t dispatchQueue;
@end
@implementation OIDAuthStatePendingAction
- (id)initWithAction:(OIDAuthStateAction)action andDispatchQueue:(dispatch_queue_t)dispatchQueue {
self = [super init];
if (self) {
_action = action;
_dispatchQueue = dispatchQueue;
}
return self;
}
@end
@interface OIDAuthState ()
/*! @brief The access token generated by the authorization server.
@discussion Rather than using this property directly, you should call
@c OIDAuthState.withFreshTokenPerformAction:.
*/
@property(nonatomic, readonly, nullable) NSString *accessToken;
/*! @brief The approximate expiration date & time of the access token.
@discussion Rather than using this property directly, you should call
@c OIDAuthState.withFreshTokenPerformAction:.
*/
@property(nonatomic, readonly, nullable) NSDate *accessTokenExpirationDate;
/*! @brief ID Token value associated with the authenticated session.
@discussion Rather than using this property directly, you should call
OIDAuthState.withFreshTokenPerformAction:.
*/
@property(nonatomic, readonly, nullable) NSString *idToken;
/*! @brief Private method, called when the internal state changes.
*/
- (void)didChangeState;
@end
static dispatch_queue_t _sharedDelegateQueue = nil;
@implementation OIDAuthState {
/*! @brief Array of pending actions (use @c _pendingActionsSyncObject to synchronize access).
*/
NSMutableArray *_pendingActions;
/*! @brief Object for synchronizing access to @c pendingActions.
*/
id _pendingActionsSyncObject;
/*! @brief If YES, tokens will be refreshed on the next API call regardless of expiry.
*/
BOOL _needsTokenRefresh;
}
#pragma mark - Convenience initializers
+ (id<OIDExternalUserAgentSession>)
authStateByPresentingAuthorizationRequest:(OIDAuthorizationRequest *)authorizationRequest
externalUserAgent:(id<OIDExternalUserAgent>)externalUserAgent
callback:(OIDAuthStateAuthorizationCallback)callback {
// presents the authorization request
id<OIDExternalUserAgentSession> authFlowSession = [OIDAuthorizationService
presentAuthorizationRequest:authorizationRequest
externalUserAgent:externalUserAgent
callback:^(OIDAuthorizationResponse *_Nullable authorizationResponse,
NSError *_Nullable authorizationError) {
// inspects response and processes further if needed (e.g. authorization
// code exchange)
if (authorizationResponse) {
if ([authorizationRequest.responseType
isEqualToString:OIDResponseTypeCode]) {
// if the request is for the code flow (NB. not hybrid), assumes the
// code is intended for this client, and performs the authorization
// code exchange
OIDTokenRequest *tokenExchangeRequest =
[authorizationResponse tokenExchangeRequest];
[OIDAuthorizationService performTokenRequest:tokenExchangeRequest
originalAuthorizationResponse:authorizationResponse
callback:^(OIDTokenResponse *_Nullable tokenResponse,
NSError *_Nullable tokenError) {
OIDAuthState *authState;
if (tokenResponse) {
authState = [[OIDAuthState alloc]
initWithAuthorizationResponse:
authorizationResponse
tokenResponse:tokenResponse];
}
callback(authState, tokenError);
}];
} else {
// hybrid flow (code id_token). Two possible cases:
// 1. The code is not for this client, ie. will be sent to a
// webservice that performs the id token verification and token
// exchange
// 2. The code is for this client and, for security reasons, the
// application developer must verify the id_token signature and
// c_hash before calling the token endpoint
OIDAuthState *authState = [[OIDAuthState alloc]
initWithAuthorizationResponse:authorizationResponse];
callback(authState, authorizationError);
}
} else {
callback(nil, authorizationError);
}
}];
return authFlowSession;
}
#pragma mark - Initializers
- (nonnull instancetype)init
OID_UNAVAILABLE_USE_INITIALIZER(@selector(initWithAuthorizationResponse:tokenResponse:))
/*! @brief Creates an auth state from an authorization response.
@param authorizationResponse The authorization response.
*/
- (instancetype)initWithAuthorizationResponse:(OIDAuthorizationResponse *)authorizationResponse {
return [self initWithAuthorizationResponse:authorizationResponse tokenResponse:nil];
}
/*! @brief Designated initializer.
@param authorizationResponse The authorization response.
@discussion Creates an auth state from an authorization response and token response.
*/
- (instancetype)initWithAuthorizationResponse:(OIDAuthorizationResponse *)authorizationResponse
tokenResponse:(nullable OIDTokenResponse *)tokenResponse {
return [self initWithAuthorizationResponse:authorizationResponse
tokenResponse:tokenResponse
registrationResponse:nil];
}
/*! @brief Creates an auth state from an registration response.
@param registrationResponse The registration response.
*/
- (instancetype)initWithRegistrationResponse:(OIDRegistrationResponse *)registrationResponse {
return [self initWithAuthorizationResponse:nil
tokenResponse:nil
registrationResponse:registrationResponse];
}
- (instancetype)initWithAuthorizationResponse:
(nullable OIDAuthorizationResponse *)authorizationResponse
tokenResponse:(nullable OIDTokenResponse *)tokenResponse
registrationResponse:(nullable OIDRegistrationResponse *)registrationResponse {
self = [super init];
if (self) {
_pendingActionsSyncObject = [[NSObject alloc] init];
if (registrationResponse) {
[self updateWithRegistrationResponse:registrationResponse];
}
if (authorizationResponse) {
[self updateWithAuthorizationResponse:authorizationResponse error:nil];
}
if (tokenResponse) {
[self updateWithTokenResponse:tokenResponse error:nil];
}
}
return self;
}
#pragma mark - NSObject overrides
- (NSString *)description {
return [NSString stringWithFormat:@"<%@: %p, isAuthorized: %@, refreshToken: \"%@\", "
"scope: \"%@\", accessToken: \"%@\", "
"accessTokenExpirationDate: %@, idToken: \"%@\", "
"lastAuthorizationResponse: %@, lastTokenResponse: %@, "
"lastRegistrationResponse: %@, authorizationError: %@>",
NSStringFromClass([self class]),
(void *)self,
(self.isAuthorized) ? @"YES" : @"NO",
[OIDTokenUtilities redact:_refreshToken],
_scope,
[OIDTokenUtilities redact:self.accessToken],
self.accessTokenExpirationDate,
[OIDTokenUtilities redact:self.idToken],
_lastAuthorizationResponse,
_lastTokenResponse,
_lastRegistrationResponse,
_authorizationError];
}
#pragma mark - NSSecureCoding
+ (BOOL)supportsSecureCoding {
return YES;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
_lastAuthorizationResponse = [aDecoder decodeObjectOfClass:[OIDAuthorizationResponse class]
forKey:kLastAuthorizationResponseKey];
_lastTokenResponse = [aDecoder decodeObjectOfClass:[OIDTokenResponse class]
forKey:kLastTokenResponseKey];
self = [self initWithAuthorizationResponse:_lastAuthorizationResponse
tokenResponse:_lastTokenResponse];
if (self) {
_authorizationError =
[aDecoder decodeObjectOfClass:[NSError class] forKey:kAuthorizationErrorKey];
_scope = [aDecoder decodeObjectOfClass:[NSString class] forKey:kScopeKey];
_refreshToken = [aDecoder decodeObjectOfClass:[NSString class] forKey:kRefreshTokenKey];
_needsTokenRefresh = [aDecoder decodeBoolForKey:kNeedsTokenRefreshKey];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:_lastAuthorizationResponse forKey:kLastAuthorizationResponseKey];
[aCoder encodeObject:_lastTokenResponse forKey:kLastTokenResponseKey];
if (_authorizationError) {
NSError *codingSafeAuthorizationError = [NSError errorWithDomain:_authorizationError.domain
code:_authorizationError.code
userInfo:nil];
[aCoder encodeObject:codingSafeAuthorizationError forKey:kAuthorizationErrorKey];
}
[aCoder encodeObject:_scope forKey:kScopeKey];
[aCoder encodeObject:_refreshToken forKey:kRefreshTokenKey];
[aCoder encodeBool:_needsTokenRefresh forKey:kNeedsTokenRefreshKey];
}
#pragma mark - Private convenience getters
- (NSString *)accessToken {
if (_authorizationError) {
return nil;
}
return _lastTokenResponse ? _lastTokenResponse.accessToken
: _lastAuthorizationResponse.accessToken;
}
- (NSString *)tokenType {
if (_authorizationError) {
return nil;
}
return _lastTokenResponse ? _lastTokenResponse.tokenType
: _lastAuthorizationResponse.tokenType;
}
- (NSDate *)accessTokenExpirationDate {
if (_authorizationError) {
return nil;
}
return _lastTokenResponse ? _lastTokenResponse.accessTokenExpirationDate
: _lastAuthorizationResponse.accessTokenExpirationDate;
}
- (NSString *)idToken {
if (_authorizationError) {
return nil;
}
return _lastTokenResponse ? _lastTokenResponse.idToken
: _lastAuthorizationResponse.idToken;
}
#pragma mark - Getters
- (BOOL)isAuthorized {
return !self.authorizationError && (self.accessToken || self.idToken || self.refreshToken);
}
#pragma mark - Updating the state
- (void)updateWithRegistrationResponse:(OIDRegistrationResponse *)registrationResponse {
_lastRegistrationResponse = registrationResponse;
_refreshToken = nil;
_scope = nil;
_lastAuthorizationResponse = nil;
_lastTokenResponse = nil;
_authorizationError = nil;
[self didChangeState];
}
- (void)updateWithAuthorizationResponse:(nullable OIDAuthorizationResponse *)authorizationResponse
error:(nullable NSError *)error {
// If the error is an OAuth authorization error, updates the state. Other errors are ignored.
if (error.domain == OIDOAuthAuthorizationErrorDomain) {
[self updateWithAuthorizationError:error];
return;
}
if (!authorizationResponse) {
return;
}
_lastAuthorizationResponse = authorizationResponse;
// clears the last token response and refresh token as these now relate to an old authorization
// that is no longer relevant
_lastTokenResponse = nil;
_refreshToken = nil;
_authorizationError = nil;
// if the response's scope is nil, it means that it equals that of the request
// see: https://tools.ietf.org/html/rfc6749#section-5.1
_scope = (authorizationResponse.scope) ? authorizationResponse.scope
: authorizationResponse.request.scope;
[self didChangeState];
}
- (void)updateWithTokenResponse:(nullable OIDTokenResponse *)tokenResponse
error:(nullable NSError *)error {
if (_authorizationError) {
// Calling updateWithTokenResponse while in an error state probably means the developer obtained
// a new token and did the exchange without also calling updateWithAuthorizationResponse.
// Attempts to handle gracefully, but warns the developer that this is unexpected.
NSLog(@"OIDAuthState:updateWithTokenResponse should not be called in an error state [%@] call"
"updateWithAuthorizationResponse with the result of the fresh authorization response"
"first",
_authorizationError);
_authorizationError = nil;
}
// If the error is an OAuth authorization error, updates the state. Other errors are ignored.
if (error.domain == OIDOAuthTokenErrorDomain) {
[self updateWithAuthorizationError:error];
return;
}
if (!tokenResponse) {
return;
}
_lastTokenResponse = tokenResponse;
// updates the scope and refresh token if they are present on the TokenResponse.
// according to the spec, these may be changed by the server, including when refreshing the
// access token. See: https://tools.ietf.org/html/rfc6749#section-5.1 and
// https://tools.ietf.org/html/rfc6749#section-6
if (tokenResponse.scope) {
_scope = tokenResponse.scope;
}
if (tokenResponse.refreshToken) {
_refreshToken = tokenResponse.refreshToken;
}
[self didChangeState];
}
- (void)updateWithAuthorizationError:(NSError *)oauthError {
_authorizationError = oauthError;
[self didChangeState];
[_errorDelegate authState:self didEncounterAuthorizationError:oauthError];
}
#pragma mark - OAuth Requests
- (OIDTokenRequest *)tokenRefreshRequest {
return [self tokenRefreshRequestWithAdditionalParameters:nil];
}
- (OIDTokenRequest *)tokenRefreshRequestWithAdditionalParameters:
(NSDictionary<NSString *, NSString *> *)additionalParameters {
if (!_refreshToken) {
[OIDErrorUtilities raiseException:kRefreshTokenRequestException];
}
return [[OIDTokenRequest alloc]
initWithConfiguration:_lastAuthorizationResponse.request.configuration
grantType:OIDGrantTypeRefreshToken
authorizationCode:nil
redirectURL:nil
clientID:_lastAuthorizationResponse.request.clientID
clientSecret:_lastAuthorizationResponse.request.clientSecret
scope:nil
refreshToken:_refreshToken
codeVerifier:nil
additionalParameters:additionalParameters
additionalHeaders:nil];
}
- (OIDTokenRequest *)tokenRefreshRequestWithAdditionalParameters:
(NSDictionary<NSString *, NSString *> *)additionalParameters
additionalHeaders:
(NSDictionary<NSString *,NSString *> *)additionalHeaders {
if (!_refreshToken) {
[OIDErrorUtilities raiseException:kRefreshTokenRequestException];
}
return [[OIDTokenRequest alloc]
initWithConfiguration:_lastAuthorizationResponse.request.configuration
grantType:OIDGrantTypeRefreshToken
authorizationCode:nil
redirectURL:nil
clientID:_lastAuthorizationResponse.request.clientID
clientSecret:_lastAuthorizationResponse.request.clientSecret
scope:nil
refreshToken:_refreshToken
codeVerifier:nil
additionalParameters:additionalParameters
additionalHeaders:additionalHeaders];
}
- (OIDTokenRequest *)tokenRefreshRequestWithAdditionalHeaders:
(NSDictionary<NSString *, NSString *> *)additionalHeaders {
if (!_refreshToken) {
[OIDErrorUtilities raiseException:kRefreshTokenRequestException];
}
return [[OIDTokenRequest alloc]
initWithConfiguration:_lastAuthorizationResponse.request.configuration
grantType:OIDGrantTypeRefreshToken
authorizationCode:nil
redirectURL:nil
clientID:_lastAuthorizationResponse.request.clientID
clientSecret:_lastAuthorizationResponse.request.clientSecret
scope:nil
refreshToken:_refreshToken
codeVerifier:nil
additionalParameters:nil
additionalHeaders:additionalHeaders];
}
#pragma mark - Stateful Actions
- (void)didChangeState {
[_stateChangeDelegate didChangeState:self];
}
- (void)setNeedsTokenRefresh {
_needsTokenRefresh = YES;
}
+ (void)setSharedDelegateQueue:(dispatch_queue_t)queue {
_sharedDelegateQueue = queue;
}
+ (dispatch_queue_t)sharedDelegateQueue {
return _sharedDelegateQueue ?: dispatch_get_main_queue();
}
- (void)performActionWithFreshTokens:(OIDAuthStateAction)action {
[self performActionWithFreshTokens:action additionalRefreshParameters:nil];
}
- (void)performActionWithFreshTokens:(OIDAuthStateAction)action
additionalRefreshParameters:
(nullable NSDictionary<NSString *, NSString *> *)additionalParameters {
[self performActionWithFreshTokens:action
additionalRefreshParameters:additionalParameters
dispatchQueue:dispatch_get_main_queue()];
}
- (void)performActionWithFreshTokens:(OIDAuthStateAction)action
additionalRefreshParameters:
(nullable NSDictionary<NSString *, NSString *> *)additionalParameters
dispatchQueue:(dispatch_queue_t)dispatchQueue {
if ([self isTokenFresh]) {
// access token is valid within tolerance levels, perform action
dispatch_async(dispatchQueue, ^{
action(self.accessToken, self.idToken, nil);
});
return;
}
if (!_refreshToken) {
// no refresh token available and token has expired
NSError *tokenRefreshError = [
OIDErrorUtilities errorWithCode:OIDErrorCodeTokenRefreshError
underlyingError:nil
description:@"Unable to refresh expired token without a refresh token."];
dispatch_async(dispatchQueue, ^{
action(nil, nil, tokenRefreshError);
});
return;
}
// access token is expired, first refresh the token, then perform action
NSAssert(_pendingActionsSyncObject, @"_pendingActionsSyncObject cannot be nil", @"");
OIDAuthStatePendingAction* pendingAction =
[[OIDAuthStatePendingAction alloc] initWithAction:action andDispatchQueue:dispatchQueue];
@synchronized(_pendingActionsSyncObject) {
// if a token is already in the process of being refreshed, adds to pending actions
if (_pendingActions) {
[_pendingActions addObject:pendingAction];
return;
}
// creates a list of pending actions, starting with this one
_pendingActions = [NSMutableArray arrayWithObject:pendingAction];
}
// refresh the tokens
OIDTokenRequest *tokenRefreshRequest =
[self tokenRefreshRequestWithAdditionalParameters:additionalParameters];
[OIDAuthorizationService performTokenRequest:tokenRefreshRequest
originalAuthorizationResponse:_lastAuthorizationResponse
callbackDispatchQueue:[OIDAuthState sharedDelegateQueue]
callback:^(OIDTokenResponse *_Nullable response,
NSError *_Nullable error) {
// update OIDAuthState based on response
if (response) {
self->_needsTokenRefresh = NO;
[self updateWithTokenResponse:response error:nil];
} else {
if (error.domain == OIDOAuthTokenErrorDomain) {
self->_needsTokenRefresh = NO;
[self updateWithAuthorizationError:error];
} else {
if ([self->_errorDelegate respondsToSelector:
@selector(authState:didEncounterTransientError:)]) {
[self->_errorDelegate authState:self didEncounterTransientError:error];
}
}
}
// nil the pending queue and process everything that was queued up
NSArray *actionsToProcess;
@synchronized(self->_pendingActionsSyncObject) {
actionsToProcess = self->_pendingActions;
self->_pendingActions = nil;
}
for (OIDAuthStatePendingAction* actionToProcess in actionsToProcess) {
dispatch_async(actionToProcess.dispatchQueue, ^{
actionToProcess.action(self.accessToken, self.idToken, error);
});
}
}];
}
#pragma mark -
/*! @fn isTokenFresh
@brief Determines whether a token refresh request must be made to refresh the tokens.
*/
- (BOOL)isTokenFresh {
if (_needsTokenRefresh) {
// forced refresh
return NO;
}
if (!self.accessTokenExpirationDate) {
// if there is no expiration time but we have an access token, it is assumed to never expire
return !!self.accessToken;
}
// has the token expired?
BOOL tokenFresh = [self.accessTokenExpirationDate timeIntervalSinceNow] > kExpiryTimeTolerance;
return tokenFresh;
}
@end