GIDSignInPreferences.m 4.2 KB

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