|
| 1 | +// |
| 2 | +// Copyright (c) Microsoft Corporation. |
| 3 | +// All rights reserved. |
| 4 | +// |
| 5 | +// This code is licensed under the MIT License. |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +// of this software and associated documentation files(the "Software"), to deal |
| 9 | +// in the Software without restriction, including without limitation the rights |
| 10 | +// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell |
| 11 | +// copies of the Software, and to permit persons to whom the Software is |
| 12 | +// furnished to do so, subject to the following conditions : |
| 13 | +// |
| 14 | +// The above copyright notice and this permission notice shall be included in |
| 15 | +// all copies or substantial portions of the Software. |
| 16 | +// |
| 17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | +// THE SOFTWARE. |
| 24 | + |
| 25 | +#import "MSIDNonceTokenRequest.h" |
| 26 | +#import "MSIDRequestParameters.h" |
| 27 | +#import "MSIDAuthority.h" |
| 28 | +#import "MSIDOpenIdProviderMetadata.h" |
| 29 | +#import "MSIDAccountIdentifier.h" |
| 30 | +#import "MSIDNonceHttpRequest.h" |
| 31 | +#import "MSIDCachedNonce.h" |
| 32 | + |
| 33 | +const static NSUInteger kMSIDNonceLifetimeInSeconds = 180; |
| 34 | +@implementation MSIDNonceTokenRequest |
| 35 | + |
| 36 | +- (nullable instancetype)initWithRequestParameters:(nonnull MSIDRequestParameters *)parameters |
| 37 | +{ |
| 38 | + self = [super init]; |
| 39 | + if (self) |
| 40 | + { |
| 41 | + _requestParameters = parameters; |
| 42 | + } |
| 43 | + return self; |
| 44 | +} |
| 45 | + |
| 46 | +- (void)executeRequestWithCompletion:(nonnull MSIDNonceRequestCompletion)completionBlock |
| 47 | +{ |
| 48 | + MSIDCachedNonce *cachedNonce = [self.class getCachedNonceForKey:self.requestParameters.authority.environment]; |
| 49 | + if (cachedNonce) |
| 50 | + { |
| 51 | + completionBlock(cachedNonce.nonce, nil); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + if (self.requestParameters.authority.metadata.tokenEndpoint) |
| 56 | + { |
| 57 | + [self executeNetworkRequestWithCompletion:completionBlock]; |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + [self.requestParameters.authority resolveAndValidate:YES |
| 62 | + userPrincipalName:_requestParameters.accountIdentifier.displayableId |
| 63 | + context:_requestParameters |
| 64 | + completionBlock:^(NSURL __unused *openIdConfigurationEndpoint, BOOL __unused validated, NSError *error) |
| 65 | + { |
| 66 | + if (error) |
| 67 | + { |
| 68 | + completionBlock(nil, error); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + [self.requestParameters.authority loadOpenIdMetadataWithContext:self.requestParameters |
| 73 | + completionBlock:^(__unused MSIDOpenIdProviderMetadata *metadata, NSError *openIdError) |
| 74 | + { |
| 75 | + |
| 76 | + if (openIdError) |
| 77 | + { |
| 78 | + completionBlock(nil, openIdError); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + [self executeNetworkRequestWithCompletion:completionBlock]; |
| 83 | + }]; |
| 84 | + }]; |
| 85 | +} |
| 86 | + |
| 87 | +- (void)executeNetworkRequestWithCompletion:(nonnull MSIDNonceRequestCompletion)completionBlock |
| 88 | +{ |
| 89 | + MSIDNonceHttpRequest *nonceRequest = [[MSIDNonceHttpRequest alloc] initWithTokenEndpoint:self.requestParameters.tokenEndpoint |
| 90 | + context:self.requestParameters]; |
| 91 | + [nonceRequest sendWithBlock:^(NSDictionary *response, NSError *error) |
| 92 | + { |
| 93 | + if (error) |
| 94 | + { |
| 95 | + if (completionBlock) completionBlock(nil, error); |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + if (![response isKindOfClass:[NSDictionary class]]) |
| 100 | + { |
| 101 | + MSID_LOG_WITH_CTX(MSIDLogLevelError, self.requestParameters, @"Unexpected nonce response received"); |
| 102 | + NSError *nwError = MSIDCreateError(MSIDErrorDomain, MSIDErrorServerInvalidResponse, @"Unexpected nonce response", nil, nil, nil, nil, nil, YES); |
| 103 | + if (completionBlock) completionBlock(nil, nwError); |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + NSString *nonce = [response msidStringObjectForKey:@"Nonce"]; |
| 108 | + |
| 109 | + if ([NSString msidIsStringNilOrBlank:nonce]) |
| 110 | + { |
| 111 | + MSID_LOG_WITH_CTX(MSIDLogLevelError, self.requestParameters, @"Didn't receive valid nonce in response"); |
| 112 | + NSError *nwError = MSIDCreateError(MSIDErrorDomain, MSIDErrorServerInvalidResponse, @"Didn't receive valid nonce in response", nil, nil, nil, nil, nil, YES); |
| 113 | + if (completionBlock) completionBlock(nil, nwError); |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + [self.class cacheNonceForKey:self.requestParameters.authority.environment nonce:nonce]; |
| 118 | + if (completionBlock) |
| 119 | + { |
| 120 | + completionBlock(nonce, nil); |
| 121 | + } |
| 122 | + }]; |
| 123 | +} |
| 124 | + |
| 125 | +#pragma mark - Cache |
| 126 | + |
| 127 | ++ (MSIDCache *)nonceCache |
| 128 | +{ |
| 129 | + static MSIDCache *k_nonceCache; |
| 130 | + static dispatch_once_t once_token; |
| 131 | + dispatch_once(&once_token, ^{ |
| 132 | + k_nonceCache = [MSIDCache new]; |
| 133 | + }); |
| 134 | + |
| 135 | + return k_nonceCache; |
| 136 | +} |
| 137 | + |
| 138 | ++ (nullable MSIDCachedNonce *)getCachedNonceForKey:(NSString *)key |
| 139 | +{ |
| 140 | + MSIDCache *cache = [self.class nonceCache]; |
| 141 | + MSIDCachedNonce *cachedNonce = [cache objectForKey:key]; |
| 142 | + if (cachedNonce) |
| 143 | + { |
| 144 | + NSTimeInterval ti = [[NSDate date] timeIntervalSinceDate:cachedNonce.cachedDate]; |
| 145 | + if (ti > 0 && ti < kMSIDNonceLifetimeInSeconds) |
| 146 | + { |
| 147 | + return cachedNonce; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + return nil; |
| 152 | +} |
| 153 | + |
| 154 | ++ (BOOL)cacheNonceForKey:(NSString *)key nonce:(NSString *)nonce |
| 155 | +{ |
| 156 | + if (!nonce || !key) |
| 157 | + { |
| 158 | + return NO; |
| 159 | + } |
| 160 | + |
| 161 | + MSIDCachedNonce *cachedNonce = [[MSIDCachedNonce alloc] initWithNonce:nonce]; |
| 162 | + [self.class.nonceCache setObject:cachedNonce forKey:key]; |
| 163 | + return YES; |
| 164 | +} |
| 165 | +@end |
0 commit comments