FIRIdentityToolkitRequest.m 3.2 KB

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