FIRIdentityToolkitRequest.m 4.1 KB

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