GIDSignInPreferences.m 4.1 KB

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