FIRIdentityToolkitRequest.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. }
  34. - (nullable instancetype)initWithEndpoint:(NSString *)endpoint
  35. requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration {
  36. self = [super init];
  37. if (self) {
  38. _APIKey = [requestConfiguration.APIKey copy];
  39. _endpoint = [endpoint copy];
  40. _requestConfiguration = requestConfiguration;
  41. _useIdentityPlatform = NO;
  42. _useStaging = NO;
  43. // Automatically set the tenant ID. If the request is initialized before FIRAuth is configured,
  44. // set tenant ID to nil.
  45. @try {
  46. _tenantID = [FIRAuth auth].tenantID;
  47. } @catch (NSException *e) {
  48. _tenantID = nil;
  49. }
  50. }
  51. return self;
  52. }
  53. - (BOOL)containsPostBody {
  54. return YES;
  55. }
  56. - (nullable NSString *)queryParams {
  57. return nil;
  58. }
  59. - (NSURL *)requestURL {
  60. NSString *apiURLFormat;
  61. NSString *apiProtocol;
  62. NSString *apiHostAndPathPrefix;
  63. NSString *emulatorHostAndPort = _requestConfiguration.emulatorHostAndPort;
  64. if (_useIdentityPlatform) {
  65. apiURLFormat = kIdentityPlatformAPIURLFormat;
  66. apiProtocol = kHttpsProtocol;
  67. if (emulatorHostAndPort) {
  68. apiProtocol = kHttpProtocol;
  69. apiHostAndPathPrefix =
  70. [NSString stringWithFormat:kEmulatorHostAndPrefixFormat, emulatorHostAndPort,
  71. kIdentityPlatformAPIHost];
  72. } else if (_useStaging) {
  73. apiHostAndPathPrefix = kIdentityPlatformStagingAPIHost;
  74. } else {
  75. apiHostAndPathPrefix = kIdentityPlatformAPIHost;
  76. }
  77. } else {
  78. apiURLFormat = kFirebaseAuthAPIURLFormat;
  79. apiProtocol = kHttpsProtocol;
  80. if (emulatorHostAndPort) {
  81. apiProtocol = kHttpProtocol;
  82. apiHostAndPathPrefix = [NSString
  83. stringWithFormat:kEmulatorHostAndPrefixFormat, emulatorHostAndPort, kFirebaseAuthAPIHost];
  84. } else if (_useStaging) {
  85. apiHostAndPathPrefix = kFirebaseAuthStagingAPIHost;
  86. } else {
  87. apiHostAndPathPrefix = kFirebaseAuthAPIHost;
  88. }
  89. }
  90. NSMutableString *URLString = [NSMutableString
  91. stringWithFormat:apiURLFormat, apiProtocol, apiHostAndPathPrefix, _endpoint, _APIKey];
  92. NSString *queryParams = [self queryParams];
  93. if (queryParams) {
  94. [URLString appendString:queryParams];
  95. }
  96. NSURL *URL = [NSURL URLWithString:URLString];
  97. return URL;
  98. }
  99. - (FIRAuthRequestConfiguration *)requestConfiguration {
  100. return _requestConfiguration;
  101. }
  102. #pragma mark - Internal API for development
  103. + (NSString *)host {
  104. return gAPIHost;
  105. }
  106. + (void)setHost:(NSString *)host {
  107. gAPIHost = host;
  108. }
  109. NS_ASSUME_NONNULL_END
  110. @end