FIRIdentityToolkitRequest.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 kFirebaseAuthAPIURLFormat =
  20. @"https://%@/identitytoolkit/v3/relyingparty/%@?key=%@";
  21. static NSString *const kIdentityPlatformAPIURLFormat = @"https://%@/v2/%@?key=%@";
  22. static NSString *gAPIHost = @"www.googleapis.com";
  23. static NSString *kFirebaseAuthAPIHost = @"www.googleapis.com";
  24. static NSString *kIdentityPlatformAPIHost = @"identitytoolkit.googleapis.com";
  25. static NSString *kFirebaseAuthStagingAPIHost = @"staging-www.sandbox.googleapis.com";
  26. static NSString *kIdentityPlatformStagingAPIHost =
  27. @"staging-identitytoolkit.sandbox.googleapis.com";
  28. @implementation FIRIdentityToolkitRequest {
  29. FIRAuthRequestConfiguration *_requestConfiguration;
  30. BOOL _useIdentityPlatform;
  31. BOOL _useStaging;
  32. }
  33. - (nullable instancetype)initWithEndpoint:(NSString *)endpoint
  34. requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration {
  35. self = [super init];
  36. if (self) {
  37. _APIKey = [requestConfiguration.APIKey copy];
  38. _endpoint = [endpoint copy];
  39. _requestConfiguration = requestConfiguration;
  40. _useIdentityPlatform = NO;
  41. _useStaging = NO;
  42. // Automatically set the tenant ID. If the request is initialized before FIRAuth is configured,
  43. // set tenant ID to nil.
  44. @try {
  45. _tenantID = [FIRAuth auth].tenantID;
  46. } @catch (NSException *e) {
  47. _tenantID = nil;
  48. }
  49. }
  50. return self;
  51. }
  52. - (nullable instancetype)initWithEndpoint:(NSString *)endpoint
  53. requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration
  54. useIdentityPlatform:(BOOL)useIdentityPlatform
  55. useStaging:(BOOL)useStaging {
  56. self = [self initWithEndpoint:endpoint requestConfiguration:requestConfiguration];
  57. if (self) {
  58. _useIdentityPlatform = useIdentityPlatform;
  59. _useStaging = useStaging;
  60. }
  61. return self;
  62. }
  63. - (BOOL)containsPostBody {
  64. return YES;
  65. }
  66. - (NSURL *)requestURL {
  67. NSString *apiURLFormat;
  68. NSString *apiHost;
  69. if (_useIdentityPlatform) {
  70. apiURLFormat = kIdentityPlatformAPIURLFormat;
  71. if (_useStaging) {
  72. apiHost = kIdentityPlatformStagingAPIHost;
  73. } else {
  74. apiHost = kIdentityPlatformAPIHost;
  75. }
  76. } else {
  77. apiURLFormat = kFirebaseAuthAPIURLFormat;
  78. if (_useStaging) {
  79. apiHost = kFirebaseAuthStagingAPIHost;
  80. } else {
  81. apiHost = kFirebaseAuthAPIHost;
  82. }
  83. }
  84. NSString *URLString = [NSString stringWithFormat:apiURLFormat, apiHost, _endpoint, _APIKey];
  85. NSURL *URL = [NSURL URLWithString:URLString];
  86. return URL;
  87. }
  88. - (FIRAuthRequestConfiguration *)requestConfiguration {
  89. return _requestConfiguration;
  90. }
  91. #pragma mark - Internal API for development
  92. + (NSString *)host {
  93. return gAPIHost;
  94. }
  95. + (void)setHost:(NSString *)host {
  96. gAPIHost = host;
  97. }
  98. NS_ASSUME_NONNULL_END
  99. @end