|
28 | 28 | #import "GoogleSignIn/Sources/GIDCallbackQueue.h" |
29 | 29 | #import "GoogleSignIn/Sources/GIDScopes.h" |
30 | 30 | #import "GoogleSignIn/Sources/GIDSignInCallbackSchemes.h" |
| 31 | +#import "GoogleSignIn/Sources/GIDTokenClaimsInternalOptions.h" |
31 | 32 | #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST |
32 | 33 | #import <AppCheckCore/GACAppCheckToken.h> |
33 | 34 | #import "GoogleSignIn/Sources/GIDAppCheck/Implementations/GIDAppCheck.h" |
|
136 | 137 | static NSString *const kLoginHintParameter = @"login_hint"; |
137 | 138 | static NSString *const kHostedDomainParameter = @"hd"; |
138 | 139 |
|
| 140 | +// Parameter for requesting the token claims. |
| 141 | +static NSString *const kTokenClaimsParameter = @"claims"; |
| 142 | + |
139 | 143 | // Parameters for auth and token exchange endpoints using App Attest. |
140 | 144 | static NSString *const kClientAssertionParameter = @"client_assertion"; |
141 | 145 | static NSString *const kClientAssertionTypeParameter = @"client_assertion_type"; |
@@ -169,6 +173,7 @@ @implementation GIDSignIn { |
169 | 173 | // set when a sign-in flow is begun via |signInWithOptions:| when the options passed don't |
170 | 174 | // represent a sign in continuation. |
171 | 175 | GIDSignInInternalOptions *_currentOptions; |
| 176 | + GIDTokenClaimsInternalOptions *_tokenClaimsInternalOptions; |
172 | 177 | #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST |
173 | 178 | GIDAppCheck *_appCheck API_AVAILABLE(ios(14)); |
174 | 179 | #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST |
@@ -284,14 +289,63 @@ - (void)signInWithPresentingViewController:(UIViewController *)presentingViewCon |
284 | 289 | additionalScopes:(nullable NSArray<NSString *> *)additionalScopes |
285 | 290 | nonce:(nullable NSString *)nonce |
286 | 291 | completion:(nullable GIDSignInCompletion)completion { |
| 292 | + [self signInWithPresentingViewController:presentingViewController |
| 293 | + hint:hint |
| 294 | + additionalScopes:additionalScopes |
| 295 | + nonce:nonce |
| 296 | + tokenClaims:nil |
| 297 | + completion:completion]; |
| 298 | +} |
| 299 | + |
| 300 | +- (void)signInWithPresentingViewController:(UIViewController *)presentingViewController |
| 301 | + tokenClaims:(nullable NSSet<GIDTokenClaim *> *)tokenClaims |
| 302 | + completion:(nullable GIDSignInCompletion)completion { |
| 303 | + [self signInWithPresentingViewController:presentingViewController |
| 304 | + hint:nil |
| 305 | + tokenClaims:tokenClaims |
| 306 | + completion:completion]; |
| 307 | +} |
| 308 | + |
| 309 | +- (void)signInWithPresentingViewController:(UIViewController *)presentingViewController |
| 310 | + hint:(nullable NSString *)hint |
| 311 | + tokenClaims:(nullable NSSet<GIDTokenClaim *> *)tokenClaims |
| 312 | + completion:(nullable GIDSignInCompletion)completion { |
| 313 | + [self signInWithPresentingViewController:presentingViewController |
| 314 | + hint:hint |
| 315 | + additionalScopes:@[] |
| 316 | + tokenClaims:tokenClaims |
| 317 | + completion:completion]; |
| 318 | +} |
| 319 | + |
| 320 | +- (void)signInWithPresentingViewController:(UIViewController *)presentingViewController |
| 321 | + hint:(nullable NSString *)hint |
| 322 | + additionalScopes:(nullable NSArray<NSString *> *)additionalScopes |
| 323 | + tokenClaims:(nullable NSSet<GIDTokenClaim *> *)tokenClaims |
| 324 | + completion:(nullable GIDSignInCompletion)completion { |
| 325 | + [self signInWithPresentingViewController:presentingViewController |
| 326 | + hint:hint |
| 327 | + additionalScopes:additionalScopes |
| 328 | + nonce:nil |
| 329 | + tokenClaims:tokenClaims |
| 330 | + completion:completion]; |
| 331 | +} |
| 332 | + |
| 333 | + |
| 334 | +- (void)signInWithPresentingViewController:(UIViewController *)presentingViewController |
| 335 | + hint:(nullable NSString *)hint |
| 336 | + additionalScopes:(nullable NSArray<NSString *> *)additionalScopes |
| 337 | + nonce:(nullable NSString *)nonce |
| 338 | + tokenClaims:(nullable NSSet<GIDTokenClaim *> *)tokenClaims |
| 339 | + completion:(nullable GIDSignInCompletion)completion { |
287 | 340 | GIDSignInInternalOptions *options = |
288 | | - [GIDSignInInternalOptions defaultOptionsWithConfiguration:_configuration |
289 | | - presentingViewController:presentingViewController |
290 | | - loginHint:hint |
291 | | - addScopesFlow:NO |
292 | | - scopes:additionalScopes |
293 | | - nonce:nonce |
294 | | - completion:completion]; |
| 341 | + [GIDSignInInternalOptions defaultOptionsWithConfiguration:_configuration |
| 342 | + presentingViewController:presentingViewController |
| 343 | + loginHint:hint |
| 344 | + addScopesFlow:NO |
| 345 | + scopes:additionalScopes |
| 346 | + nonce:nonce |
| 347 | + tokenClaims:tokenClaims |
| 348 | + completion:completion]; |
295 | 349 | [self signInWithOptions:options]; |
296 | 350 | } |
297 | 351 |
|
@@ -375,14 +429,62 @@ - (void)signInWithPresentingWindow:(NSWindow *)presentingWindow |
375 | 429 | additionalScopes:(nullable NSArray<NSString *> *)additionalScopes |
376 | 430 | nonce:(nullable NSString *)nonce |
377 | 431 | completion:(nullable GIDSignInCompletion)completion { |
| 432 | + [self signInWithPresentingWindow:presentingWindow |
| 433 | + hint:hint |
| 434 | + additionalScopes:additionalScopes |
| 435 | + nonce:nonce |
| 436 | + tokenClaims:nil |
| 437 | + completion:completion]; |
| 438 | +} |
| 439 | + |
| 440 | +- (void)signInWithPresentingWindow:(NSWindow *)presentingWindow |
| 441 | + tokenClaims:(nullable NSSet<GIDTokenClaim *> *)tokenClaims |
| 442 | + completion:(nullable GIDSignInCompletion)completion { |
| 443 | + [self signInWithPresentingWindow:presentingWindow |
| 444 | + hint:nil |
| 445 | + tokenClaims:tokenClaims |
| 446 | + completion:completion]; |
| 447 | +} |
| 448 | + |
| 449 | +- (void)signInWithPresentingWindow:(NSWindow *)presentingWindow |
| 450 | + hint:(nullable NSString *)hint |
| 451 | + tokenClaims:(nullable NSSet<GIDTokenClaim *> *)tokenClaims |
| 452 | + completion:(nullable GIDSignInCompletion)completion { |
| 453 | + [self signInWithPresentingWindow:presentingWindow |
| 454 | + hint:hint |
| 455 | + additionalScopes:@[] |
| 456 | + tokenClaims:tokenClaims |
| 457 | + completion:completion]; |
| 458 | +} |
| 459 | + |
| 460 | +- (void)signInWithPresentingWindow:(NSWindow *)presentingWindow |
| 461 | + hint:(nullable NSString *)hint |
| 462 | + additionalScopes:(nullable NSArray<NSString *> *)additionalScopes |
| 463 | + tokenClaims:(nullable NSSet<GIDTokenClaim *> *)tokenClaims |
| 464 | + completion:(nullable GIDSignInCompletion)completion { |
| 465 | + [self signInWithPresentingWindow:presentingWindow |
| 466 | + hint:hint |
| 467 | + additionalScopes:additionalScopes |
| 468 | + nonce:nil |
| 469 | + tokenClaims:tokenClaims |
| 470 | + completion:completion]; |
| 471 | +} |
| 472 | + |
| 473 | +- (void)signInWithPresentingWindow:(NSWindow *)presentingWindow |
| 474 | + hint:(nullable NSString *)hint |
| 475 | + additionalScopes:(nullable NSArray<NSString *> *)additionalScopes |
| 476 | + nonce:(nullable NSString *)nonce |
| 477 | + tokenClaims:(nullable NSSet<GIDTokenClaim *> *)tokenClaims |
| 478 | + completion:(nullable GIDSignInCompletion)completion { |
378 | 479 | GIDSignInInternalOptions *options = |
379 | | - [GIDSignInInternalOptions defaultOptionsWithConfiguration:_configuration |
380 | | - presentingWindow:presentingWindow |
381 | | - loginHint:hint |
382 | | - addScopesFlow:NO |
383 | | - scopes:additionalScopes |
384 | | - nonce:nonce |
385 | | - completion:completion]; |
| 480 | + [GIDSignInInternalOptions defaultOptionsWithConfiguration:_configuration |
| 481 | + presentingWindow:presentingWindow |
| 482 | + loginHint:hint |
| 483 | + addScopesFlow:NO |
| 484 | + scopes:additionalScopes |
| 485 | + nonce:nonce |
| 486 | + tokenClaims:tokenClaims |
| 487 | + completion:completion]; |
386 | 488 | [self signInWithOptions:options]; |
387 | 489 | } |
388 | 490 |
|
@@ -542,6 +644,7 @@ - (instancetype)initWithKeychainStore:(GTMKeychainStore *)keychainStore |
542 | 644 | self = [super init]; |
543 | 645 | if (self) { |
544 | 646 | _keychainStore = keychainStore; |
| 647 | + _tokenClaimsInternalOptions = [[GIDTokenClaimsInternalOptions alloc] init]; |
545 | 648 |
|
546 | 649 | // Get the bundle of the current executable. |
547 | 650 | NSBundle *bundle = NSBundle.mainBundle; |
@@ -636,6 +739,18 @@ - (void)signInWithOptions:(GIDSignInInternalOptions *)options { |
636 | 739 | } |
637 | 740 | }]; |
638 | 741 | } else { |
| 742 | + NSError *claimsError; |
| 743 | + |
| 744 | + // If tokenClaims are invalid or JSON serialization fails, return with an error. |
| 745 | + if (![self processTokenClaimsForOptions:options error:&claimsError]) { |
| 746 | + if (options.completion) { |
| 747 | + self->_currentOptions = nil; |
| 748 | + dispatch_async(dispatch_get_main_queue(), ^{ |
| 749 | + options.completion(nil, claimsError); |
| 750 | + }); |
| 751 | + } |
| 752 | + return; |
| 753 | + } |
639 | 754 | [self authenticateWithOptions:options]; |
640 | 755 | } |
641 | 756 | } |
@@ -765,6 +880,9 @@ - (void)authorizationRequestWithOptions:(GIDSignInInternalOptions *)options comp |
765 | 880 | if (options.configuration.hostedDomain) { |
766 | 881 | additionalParameters[kHostedDomainParameter] = options.configuration.hostedDomain; |
767 | 882 | } |
| 883 | + if (options.tokenClaimsAsJSON) { |
| 884 | + additionalParameters[kTokenClaimsParameter] = options.tokenClaimsAsJSON; |
| 885 | + } |
768 | 886 |
|
769 | 887 | #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST |
770 | 888 | [additionalParameters addEntriesFromDictionary: |
@@ -1149,6 +1267,24 @@ - (void)assertValidPresentingViewController { |
1149 | 1267 | } |
1150 | 1268 | } |
1151 | 1269 |
|
| 1270 | +- (BOOL)processTokenClaimsForOptions:(GIDSignInInternalOptions *)options |
| 1271 | + error:(NSError **)error { |
| 1272 | + if (!options.tokenClaims) { |
| 1273 | + return YES; // Success |
| 1274 | + } |
| 1275 | + |
| 1276 | + NSString *tokenClaimsAsJSON = |
| 1277 | + [_tokenClaimsInternalOptions validatedJSONStringForClaims:options.tokenClaims |
| 1278 | + error:error]; |
| 1279 | + |
| 1280 | + if (!tokenClaimsAsJSON) { |
| 1281 | + return NO; // Failure |
| 1282 | + } |
| 1283 | + |
| 1284 | + options.tokenClaimsAsJSON = tokenClaimsAsJSON; |
| 1285 | + return YES; // Success |
| 1286 | +} |
| 1287 | + |
1152 | 1288 | // Checks whether or not this is the first time the app runs. |
1153 | 1289 | - (BOOL)isFreshInstall { |
1154 | 1290 | NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; |
|
0 commit comments