GIDAuthorizationUtil.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 *)
  30. authorizationRequestWithOptions:(GIDSignInInternalOptions *)options
  31. emmSupport:(nullable NSString *)emmSupport {
  32. GIDSignInCallbackSchemes *schemes =
  33. [[GIDSignInCallbackSchemes alloc] initWithClientIdentifier:options.configuration.clientID];
  34. NSString *urlString = [NSString stringWithFormat:@"%@:%@",
  35. [schemes clientIdentifierScheme], kBrowserCallbackPath];
  36. NSURL *redirectURL = [NSURL URLWithString:urlString];
  37. NSMutableDictionary<NSString *, NSString *> *additionalParameters = [@{} mutableCopy];
  38. additionalParameters[kIncludeGrantedScopesParameter] = @"true";
  39. if (options.configuration.serverClientID) {
  40. additionalParameters[kAudienceParameter] = options.configuration.serverClientID;
  41. }
  42. if (options.loginHint) {
  43. additionalParameters[kLoginHintParameter] = options.loginHint;
  44. }
  45. if (options.configuration.hostedDomain) {
  46. additionalParameters[kHostedDomainParameter] = options.configuration.hostedDomain;
  47. }
  48. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  49. [additionalParameters addEntriesFromDictionary:
  50. [GIDEMMSupport parametersWithParameters:options.extraParams
  51. emmSupport:emmSupport
  52. isPasscodeInfoRequired:NO]];
  53. #elif TARGET_OS_OSX || TARGET_OS_MACCATALYST
  54. [additionalParameters addEntriesFromDictionary:options.extraParams];
  55. #endif // TARGET_OS_OSX || TARGET_OS_MACCATALYST
  56. additionalParameters[kSDKVersionLoggingParameter] = GIDVersion();
  57. additionalParameters[kEnvironmentLoggingParameter] = GIDEnvironment();
  58. NSURL *authorizationEndpointURL = [GIDSignInPreferences authorizationEndpointURL];
  59. NSURL *tokenEndpointURL = [GIDSignInPreferences tokenEndpointURL];
  60. OIDServiceConfiguration *appAuthConfiguration =
  61. [[OIDServiceConfiguration alloc] initWithAuthorizationEndpoint:authorizationEndpointURL
  62. tokenEndpoint:tokenEndpointURL];
  63. OIDAuthorizationRequest *request =
  64. [[OIDAuthorizationRequest alloc] initWithConfiguration:appAuthConfiguration
  65. clientId:options.configuration.clientID
  66. scopes:options.scopes
  67. redirectURL:redirectURL
  68. responseType:OIDResponseTypeCode
  69. additionalParameters:additionalParameters];
  70. return request;
  71. }
  72. + (nullable NSArray<NSString *> *)
  73. resolvedScopesFromGrantedScoped:(NSArray<NSString *> *)scopes
  74. withNewScopes:(NSArray<NSString *> *)newScopes
  75. error:(NSError * __autoreleasing *)error {
  76. NSMutableSet<NSString *> *grantedScopes = [NSMutableSet setWithArray:scopes];
  77. NSSet<NSString *> *requestedScopes = [NSSet setWithArray:newScopes];
  78. if ([requestedScopes isSubsetOfSet:grantedScopes]) {
  79. // All requested scopes have already been granted, generate an error.
  80. *error = [NSError errorWithDomain:kGIDSignInErrorDomain
  81. code:kGIDSignInErrorCodeScopesAlreadyGranted
  82. userInfo:nil];
  83. return nil;
  84. }
  85. // Use the union of granted and requested scopes.
  86. [grantedScopes unionSet:requestedScopes];
  87. return [grantedScopes allObjects];
  88. }
  89. @end
  90. NS_ASSUME_NONNULL_END