Files
firebase-ios-sdk/FirebaseAuth/Sources/Backend/FIRIdentityToolkitRequest.m
T

140 lines
4.3 KiB
Objective-C
Raw Normal View History

2017-05-15 12:27:07 -07:00
/*
* Copyright 2017 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2020-04-10 11:54:51 -07:00
#import "FirebaseAuth/Sources/Backend/FIRIdentityToolkitRequest.h"
2017-05-15 12:27:07 -07:00
2020-08-17 17:25:27 -07:00
#import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRAuth.h"
2017-07-28 17:43:33 -07:00
NS_ASSUME_NONNULL_BEGIN
2020-09-10 16:14:55 -07:00
static NSString *const kHttpsProtocol = @"https:";
static NSString *const kHttpProtocol = @"http:";
2020-03-17 10:22:29 -07:00
static NSString *const kFirebaseAuthAPIURLFormat =
2020-09-10 16:14:55 -07:00
@"%@//%@/identitytoolkit/v3/relyingparty/%@?key=%@";
static NSString *const kIdentityPlatformAPIURLFormat = @"%@//%@/v2/%@?key=%@";
static NSString *const kEmulatorHostAndPrefixFormat = @"%@/%@";
2017-05-15 12:27:07 -07:00
static NSString *gAPIHost = @"www.googleapis.com";
2020-03-10 13:30:20 -07:00
static NSString *kFirebaseAuthAPIHost = @"www.googleapis.com";
static NSString *kIdentityPlatformAPIHost = @"identitytoolkit.googleapis.com";
static NSString *kFirebaseAuthStagingAPIHost = @"staging-www.sandbox.googleapis.com";
2020-03-17 10:22:29 -07:00
static NSString *kIdentityPlatformStagingAPIHost =
@"staging-identitytoolkit.sandbox.googleapis.com";
2020-03-10 13:30:20 -07:00
2017-07-28 17:43:33 -07:00
@implementation FIRIdentityToolkitRequest {
FIRAuthRequestConfiguration *_requestConfiguration;
2020-03-10 13:30:20 -07:00
BOOL _useIdentityPlatform;
BOOL _useStaging;
2017-07-28 17:43:33 -07:00
}
2017-05-15 12:27:07 -07:00
- (nullable instancetype)initWithEndpoint:(NSString *)endpoint
2017-07-28 17:43:33 -07:00
requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration {
2017-05-15 12:27:07 -07:00
self = [super init];
if (self) {
2017-07-28 17:43:33 -07:00
_APIKey = [requestConfiguration.APIKey copy];
2020-03-10 13:30:20 -07:00
_endpoint = [endpoint copy];
2017-07-28 17:43:33 -07:00
_requestConfiguration = requestConfiguration;
2020-03-10 13:30:20 -07:00
_useIdentityPlatform = NO;
_useStaging = NO;
2020-08-17 17:25:27 -07:00
// Automatically set the tenant ID. If the request is initialized before FIRAuth is configured,
// set tenant ID to nil.
@try {
_tenantID = [FIRAuth auth].tenantID;
} @catch (NSException *e) {
_tenantID = nil;
}
2020-03-10 13:30:20 -07:00
}
return self;
}
- (nullable instancetype)initWithEndpoint:(NSString *)endpoint
requestConfiguration:(FIRAuthRequestConfiguration *)requestConfiguration
useIdentityPlatform:(BOOL)useIdentityPlatform
useStaging:(BOOL)useStaging {
2020-03-17 10:22:29 -07:00
self = [self initWithEndpoint:endpoint requestConfiguration:requestConfiguration];
2020-03-10 13:30:20 -07:00
if (self) {
_useIdentityPlatform = useIdentityPlatform;
_useStaging = useStaging;
2017-05-15 12:27:07 -07:00
}
return self;
}
2017-08-24 13:58:50 -07:00
- (BOOL)containsPostBody {
return YES;
}
2017-05-15 12:27:07 -07:00
- (NSURL *)requestURL {
2020-03-10 13:30:20 -07:00
NSString *apiURLFormat;
2020-09-10 16:14:55 -07:00
NSString *apiProtocol;
NSString *apiHostAndPathPrefix;
NSString *emulatorHostAndPort = _requestConfiguration.emulatorHostAndPort;
2020-03-10 13:30:20 -07:00
if (_useIdentityPlatform) {
apiURLFormat = kIdentityPlatformAPIURLFormat;
2020-09-10 16:14:55 -07:00
apiProtocol = kHttpsProtocol;
if (emulatorHostAndPort) {
apiProtocol = kHttpProtocol;
apiHostAndPathPrefix =
[NSString stringWithFormat:kEmulatorHostAndPrefixFormat, emulatorHostAndPort,
kIdentityPlatformAPIHost];
} else if (_useStaging) {
apiHostAndPathPrefix = kIdentityPlatformStagingAPIHost;
2020-03-10 13:30:20 -07:00
} else {
2020-09-10 16:14:55 -07:00
apiHostAndPathPrefix = kIdentityPlatformAPIHost;
2020-03-10 13:30:20 -07:00
}
} else {
apiURLFormat = kFirebaseAuthAPIURLFormat;
2020-09-10 16:14:55 -07:00
apiProtocol = kHttpsProtocol;
if (emulatorHostAndPort) {
apiProtocol = kHttpProtocol;
apiHostAndPathPrefix = [NSString
stringWithFormat:kEmulatorHostAndPrefixFormat, emulatorHostAndPort, kFirebaseAuthAPIHost];
} else if (_useStaging) {
apiHostAndPathPrefix = kFirebaseAuthStagingAPIHost;
2020-03-10 13:30:20 -07:00
} else {
2020-09-10 16:14:55 -07:00
apiHostAndPathPrefix = kFirebaseAuthAPIHost;
2020-03-10 13:30:20 -07:00
}
}
2020-09-10 16:14:55 -07:00
NSString *URLString = [NSString
stringWithFormat:apiURLFormat, apiProtocol, apiHostAndPathPrefix, _endpoint, _APIKey];
2017-05-15 12:27:07 -07:00
NSURL *URL = [NSURL URLWithString:URLString];
return URL;
}
2017-07-28 17:43:33 -07:00
- (FIRAuthRequestConfiguration *)requestConfiguration {
return _requestConfiguration;
}
2017-05-15 12:27:07 -07:00
#pragma mark - Internal API for development
+ (NSString *)host {
return gAPIHost;
}
+ (void)setHost:(NSString *)host {
gAPIHost = host;
}
2017-07-28 17:43:33 -07:00
NS_ASSUME_NONNULL_END
2017-05-15 12:27:07 -07:00
@end