GIDSignInPreferences.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 kOpenIDRealmParameter = @"openid.realm";
  24. NSString *const kBrowserCallbackPath = @"/oauth2callback";
  25. // Parameters in the callback URL coming back from browser.
  26. NSString *const kOAuth2ErrorKeyName = @"error";
  27. NSString *const kOAuth2AccessDenied = @"access_denied";
  28. NSString *const kEMMPasscodeInfoRequiredKeyName = @"emm_passcode_info_required";
  29. static NSString *const kLSOServer = @"accounts.google.com";
  30. static NSString *const kTokenServer = @"oauth2.googleapis.com";
  31. static NSString *const kUserInfoServer = @"www.googleapis.com";
  32. // Supported Apple execution environments
  33. static NSString *const kAppleEnvironmentUnknown = @"unknown";
  34. static NSString *const kAppleEnvironmentIOS = @"ios";
  35. static NSString *const kAppleEnvironmentIOSSimulator = @"ios-sim";
  36. static NSString *const kAppleEnvironmentMacOS = @"macos";
  37. static NSString *const kAppleEnvironmentMacOSIOSOnMac = @"macos-ios";
  38. static NSString *const kAppleEnvironmentMacOSMacCatalyst = @"macos-cat";
  39. // The URL template for the authorization endpoint.
  40. static NSString *const kAuthorizationURLTemplate = @"https://%@/o/oauth2/v2/auth";
  41. // The URL template for the token endpoint.
  42. static NSString *const kTokenURLTemplate = @"https://%@/token";
  43. #ifndef GID_SDK_VERSION
  44. #error "GID_SDK_VERSION is not defined: add -DGID_SDK_VERSION=x.x.x to the build invocation."
  45. #endif
  46. // Because macro expansions aren't performed on a token following the # preprocessor operator, we
  47. // wrap STR_EXPAND(x) with the STR(x) to produce a quoted string representation of a macro.
  48. // https://www.guyrutenberg.com/2008/12/20/expanding-macros-into-string-constants-in-c/
  49. #define STR(x) STR_EXPAND(x)
  50. #define STR_EXPAND(x) #x
  51. // The prefixed sdk version string to differentiate gid version values used with the legacy gpsdk
  52. // logging key.
  53. NSString* GIDVersion(void) {
  54. return [NSString stringWithFormat:@"gid-%@", @STR(GID_SDK_VERSION)];
  55. }
  56. // Get the current Apple execution environment.
  57. NSString* GIDEnvironment(void) {
  58. NSString *appleEnvironment = kAppleEnvironmentUnknown;
  59. #if TARGET_OS_MACCATALYST
  60. appleEnvironment = kAppleEnvironmentMacOSMacCatalyst;
  61. #elif TARGET_OS_IOS
  62. #if TARGET_OS_SIMULATOR
  63. appleEnvironment = kAppleEnvironmentIOSSimulator;
  64. #else // TARGET_OS_SIMULATOR
  65. #if defined(__IPHONE_14_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_14_0
  66. if (@available(iOS 14.0, *)) {
  67. if ([NSProcessInfo.processInfo respondsToSelector:@selector(isiOSAppOnMac)]) {
  68. appleEnvironment = NSProcessInfo.processInfo.iOSAppOnMac ? kAppleEnvironmentMacOSIOSOnMac :
  69. kAppleEnvironmentIOS;
  70. } else {
  71. appleEnvironment = kAppleEnvironmentIOS;
  72. }
  73. }
  74. #else // defined(__IPHONE_14_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_14_0
  75. appleEnvironment = kAppleEnvironmentIOS;
  76. #endif // defined(__IPHONE_14_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_14_0
  77. #endif // TARGET_OS_SIMULATOR
  78. #elif TARGET_OS_OSX
  79. appleEnvironment = kAppleEnvironmentMacOS;
  80. #endif // TARGET_OS_MACCATALYST
  81. return appleEnvironment;
  82. }
  83. @implementation GIDSignInPreferences
  84. + (NSString *)googleAuthorizationServer {
  85. return kLSOServer;
  86. }
  87. + (NSString *)googleTokenServer {
  88. return kTokenServer;
  89. }
  90. + (NSString *)googleUserInfoServer {
  91. return kUserInfoServer;
  92. }
  93. + (NSURL *)authorizationEndpointURL {
  94. NSString *authorizationEnpointURL = [NSString stringWithFormat:kAuthorizationURLTemplate,
  95. [self googleAuthorizationServer]];
  96. return [NSURL URLWithString:authorizationEnpointURL];
  97. }
  98. + (NSURL *)tokenEndpointURL {
  99. NSString *tokenEndpointURL = [NSString stringWithFormat:kTokenURLTemplate,
  100. [self googleTokenServer]];
  101. return [NSURL URLWithString:tokenEndpointURL];
  102. }
  103. @end
  104. NS_ASSUME_NONNULL_END