GIDAuthorizationFlowProcessor.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. @implementation GIDAuthorizationFlowProcessor {
  34. // AppAuth external user-agent session state.
  35. id<OIDExternalUserAgentSession> _currentAuthorizationFlow;
  36. // AppAuth configuration object.
  37. OIDServiceConfiguration *_appAuthConfiguration;
  38. }
  39. # pragma mark - Public API
  40. - (BOOL)isStarted {
  41. return _currentAuthorizationFlow != nil;
  42. }
  43. - (void)startWithOptions:(GIDSignInInternalOptions *)options
  44. emmSupport:(nullable NSString *)emmSupport
  45. completion:(void (^)(OIDAuthorizationResponse *_Nullable authorizationResponse,
  46. NSError *_Nullable error))completion {
  47. GIDSignInCallbackSchemes *schemes =
  48. [[GIDSignInCallbackSchemes alloc] initWithClientIdentifier:options.configuration.clientID];
  49. NSString *urlString = [NSString stringWithFormat:@"%@:%@",
  50. [schemes clientIdentifierScheme], kBrowserCallbackPath];
  51. NSURL *redirectURL = [NSURL URLWithString:urlString];
  52. NSMutableDictionary<NSString *, NSString *> *additionalParameters = [@{} mutableCopy];
  53. additionalParameters[kIncludeGrantedScopesParameter] = @"true";
  54. if (options.configuration.serverClientID) {
  55. additionalParameters[kAudienceParameter] = options.configuration.serverClientID;
  56. }
  57. if (options.loginHint) {
  58. additionalParameters[kLoginHintParameter] = options.loginHint;
  59. }
  60. if (options.configuration.hostedDomain) {
  61. additionalParameters[kHostedDomainParameter] = options.configuration.hostedDomain;
  62. }
  63. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  64. [additionalParameters addEntriesFromDictionary:
  65. [GIDEMMSupport parametersWithParameters:options.extraParams
  66. emmSupport:emmSupport
  67. isPasscodeInfoRequired:NO]];
  68. #elif TARGET_OS_OSX || TARGET_OS_MACCATALYST
  69. [additionalParameters addEntriesFromDictionary:options.extraParams];
  70. #endif // TARGET_OS_OSX || TARGET_OS_MACCATALYST
  71. additionalParameters[kSDKVersionLoggingParameter] = GIDVersion();
  72. additionalParameters[kEnvironmentLoggingParameter] = GIDEnvironment();
  73. NSURL *authorizationEndpointURL = [GIDSignInPreferences authorizationEndpointURL];
  74. NSURL *tokenEndpointURL = [GIDSignInPreferences tokenEndpointURL];
  75. OIDServiceConfiguration *appAuthConfiguration =
  76. [[OIDServiceConfiguration alloc] initWithAuthorizationEndpoint:authorizationEndpointURL
  77. tokenEndpoint:tokenEndpointURL];
  78. OIDAuthorizationRequest *request =
  79. [[OIDAuthorizationRequest alloc] initWithConfiguration:appAuthConfiguration
  80. clientId:options.configuration.clientID
  81. scopes:options.scopes
  82. redirectURL:redirectURL
  83. responseType:OIDResponseTypeCode
  84. additionalParameters:additionalParameters];
  85. _currentAuthorizationFlow = [OIDAuthorizationService
  86. presentAuthorizationRequest:request
  87. #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
  88. presentingViewController:options.presentingViewController
  89. #elif TARGET_OS_OSX
  90. presentingWindow:options.presentingWindow
  91. #endif // TARGET_OS_OSX
  92. callback:^(OIDAuthorizationResponse *authorizationResponse,
  93. NSError *error) {
  94. completion(authorizationResponse, error);
  95. }];
  96. }
  97. - (BOOL)resumeExternalUserAgentFlowWithURL:(NSURL *)url {
  98. if ([_currentAuthorizationFlow resumeExternalUserAgentFlowWithURL:url]) {
  99. _currentAuthorizationFlow = nil;
  100. return YES;
  101. } else {
  102. return NO;
  103. }
  104. }
  105. - (void)cancelAuthenticationFlow {
  106. [_currentAuthorizationFlow cancel];
  107. _currentAuthorizationFlow = nil;
  108. }
  109. @end
  110. NS_ASSUME_NONNULL_END