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