-
Notifications
You must be signed in to change notification settings - Fork 408
Starts defining multi-tenancy APIs. This includes: #526
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
/*! | ||
* Copyright 2019 Google Inc. | ||
* | ||
* 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. | ||
*/ | ||
|
||
import * as utils from '../utils'; | ||
import * as validator from '../utils/validator'; | ||
import {AuthClientErrorCode, FirebaseAuthError} from '../utils/error'; | ||
import { | ||
EmailSignInConfig, EmailSignInConfigServerRequest, EmailSignInProviderConfig, | ||
} from './auth-config'; | ||
|
||
/** The server side tenant type enum. */ | ||
export type TenantServerType = 'LIGHTWEIGHT' | 'FULL_SERVICE' | 'TYPE_UNSPECIFIED'; | ||
|
||
/** The client side tenant type enum. */ | ||
export type TenantType = 'lightweight' | 'full_service' | 'type_unspecified'; | ||
|
||
/** The TenantOptions interface used for create/read/update tenant operations. */ | ||
export interface TenantOptions { | ||
displayName?: string; | ||
type?: TenantType; | ||
emailSignInConfig?: EmailSignInProviderConfig; | ||
} | ||
|
||
/** The corresponding server side representation of a TenantOptions object. */ | ||
export interface TenantOptionsServerRequest extends EmailSignInConfigServerRequest { | ||
displayName?: string; | ||
type?: TenantServerType; | ||
} | ||
|
||
/** The tenant server response interface. */ | ||
export interface TenantServerResponse { | ||
name: string; | ||
type?: TenantServerType; | ||
displayName?: string; | ||
allowPasswordSignup: boolean; | ||
enableEmailLinkSignin: boolean; | ||
} | ||
|
||
/** The interface representing the listTenant API response. */ | ||
export interface ListTenantsResult { | ||
tenants: Tenant[]; | ||
pageToken?: string; | ||
} | ||
|
||
|
||
/** | ||
* Tenant class that defines a Firebase Auth tenant. | ||
*/ | ||
export class Tenant { | ||
public readonly tenantId: string; | ||
public readonly type?: TenantType; | ||
public readonly displayName?: string; | ||
public readonly emailSignInConfig?: EmailSignInConfig; | ||
|
||
/** | ||
* Builds the corresponding server request for a TenantOptions object. | ||
* | ||
* @param {TenantOptions} tenantOptions The properties to convert to a server request. | ||
* @param {boolean} createRequest Whether this is a create request. | ||
* @return {object} The equivalent server request. | ||
*/ | ||
public static buildServerRequest( | ||
hiranya911 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tenantOptions: TenantOptions, createRequest: boolean): TenantOptionsServerRequest { | ||
Tenant.validate(tenantOptions, createRequest); | ||
let request: TenantOptionsServerRequest = {}; | ||
if (typeof tenantOptions.emailSignInConfig !== 'undefined') { | ||
request = EmailSignInConfig.buildServerRequest(tenantOptions.emailSignInConfig); | ||
} | ||
if (typeof tenantOptions.displayName !== 'undefined') { | ||
request.displayName = tenantOptions.displayName; | ||
} | ||
if (typeof tenantOptions.type !== 'undefined') { | ||
request.type = tenantOptions.type.toUpperCase() as TenantServerType; | ||
} | ||
return request; | ||
} | ||
|
||
/** | ||
* Returns the tenant ID corresponding to the resource name if available. | ||
* | ||
* @param {string} resourceName The server side resource name | ||
* @return {?string} The tenant ID corresponding to the resource, null otherwise. | ||
*/ | ||
public static getTenantIdFromResourceName(resourceName: string): string | null { | ||
// name is of form projects/project1/tenants/tenant1 | ||
const matchTenantRes = resourceName.match(/\/tenants\/(.*)$/); | ||
if (!matchTenantRes || matchTenantRes.length < 2) { | ||
return null; | ||
} | ||
return matchTenantRes[1]; | ||
} | ||
|
||
/** | ||
* Validates a tenant options object. Throws an error on failure. | ||
* | ||
* @param {any} request The tenant options object to validate. | ||
* @param {boolean} createRequest Whether this is a create request. | ||
*/ | ||
private static validate(request: any, createRequest: boolean) { | ||
const validKeys = { | ||
displayName: true, | ||
type: true, | ||
emailSignInConfig: true, | ||
}; | ||
const label = createRequest ? 'CreateTenantRequest' : 'UpdateTenantRequest'; | ||
if (!validator.isNonNullObject(request)) { | ||
throw new FirebaseAuthError( | ||
AuthClientErrorCode.INVALID_ARGUMENT, | ||
`"${label}" must be a valid non-null object.`, | ||
); | ||
} | ||
// Check for unsupported top level attributes. | ||
for (const key in request) { | ||
if (!(key in validKeys)) { | ||
throw new FirebaseAuthError( | ||
AuthClientErrorCode.INVALID_ARGUMENT, | ||
`"${key}" is not a valid ${label} parameter.`, | ||
); | ||
} | ||
} | ||
// Validate displayName type if provided. | ||
if (typeof request.displayName !== 'undefined' && | ||
!validator.isNonEmptyString(request.displayName)) { | ||
throw new FirebaseAuthError( | ||
AuthClientErrorCode.INVALID_ARGUMENT, | ||
`"${label}.displayName" must be a valid non-empty string.`, | ||
); | ||
} | ||
// Validate type if provided. | ||
if (typeof request.type !== 'undefined' && !createRequest) { | ||
throw new FirebaseAuthError( | ||
AuthClientErrorCode.INVALID_ARGUMENT, | ||
'"Tenant.type" is an immutable property.', | ||
); | ||
} | ||
if (createRequest && | ||
request.type !== 'full_service' && | ||
request.type !== 'lightweight') { | ||
throw new FirebaseAuthError( | ||
AuthClientErrorCode.INVALID_ARGUMENT, | ||
`"${label}.type" must be either "full_service" or "lightweight".`, | ||
); | ||
} | ||
// Validate emailSignInConfig type if provided. | ||
if (typeof request.emailSignInConfig !== 'undefined') { | ||
// This will throw an error if invalid. | ||
EmailSignInConfig.buildServerRequest(request.emailSignInConfig); | ||
} | ||
} | ||
|
||
/** | ||
* The Tenant object constructor. | ||
* | ||
* @param {any} response The server side response used to initialize the Tenant object. | ||
* @constructor | ||
*/ | ||
constructor(response: any) { | ||
const tenantId = Tenant.getTenantIdFromResourceName(response.name); | ||
if (!tenantId) { | ||
throw new FirebaseAuthError( | ||
AuthClientErrorCode.INTERNAL_ERROR, | ||
'INTERNAL ASSERT FAILED: Invalid tenant response', | ||
); | ||
} | ||
this.tenantId = tenantId; | ||
this.displayName = response.displayName; | ||
this.type = (response.type && response.type.toLowerCase()) || undefined; | ||
try { | ||
this.emailSignInConfig = new EmailSignInConfig(response); | ||
} catch (e) { | ||
this.emailSignInConfig = undefined; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might want to put a comment here to indicate why it's ok to ignore the error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} | ||
} | ||
|
||
/** @return {object} The plain object representation of the tenant. */ | ||
public toJSON(): object { | ||
return { | ||
tenantId: this.tenantId, | ||
displayName: this.displayName, | ||
type: this.type, | ||
emailSignInConfig: this.emailSignInConfig && this.emailSignInConfig.toJSON(), | ||
}; | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.