GIDAuthorizationFlowProcessor.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright 2023 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/GIDAuthorizationFlowProcessor/Implementations/GIDAuthorizationFlowProcessor.h"
  17. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDConfiguration.h"
  18. #import "GoogleSignIn/Sources/GIDEMMSupport.h"
  19. #import "GoogleSignIn/Sources/GIDSignInCallbackSchemes.h"
  20. #import "GoogleSignIn/Sources/GIDSignInInternalOptions.h"
  21. #import "GoogleSignIn/Sources/GIDSignInPreferences.h"
  22. #ifdef SWIFT_PACKAGE
  23. @import AppAuth;
  24. #else
  25. #import <AppAuth/AppAuth.h>
  26. #endif
  27. NS_ASSUME_NONNULL_BEGIN
  28. // Parameters for the auth and token exchange endpoints.
  29. static NSString *const kAudienceParameter = @"audience";
  30. static NSString *const kIncludeGrantedScopesParameter = @"include_granted_scopes";
  31. static NSString *const kLoginHintParameter = @"login_hint";
  32. static NSString *const kHostedDomainParameter = @"hd";
  33. @interface GIDAuthorizationFlowProcessor ()
  34. /// AppAuth external user-agent session state.
  35. @property(nonatomic, nullable)id<OIDExternalUserAgentSession> currentAuthorizationFlow;
  36. /// AppAuth configuration object.
  37. @property(nonatomic)OIDServiceConfiguration *appAuthConfiguration;
  38. @end
  39. @implementation GIDAuthorizationFlowProcessor
  40. # pragma mark - Public API
  41. - (BOOL)isStarted {
  42. return self.currentAuthorizationFlow != nil;
  43. }
  44. - (void)startWithOptions:(GIDSignInInternalOptions *)options
  45. emmSupport:(nullable NSString *)emmSupport
  46. completion:(void (^)(OIDAuthorizationResponse *_Nullable authorizationResponse,
  47. NSError *_Nullable error))completion {
  48. GIDSignInCallbackSchemes *schemes =
  49. [[GIDSignInCallbackSchemes alloc] initWithClientIdentifier:options.configuration.clientID];
  50. NSString *urlString = [NSString stringWithFormat:@"%@:%@",
  51. [schemes clientIdentifierScheme], kBrowserCallbackPath];
  52. NSURL *redirectURL = [NSURL URLWithString:urlString];
  53. NSMutableDictionary<NSString *, NSString *> *additionalParameters = [@{} mutableCopy];
  54. additionalParameters[kIncludeGrantedScopesParameter] = @"true";
  55. if (options.configuration.serverClientID) {
  56. additionalParameters[kAudienceParameter] = options.configuration.serverClientID;
  57. }
  58. if (options.loginHint) {
  59. additionalParameters[kLoginHintParameter] = options.loginHint;
  60. }
  61. if (options.configuration.hostedDomain) {
  62. additionalParameters[kHostedDomainParameter] = options.configuration.hostedDomain;
  63. }
  64. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  65. [additionalParameters addEntriesFromDictionary:
  66. [GIDEMMSupport parametersWithParameters:options.extraParams
  67. emmSupport:emmSupport
  68. isPasscodeInfoRequired:NO]];
  69. #elif TARGET_OS_OSX || TARGET_OS_MACCATALYST
  70. [additionalParameters addEntriesFromDictionary:options.extraParams];
  71. #endif // TARGET_OS_OSX || TARGET_OS_MACCATALYST
  72. additionalParameters[kSDKVersionLoggingParameter] = GIDVersion();
  73. additionalParameters[kEnvironmentLoggingParameter] = GIDEnvironment();
  74. NSURL *authorizationEndpointURL = [GIDSignInPreferences authorizationEndpointURL];
  75. NSURL *tokenEndpointURL = [GIDSignInPreferences tokenEndpointURL];
  76. OIDServiceConfiguration *appAuthConfiguration =
  77. [[OIDServiceConfiguration alloc] initWithAuthorizationEndpoint:authorizationEndpointURL
  78. tokenEndpoint:tokenEndpointURL];
  79. OIDAuthorizationRequest *request =
  80. [[OIDAuthorizationRequest alloc] initWithConfiguration:appAuthConfiguration
  81. clientId:options.configuration.clientID
  82. scopes:options.scopes
  83. redirectURL:redirectURL
  84. responseType:OIDResponseTypeCode
  85. additionalParameters:additionalParameters];
  86. _currentAuthorizationFlow = [OIDAuthorizationService
  87. presentAuthorizationRequest:request
  88. #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
  89. presentingViewController:options.presentingViewController
  90. #elif TARGET_OS_OSX
  91. presentingWindow:options.presentingWindow
  92. #endif // TARGET_OS_OSX
  93. callback:^(OIDAuthorizationResponse *authorizationResponse,
  94. NSError *error) {
  95. completion(authorizationResponse, error);
  96. }];
  97. }
  98. - (BOOL)resumeExternalUserAgentFlowWithURL:(NSURL *)url {
  99. if ([self.currentAuthorizationFlow resumeExternalUserAgentFlowWithURL:url]) {
  100. self.currentAuthorizationFlow = nil;
  101. return YES;
  102. } else {
  103. return NO;
  104. }
  105. }
  106. - (void)cancelAuthenticationFlow {
  107. [self.currentAuthorizationFlow cancel];
  108. self.currentAuthorizationFlow = nil;
  109. }
  110. @end
  111. NS_ASSUME_NONNULL_END