GIDAuthorizationFlowProcessor.m 4.6 KB

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