FIRIdentityToolkitRequest.m 3.1 KB

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