FIRIdentityToolkitRequest.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FirebaseAuth/Sources/Backend/FIRIdentityToolkitRequest.h"
  17. #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRAuth.h"
  18. NS_ASSUME_NONNULL_BEGIN
  19. static NSString *const kHttpsProtocol = @"https:";
  20. static NSString *const kHttpProtocol = @"http:";
  21. static NSString *const kFirebaseAuthAPIURLFormat =
  22. @"%@//%@/identitytoolkit/v3/relyingparty/%@?key=%@";
  23. static NSString *const kIdentityPlatformAPIURLFormat = @"%@//%@/v2/%@?key=%@";
  24. static NSString *const kEmulatorHostAndPrefixFormat = @"%@/%@";
  25. static NSString *gAPIHost = @"www.googleapis.com";
  26. static NSString *kFirebaseAuthAPIHost = @"www.googleapis.com";
  27. static NSString *kIdentityPlatformAPIHost = @"identitytoolkit.googleapis.com";
  28. static NSString *kFirebaseAuthStagingAPIHost = @"staging-www.sandbox.googleapis.com";
  29. static NSString *kIdentityPlatformStagingAPIHost =
  30. @"staging-identitytoolkit.sandbox.googleapis.com";
  31. @implementation FIRIdentityToolkitRequest {
  32. FIRAuthRequestConfiguration *_requestConfiguration;
  33. BOOL _useIdentityPlatform;
  34. BOOL _useStaging;
  35. }
  36. - (nullable instancetype)initWithEndpoint:(NSString *)endpoint
  37. requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration {
  38. self = [super init];
  39. if (self) {
  40. _APIKey = [requestConfiguration.APIKey copy];
  41. _endpoint = [endpoint copy];
  42. _requestConfiguration = requestConfiguration;
  43. _useIdentityPlatform = NO;
  44. _useStaging = NO;
  45. // Automatically set the tenant ID. If the request is initialized before FIRAuth is configured,
  46. // set tenant ID to nil.
  47. @try {
  48. _tenantID = [FIRAuth auth].tenantID;
  49. } @catch (NSException *e) {
  50. _tenantID = nil;
  51. }
  52. }
  53. return self;
  54. }
  55. - (nullable instancetype)initWithEndpoint:(NSString *)endpoint
  56. requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration
  57. useIdentityPlatform:(BOOL)useIdentityPlatform
  58. useStaging:(BOOL)useStaging {
  59. self = [self initWithEndpoint:endpoint requestConfiguration:requestConfiguration];
  60. if (self) {
  61. _useIdentityPlatform = useIdentityPlatform;
  62. _useStaging = useStaging;
  63. }
  64. return self;
  65. }
  66. - (BOOL)containsPostBody {
  67. return YES;
  68. }
  69. - (NSURL *)requestURL {
  70. NSString *apiURLFormat;
  71. NSString *apiProtocol;
  72. NSString *apiHostAndPathPrefix;
  73. NSString *emulatorHostAndPort = _requestConfiguration.emulatorHostAndPort;
  74. if (_useIdentityPlatform) {
  75. apiURLFormat = kIdentityPlatformAPIURLFormat;
  76. apiProtocol = kHttpsProtocol;
  77. if (emulatorHostAndPort) {
  78. apiProtocol = kHttpProtocol;
  79. apiHostAndPathPrefix =
  80. [NSString stringWithFormat:kEmulatorHostAndPrefixFormat, emulatorHostAndPort,
  81. kIdentityPlatformAPIHost];
  82. } else if (_useStaging) {
  83. apiHostAndPathPrefix = kIdentityPlatformStagingAPIHost;
  84. } else {
  85. apiHostAndPathPrefix = kIdentityPlatformAPIHost;
  86. }
  87. } else {
  88. apiURLFormat = kFirebaseAuthAPIURLFormat;
  89. apiProtocol = kHttpsProtocol;
  90. if (emulatorHostAndPort) {
  91. apiProtocol = kHttpProtocol;
  92. apiHostAndPathPrefix = [NSString
  93. stringWithFormat:kEmulatorHostAndPrefixFormat, emulatorHostAndPort, kFirebaseAuthAPIHost];
  94. } else if (_useStaging) {
  95. apiHostAndPathPrefix = kFirebaseAuthStagingAPIHost;
  96. } else {
  97. apiHostAndPathPrefix = kFirebaseAuthAPIHost;
  98. }
  99. }
  100. NSString *URLString = [NSString
  101. stringWithFormat:apiURLFormat, apiProtocol, apiHostAndPathPrefix, _endpoint, _APIKey];
  102. NSURL *URL = [NSURL URLWithString:URLString];
  103. return URL;
  104. }
  105. - (FIRAuthRequestConfiguration *)requestConfiguration {
  106. return _requestConfiguration;
  107. }
  108. #pragma mark - Internal API for development
  109. + (NSString *)host {
  110. return gAPIHost;
  111. }
  112. + (void)setHost:(NSString *)host {
  113. gAPIHost = host;
  114. }
  115. NS_ASSUME_NONNULL_END
  116. @end