GIDAuthorizationUtil.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/GIDAuthorizationUtil.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. @implementation GIDAuthorizationUtil
  29. + (OIDAuthorizationRequest *)authorizationRequestWithOptions:(GIDSignInInternalOptions *)options
  30. emmSupport:(nullable NSString *)emmSupport {
  31. GIDSignInCallbackSchemes *schemes =
  32. [[GIDSignInCallbackSchemes alloc] initWithClientIdentifier:options.configuration.clientID];
  33. NSString *urlString = [NSString stringWithFormat:@"%@:%@",
  34. [schemes clientIdentifierScheme], kBrowserCallbackPath];
  35. NSURL *redirectURL = [NSURL URLWithString:urlString];
  36. NSMutableDictionary<NSString *, NSString *> *additionalParameters = [@{} mutableCopy];
  37. additionalParameters[kIncludeGrantedScopesParameter] = @"true";
  38. if (options.configuration.serverClientID) {
  39. additionalParameters[kAudienceParameter] = options.configuration.serverClientID;
  40. }
  41. if (options.loginHint) {
  42. additionalParameters[kLoginHintParameter] = options.loginHint;
  43. }
  44. if (options.configuration.hostedDomain) {
  45. additionalParameters[kHostedDomainParameter] = options.configuration.hostedDomain;
  46. }
  47. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  48. [additionalParameters addEntriesFromDictionary:
  49. [GIDEMMSupport parametersWithParameters:options.extraParams
  50. emmSupport:emmSupport
  51. isPasscodeInfoRequired:NO]];
  52. #elif TARGET_OS_OSX || TARGET_OS_MACCATALYST
  53. [additionalParameters addEntriesFromDictionary:options.extraParams];
  54. #endif // TARGET_OS_OSX || TARGET_OS_MACCATALYST
  55. additionalParameters[kSDKVersionLoggingParameter] = GIDVersion();
  56. additionalParameters[kEnvironmentLoggingParameter] = GIDEnvironment();
  57. NSURL *authorizationEndpointURL = [GIDSignInPreferences authorizationEndpointURL];
  58. NSURL *tokenEndpointURL = [GIDSignInPreferences tokenEndpointURL];
  59. OIDServiceConfiguration *appAuthConfiguration =
  60. [[OIDServiceConfiguration alloc] initWithAuthorizationEndpoint:authorizationEndpointURL
  61. tokenEndpoint:tokenEndpointURL];
  62. OIDAuthorizationRequest *request =
  63. [[OIDAuthorizationRequest alloc] initWithConfiguration:appAuthConfiguration
  64. clientId:options.configuration.clientID
  65. scopes:options.scopes
  66. redirectURL:redirectURL
  67. responseType:OIDResponseTypeCode
  68. additionalParameters:additionalParameters];
  69. return request;
  70. }
  71. + (OIDTokenRequest *)accessTokenRequestWithAuthState:(OIDAuthState *)authState
  72. serverClientID:(nullable NSString *)serverClientID
  73. openIDRealm:(nullable NSString *)openIDRealm
  74. emmSupport:(nullable NSString *)emmSupport {
  75. NSMutableDictionary<NSString *, NSString *> *additionalParameters = [@{} mutableCopy];
  76. if (serverClientID) {
  77. additionalParameters[kAudienceParameter] = serverClientID;
  78. }
  79. if (openIDRealm) {
  80. additionalParameters[kOpenIDRealmParameter] = openIDRealm;
  81. }
  82. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  83. NSDictionary<NSString *, NSObject *> *params =
  84. authState.lastAuthorizationResponse.additionalParameters;
  85. NSString *passcodeInfoRequired = (NSString *)params[kEMMPasscodeInfoRequiredKeyName];
  86. [additionalParameters addEntriesFromDictionary:
  87. [GIDEMMSupport parametersWithParameters:@{}
  88. emmSupport:emmSupport
  89. isPasscodeInfoRequired:passcodeInfoRequired.length > 0]];
  90. #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  91. additionalParameters[kSDKVersionLoggingParameter] = GIDVersion();
  92. additionalParameters[kEnvironmentLoggingParameter] = GIDEnvironment();
  93. OIDTokenRequest *tokenRequest;
  94. if (!authState.lastTokenResponse.accessToken &&
  95. authState.lastAuthorizationResponse.authorizationCode) {
  96. tokenRequest = [authState.lastAuthorizationResponse
  97. tokenExchangeRequestWithAdditionalParameters:additionalParameters];
  98. } else {
  99. [additionalParameters
  100. addEntriesFromDictionary:authState.lastTokenResponse.request.additionalParameters];
  101. tokenRequest = [authState tokenRefreshRequestWithAdditionalParameters:additionalParameters];
  102. }
  103. return tokenRequest;
  104. }
  105. + (nullable NSArray<NSString *> *)
  106. resolvedScopesFromGrantedScopes:(NSArray<NSString *> *)scopes
  107. withNewScopes:(NSArray<NSString *> *)newScopes
  108. error:(NSError * __autoreleasing *)error {
  109. NSMutableSet<NSString *> *grantedScopes = [NSMutableSet setWithArray:scopes];
  110. NSSet<NSString *> *requestedScopes = [NSSet setWithArray:newScopes];
  111. if ([requestedScopes isSubsetOfSet:grantedScopes]) {
  112. // All requested scopes have already been granted, generate an error.
  113. *error = [NSError errorWithDomain:kGIDSignInErrorDomain
  114. code:kGIDSignInErrorCodeScopesAlreadyGranted
  115. userInfo:nil];
  116. return nil;
  117. }
  118. // Use the union of granted and requested scopes.
  119. [grantedScopes unionSet:requestedScopes];
  120. return [grantedScopes allObjects];
  121. }
  122. @end
  123. NS_ASSUME_NONNULL_END