Skip to content

Commit bf0cb09

Browse files
authored
Add multi tenant support (#6142)
1 parent 22e8912 commit bf0cb09

28 files changed

+304
-0
lines changed

FirebaseAuth/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Unreleased
2+
- [added] Added support for multi-tenancy (#6142).
23
- [added] Added basic watchOS support. (#4621)
34

45
# v6.8.0

FirebaseAuth/Sources/Auth/FIRAuth.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,7 @@ - (nullable instancetype)initWithAPIKey:(NSString *)APIKey appName:(NSString *)a
517517
if (!storedUserAccessGroup) {
518518
FIRUser *user;
519519
if ([strongSelf getUser:&user error:&error]) {
520+
strongSelf.tenantID = user.tenantID;
520521
[strongSelf updateCurrentUser:user byForce:NO savingToDisk:NO error:&error];
521522
self->_lastNotifiedUserToken = user.rawAccessToken;
522523
} else {
@@ -1975,6 +1976,15 @@ - (BOOL)updateCurrentUser:(nullable FIRUser *)user
19751976
[self possiblyPostAuthStateChangeNotification];
19761977
return YES;
19771978
}
1979+
if (user) {
1980+
if ((user.tenantID || self.tenantID) && ![self.tenantID isEqualToString:user.tenantID]) {
1981+
if (error) {
1982+
*error = [FIRAuthErrorUtils tenantIDMismatchError];
1983+
}
1984+
return NO;
1985+
}
1986+
}
1987+
19781988
BOOL success = YES;
19791989
if (saveToDisk) {
19801990
success = [self saveUser:user error:error];

FirebaseAuth/Sources/Backend/FIRAuthBackend.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,17 @@
396396
*/
397397
static NSString *const kCaptchaCheckFailedErrorMessage = @"CAPTCHA_CHECK_FAILED";
398398

399+
/** @var kTenantIDMismatch
400+
@brief This is the error message the server will respond with if the tenant id mismatches.
401+
*/
402+
static NSString *const kTenantIDMismatch = @"TENANT_ID_MISMATCH";
403+
404+
/** @var kUnsupportedTenantOperation
405+
@brief This is the error message the server will respond with if the operation does not support
406+
multi-tenant.
407+
*/
408+
static NSString *const kUnsupportedTenantOperation = @"UNSUPPORTED_TENANT_OPERATION";
409+
399410
/** @var kMissingMFAPendingCredentialErrorMessage
400411
@brief This is the error message the server will respond with if the MFA pending credential is
401412
missing.
@@ -1380,6 +1391,14 @@ + (nullable NSError *)clientErrorWithServerErrorMessage:(NSString *)serverErrorM
13801391
message:serverErrorMessage];
13811392
}
13821393

1394+
if ([shortErrorMessage isEqualToString:kTenantIDMismatch]) {
1395+
return [FIRAuthErrorUtils tenantIDMismatchError];
1396+
}
1397+
1398+
if ([shortErrorMessage isEqualToString:kUnsupportedTenantOperation]) {
1399+
return [FIRAuthErrorUtils unsupportedTenantOperationError];
1400+
}
1401+
13831402
// In this case we handle an error that might be specified in the underlying errors dictionary,
13841403
// the error message in determined based on the @c reason key in the dictionary.
13851404
if (errorDictionary[kErrorsKey]) {

FirebaseAuth/Sources/Backend/FIRIdentityToolkitRequest.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ NS_ASSUME_NONNULL_BEGIN
3737
*/
3838
@property(nonatomic, copy, readonly) NSString *APIKey;
3939

40+
/** @property tenantID
41+
@brief The tenant ID of the request. nil if none is available.
42+
*/
43+
@property(nonatomic, copy, readonly, nullable) NSString *tenantID;
44+
4045
/** @fn init
4146
@brief Please use initWithEndpoint:APIKey:
4247
*/

FirebaseAuth/Sources/Backend/FIRIdentityToolkitRequest.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#import "FirebaseAuth/Sources/Backend/FIRIdentityToolkitRequest.h"
1818

19+
#import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRAuth.h"
20+
1921
NS_ASSUME_NONNULL_BEGIN
2022

2123
static NSString *const kFirebaseAuthAPIURLFormat =
@@ -48,6 +50,14 @@ - (nullable instancetype)initWithEndpoint:(NSString *)endpoint
4850
_requestConfiguration = requestConfiguration;
4951
_useIdentityPlatform = NO;
5052
_useStaging = NO;
53+
54+
// Automatically set the tenant ID. If the request is initialized before FIRAuth is configured,
55+
// set tenant ID to nil.
56+
@try {
57+
_tenantID = [FIRAuth auth].tenantID;
58+
} @catch (NSException *e) {
59+
_tenantID = nil;
60+
}
5161
}
5262
return self;
5363
}

FirebaseAuth/Sources/Backend/RPC/FIRCreateAuthURIRequest.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@
5858
*/
5959
static NSString *const kAppIDKey = @"appId";
6060

61+
/** @var kTenantIDKey
62+
@brief The key for the tenant id value in the request.
63+
*/
64+
static NSString *const kTenantIDKey = @"tenantId";
65+
6166
@implementation FIRCreateAuthURIRequest
6267

6368
- (nullable instancetype)initWithIdentifier:(NSString *)identifier
@@ -89,6 +94,9 @@ - (nullable id)unencodedHTTPRequestBodyWithError:(NSError *_Nullable *_Nullable)
8994
if (_appID) {
9095
postBody[kAppIDKey] = _appID;
9196
}
97+
if (self.tenantID) {
98+
postBody[kTenantIDKey] = self.tenantID;
99+
}
92100
return [postBody copy];
93101
}
94102

FirebaseAuth/Sources/Backend/RPC/FIRGetOOBConfirmationCodeRequest.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@
106106
*/
107107
static NSString *const kVerifyBeforeUpdateEmailRequestTypeValue = @"VERIFY_AND_CHANGE_EMAIL";
108108

109+
/** @var kTenantIDKey
110+
@brief The key for the tenant id value in the request.
111+
*/
112+
static NSString *const kTenantIDKey = @"tenantId";
113+
109114
@interface FIRGetOOBConfirmationCodeRequest ()
110115

111116
/** @fn initWithRequestType:email:APIKey:
@@ -279,6 +284,9 @@ - (nullable id)unencodedHTTPRequestBodyWithError:(NSError *_Nullable *_Nullable)
279284
if (_dynamicLinkDomain) {
280285
body[kDynamicLinkDomainKey] = _dynamicLinkDomain;
281286
}
287+
if (self.tenantID) {
288+
body[kTenantIDKey] = self.tenantID;
289+
}
282290

283291
return body;
284292
}

FirebaseAuth/Sources/Backend/RPC/FIRResetPasswordRequest.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
*/
3434
static NSString *const kCurrentPasswordKey = @"newPassword";
3535

36+
/** @var kTenantIDKey
37+
@brief The key for the tenant id value in the request.
38+
*/
39+
static NSString *const kTenantIDKey = @"tenantId";
40+
3641
@implementation FIRResetPasswordRequest
3742

3843
- (nullable instancetype)initWithOobCode:(NSString *)oobCode
@@ -52,6 +57,9 @@ - (nullable id)unencodedHTTPRequestBodyWithError:(NSError *_Nullable *_Nullable)
5257
if (_updatedPassword) {
5358
postBody[kCurrentPasswordKey] = _updatedPassword;
5459
}
60+
if (self.tenantID) {
61+
postBody[kTenantIDKey] = self.tenantID;
62+
}
5563
return [postBody copy];
5664
}
5765

FirebaseAuth/Sources/Backend/RPC/FIRSendVerificationCodeRequest.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@
4545
*/
4646
static NSString *const kreCAPTCHATokenKey = @"recaptchaToken";
4747

48+
/** @var kTenantIDKey
49+
@brief The key for the tenant id value in the request.
50+
*/
51+
static NSString *const kTenantIDKey = @"tenantId";
52+
4853
@implementation FIRSendVerificationCodeRequest {
4954
}
5055

@@ -76,6 +81,9 @@ - (nullable id)unencodedHTTPRequestBodyWithError:(NSError *_Nullable *_Nullable)
7681
if (_reCAPTCHAToken) {
7782
postBody[kreCAPTCHATokenKey] = _reCAPTCHAToken;
7883
}
84+
if (self.tenantID) {
85+
postBody[kTenantIDKey] = self.tenantID;
86+
}
7987
return [postBody copy];
8088
}
8189

FirebaseAuth/Sources/Backend/RPC/FIRSetAccountInfoRequest.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@
113113
*/
114114
static NSString *const kReturnSecureTokenKey = @"returnSecureToken";
115115

116+
/** @var kTenantIDKey
117+
@brief The key for the tenant id value in the request.
118+
*/
119+
static NSString *const kTenantIDKey = @"tenantId";
120+
116121
@implementation FIRSetAccountInfoRequest
117122

118123
- (nullable instancetype)initWithRequestConfiguration:
@@ -171,6 +176,9 @@ - (nullable id)unencodedHTTPRequestBodyWithError:(NSError *_Nullable *_Nullable)
171176
if (_returnSecureToken) {
172177
postBody[kReturnSecureTokenKey] = @YES;
173178
}
179+
if (self.tenantID) {
180+
postBody[kTenantIDKey] = self.tenantID;
181+
}
174182
return [postBody copy];
175183
}
176184

0 commit comments

Comments
 (0)