GIDSignInPreferences.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2021 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "GoogleSignIn/Sources/GIDSignInPreferences.h"
  15. NS_ASSUME_NONNULL_BEGIN
  16. /// Parameters for the auth and token exchange endpoints.
  17. NSString *const kAudienceParameter = @"audience";
  18. NSString *const kIncludeGrantedScopesParameter = @"include_granted_scopes";
  19. NSString *const kLoginHintParameter = @"login_hint";
  20. NSString *const kHostedDomainParameter = @"hd";
  21. NSString *const kSDKVersionLoggingParameter = @"gpsdk";
  22. NSString *const kEnvironmentLoggingParameter = @"gidenv";
  23. NSString *const kBrowserCallbackPath = @"/oauth2callback";
  24. static NSString *const kLSOServer = @"accounts.google.com";
  25. static NSString *const kTokenServer = @"oauth2.googleapis.com";
  26. static NSString *const kUserInfoServer = @"www.googleapis.com";
  27. // Supported Apple execution environments
  28. static NSString *const kAppleEnvironmentUnknown = @"unknown";
  29. static NSString *const kAppleEnvironmentIOS = @"ios";
  30. static NSString *const kAppleEnvironmentIOSSimulator = @"ios-sim";
  31. static NSString *const kAppleEnvironmentMacOS = @"macos";
  32. static NSString *const kAppleEnvironmentMacOSIOSOnMac = @"macos-ios";
  33. static NSString *const kAppleEnvironmentMacOSMacCatalyst = @"macos-cat";
  34. // The URL template for the authorization endpoint.
  35. static NSString *const kAuthorizationURLTemplate = @"https://%@/o/oauth2/v2/auth";
  36. // The URL template for the token endpoint.
  37. static NSString *const kTokenURLTemplate = @"https://%@/token";
  38. #ifndef GID_SDK_VERSION
  39. #error "GID_SDK_VERSION is not defined: add -DGID_SDK_VERSION=x.x.x to the build invocation."
  40. #endif
  41. // Because macro expansions aren't performed on a token following the # preprocessor operator, we
  42. // wrap STR_EXPAND(x) with the STR(x) to produce a quoted string representation of a macro.
  43. // https://www.guyrutenberg.com/2008/12/20/expanding-macros-into-string-constants-in-c/
  44. #define STR(x) STR_EXPAND(x)
  45. #define STR_EXPAND(x) #x
  46. // The prefixed sdk version string to differentiate gid version values used with the legacy gpsdk
  47. // logging key.
  48. NSString* GIDVersion(void) {
  49. return [NSString stringWithFormat:@"gid-%@", @STR(GID_SDK_VERSION)];
  50. }
  51. // Get the current Apple execution environment.
  52. NSString* GIDEnvironment(void) {
  53. NSString *appleEnvironment = kAppleEnvironmentUnknown;
  54. #if TARGET_OS_MACCATALYST
  55. appleEnvironment = kAppleEnvironmentMacOSMacCatalyst;
  56. #elif TARGET_OS_IOS
  57. #if TARGET_OS_SIMULATOR
  58. appleEnvironment = kAppleEnvironmentIOSSimulator;
  59. #else // TARGET_OS_SIMULATOR
  60. #if defined(__IPHONE_14_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_14_0
  61. if (@available(iOS 14.0, *)) {
  62. if ([NSProcessInfo.processInfo respondsToSelector:@selector(isiOSAppOnMac)]) {
  63. appleEnvironment = NSProcessInfo.processInfo.iOSAppOnMac ? kAppleEnvironmentMacOSIOSOnMac :
  64. kAppleEnvironmentIOS;
  65. } else {
  66. appleEnvironment = kAppleEnvironmentIOS;
  67. }
  68. }
  69. #else // defined(__IPHONE_14_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_14_0
  70. appleEnvironment = kAppleEnvironmentIOS;
  71. #endif // defined(__IPHONE_14_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_14_0
  72. #endif // TARGET_OS_SIMULATOR
  73. #elif TARGET_OS_OSX
  74. appleEnvironment = kAppleEnvironmentMacOS;
  75. #endif // TARGET_OS_MACCATALYST
  76. return appleEnvironment;
  77. }
  78. @implementation GIDSignInPreferences
  79. + (NSString *)googleAuthorizationServer {
  80. return kLSOServer;
  81. }
  82. + (NSString *)googleTokenServer {
  83. return kTokenServer;
  84. }
  85. + (NSString *)googleUserInfoServer {
  86. return kUserInfoServer;
  87. }
  88. + (NSURL *)authorizationEndpointURL {
  89. NSString *authorizationEnpointURL = [NSString stringWithFormat:kAuthorizationURLTemplate,
  90. [self googleAuthorizationServer]];
  91. return [NSURL URLWithString:authorizationEnpointURL];
  92. }
  93. + (NSURL *)tokenEndpointURL {
  94. NSString *tokenEndpointURL = [NSString stringWithFormat:kTokenURLTemplate,
  95. [self googleTokenServer]];
  96. return [NSURL URLWithString:tokenEndpointURL];
  97. }
  98. @end
  99. NS_ASSUME_NONNULL_END