GIDVerifyAccountDetail.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright 2024 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDVerifyAccountDetail.h"
  17. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDConfiguration.h"
  18. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDVerifiableAccountDetail.h"
  19. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDVerifiedAccountDetailResult.h"
  20. #import "GoogleSignIn/Sources/GIDSignInInternalOptions.h"
  21. #import "GoogleSignIn/Sources/GIDSignInCallbackSchemes.h"
  22. #if TARGET_OS_IOS
  23. @implementation GIDVerifyAccountDetail
  24. - (instancetype)initWithConfig:(nullable GIDConfiguration *)configuration {
  25. self = [super init];
  26. if (self) {
  27. _configuration = configuration;
  28. }
  29. return self;
  30. }
  31. - (instancetype)init {
  32. GIDConfiguration *configuration;
  33. NSBundle *bundle = NSBundle.mainBundle;
  34. if (bundle) {
  35. configuration = [GIDConfiguration configurationFromBundle:bundle];
  36. }
  37. return [self initWithConfig:configuration];
  38. }
  39. #pragma mark - Public methods
  40. - (void)verifyAccountDetails:(NSArray<GIDVerifiableAccountDetail *> *)accountDetails
  41. presentingViewController:(UIViewController *)presentingViewController
  42. completion:(nullable void (^)(GIDVerifiedAccountDetailResult *_Nullable verifyResult,
  43. NSError *_Nullable error))completion {
  44. [self verifyAccountDetails:accountDetails
  45. presentingViewController:presentingViewController
  46. hint:nil
  47. completion:completion];
  48. }
  49. - (void)verifyAccountDetails:(NSArray<GIDVerifiableAccountDetail *> *)accountDetails
  50. presentingViewController:(UIViewController *)presentingViewController
  51. hint:(nullable NSString *)hint
  52. completion:(nullable void (^)(GIDVerifiedAccountDetailResult *_Nullable verifyResult,
  53. NSError *_Nullable error))completion {
  54. GIDSignInInternalOptions *options =
  55. [GIDSignInInternalOptions defaultOptionsWithConfiguration:_configuration
  56. presentingViewController:presentingViewController
  57. loginHint:hint
  58. addScopesFlow:YES
  59. accountDetailsToVerify:accountDetails
  60. verifyCompletion:completion];
  61. [self verifyAccountDetailsInteractivelyWithOptions:options];
  62. }
  63. #pragma mark - Authentication flow
  64. - (void)verifyAccountDetailsInteractivelyWithOptions:(GIDSignInInternalOptions *)options {
  65. if (!options.interactive) {
  66. return;
  67. }
  68. // Ensure that a configuration is set.
  69. if (!_configuration) {
  70. // NOLINTNEXTLINE(google-objc-avoid-throwing-exception)
  71. [NSException raise:NSInvalidArgumentException
  72. format:@"No active configuration. Make sure GIDClientID is set in Info.plist."];
  73. return;
  74. }
  75. [self assertValidCurrentUser];
  76. // Explicitly throw exception for missing client ID here. This must come before
  77. // scheme check because schemes rely on reverse client IDs.
  78. [self assertValidParameters:options];
  79. [self assertValidPresentingViewController:options];
  80. // If the application does not support the required URL schemes tell the developer so.
  81. GIDSignInCallbackSchemes *schemes =
  82. [[GIDSignInCallbackSchemes alloc] initWithClientIdentifier:options.configuration.clientID];
  83. NSArray<NSString *> *unsupportedSchemes = [schemes unsupportedSchemes];
  84. if (unsupportedSchemes.count != 0) {
  85. // NOLINTNEXTLINE(google-objc-avoid-throwing-exception)
  86. [NSException raise:NSInvalidArgumentException
  87. format:@"Your app is missing support for the following URL schemes: %@",
  88. [unsupportedSchemes componentsJoinedByString:@", "]];
  89. }
  90. // TODO(#397): Start the incremental authorization flow.
  91. }
  92. #pragma mark - Helpers
  93. // Assert that a current user exists.
  94. - (void)assertValidCurrentUser {
  95. if (!GIDSignIn.sharedInstance.currentUser) {
  96. // NOLINTNEXTLINE(google-objc-avoid-throwing-exception)
  97. [NSException raise:NSInvalidArgumentException
  98. format:@"|currentUser| must be set to verify."];
  99. }
  100. }
  101. // Asserts the parameters being valid.
  102. - (void)assertValidParameters:(GIDSignInInternalOptions *)options {
  103. if (![options.configuration.clientID length]) {
  104. // NOLINTNEXTLINE(google-objc-avoid-throwing-exception)
  105. [NSException raise:NSInvalidArgumentException
  106. format:@"You must specify |clientID| in |GIDConfiguration|"];
  107. }
  108. }
  109. // Assert that the presenting view controller has been set.
  110. - (void)assertValidPresentingViewController:(GIDSignInInternalOptions *)options {
  111. if (!options.presentingViewController) {
  112. // NOLINTNEXTLINE(google-objc-avoid-throwing-exception)
  113. [NSException raise:NSInvalidArgumentException
  114. format:@"|presentingViewController| must be set."];
  115. }
  116. }
  117. @end
  118. #endif // TARGET_OS_IOS