|
37 | 37 | #import "OKTTokenResponse.h" |
38 | 38 | #import "OKTURLQueryComponent.h" |
39 | 39 | #import "OKTURLSessionProvider.h" |
| 40 | +#import "OKTDefaultTokenValidator.h" |
40 | 41 |
|
41 | 42 | /*! @brief Path appended to an OpenID Connect issuer for discovery |
42 | 43 | @see https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfig |
43 | 44 | */ |
44 | 45 | static NSString *const kOpenIDConfigurationWellKnownPath = @".well-known/openid-configuration"; |
45 | 46 |
|
46 | | -/*! @brief Max allowable iat (Issued At) time skew |
47 | | - @see https://openid.net/specs/openid-connect-core-1_0.html#IDTokenValidation |
48 | | - */ |
49 | | -static int const kOKTAuthorizationSessionIATMaxSkew = 600; |
50 | | - |
51 | 47 | NS_ASSUME_NONNULL_BEGIN |
52 | 48 |
|
53 | 49 | @interface OKTAuthorizationSession : NSObject<OKTExternalUserAgentSession> |
@@ -425,12 +421,17 @@ + (void)discoverServiceConfigurationForDiscoveryURL:(NSURL *)discoveryURL |
425 | 421 | + (void)performTokenRequest:(OKTTokenRequest *)request |
426 | 422 | delegate:(id<OktaNetworkRequestCustomizationDelegate> _Nullable)delegate |
427 | 423 | callback:(OKTTokenCallback)callback { |
428 | | - [[self class] performTokenRequest:request originalAuthorizationResponse:nil delegate:delegate callback:callback]; |
| 424 | + [[self class] performTokenRequest:request |
| 425 | + originalAuthorizationResponse:nil |
| 426 | + delegate:delegate |
| 427 | + validator:[[OKTDefaultTokenValidator alloc] init] |
| 428 | + callback:callback]; |
429 | 429 | } |
430 | 430 |
|
431 | 431 | + (void)performTokenRequest:(OKTTokenRequest *)request |
432 | 432 | originalAuthorizationResponse:(OKTAuthorizationResponse *_Nullable)authorizationResponse |
433 | 433 | delegate:(id<OktaNetworkRequestCustomizationDelegate> _Nullable)delegate |
| 434 | + validator:(id<OKTTokenValidator> _Nonnull)validator |
434 | 435 | callback:(OKTTokenCallback)callback { |
435 | 436 |
|
436 | 437 | NSURLRequest *URLRequest = [request URLRequest]; |
@@ -607,37 +608,32 @@ + (void)performTokenRequest:(OKTTokenRequest *)request |
607 | 608 |
|
608 | 609 | // OpenID Connect Core Section 3.1.3.7. rules #7 & #8 |
609 | 610 | // Not applicable. See rule #6. |
| 611 | + |
| 612 | + NSAssert(validator != nil, @"Validator parameter is missed. Default will be used."); |
| 613 | + id<OKTTokenValidator> tokenValidator = validator ?: [OKTDefaultTokenValidator new]; |
610 | 614 |
|
611 | | - // OpenID Connect Core Section 3.1.3.7. rule #9 |
612 | | - // Validates that the current time is before the expiry time. |
613 | | - NSTimeInterval expiresAtDifference = [idToken.expiresAt timeIntervalSinceNow]; |
614 | | - if (expiresAtDifference < 0) { |
| 615 | + if ([tokenValidator isDateExpired:idToken.expiresAt token:OKTTokenTypeId]) { |
615 | 616 | NSError *invalidIDToken = |
616 | | - [OKTErrorUtilities errorWithCode:OKTErrorCodeIDTokenFailedValidationError |
617 | | - underlyingError:nil |
618 | | - description:@"ID Token expired"]; |
| 617 | + [OKTErrorUtilities errorWithCode:OKTErrorCodeIDTokenFailedValidationError |
| 618 | + underlyingError:nil |
| 619 | + description:@"ID Token expired"]; |
619 | 620 | dispatch_async(dispatch_get_main_queue(), ^{ |
620 | 621 | callback(nil, invalidIDToken); |
621 | 622 | }); |
622 | 623 | return; |
623 | 624 | } |
624 | | - |
625 | | - // OpenID Connect Core Section 3.1.3.7. rule #10 |
626 | | - // Validates that the issued at time is not more than +/- 10 minutes on the current time. |
627 | | - NSTimeInterval issuedAtDifference = [idToken.issuedAt timeIntervalSinceNow]; |
628 | | - if (fabs(issuedAtDifference) > kOKTAuthorizationSessionIATMaxSkew) { |
629 | | - NSString *message = |
630 | | - [NSString stringWithFormat:@"Issued at time is more than %d seconds before or after " |
631 | | - "the current time", |
632 | | - kOKTAuthorizationSessionIATMaxSkew]; |
633 | | - NSError *invalidIDToken = |
| 625 | + |
| 626 | + if (![tokenValidator isIssuedAtDateValid:idToken.issuedAt token:OKTTokenTypeId]) { |
| 627 | + NSString *message = |
| 628 | + [NSString stringWithFormat:@"Issued at time is invalid corresponding to the current time"]; |
| 629 | + NSError *invalidIDToken = |
634 | 630 | [OKTErrorUtilities errorWithCode:OKTErrorCodeIDTokenFailedValidationError |
635 | 631 | underlyingError:nil |
636 | 632 | description:message]; |
637 | | - dispatch_async(dispatch_get_main_queue(), ^{ |
638 | | - callback(nil, invalidIDToken); |
639 | | - }); |
640 | | - return; |
| 633 | + dispatch_async(dispatch_get_main_queue(), ^{ |
| 634 | + callback(nil, invalidIDToken); |
| 635 | + }); |
| 636 | + return; |
641 | 637 | } |
642 | 638 |
|
643 | 639 | // Only relevant for the authorization_code response type |
|
0 commit comments