|  | 
|  | 1 | +/*! | 
|  | 2 | + * Copyright 2019 Google Inc. | 
|  | 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 | + | 
|  | 17 | +import {AuthRequestHandler} from './auth-api-request'; | 
|  | 18 | +import {FirebaseApp} from '../firebase-app'; | 
|  | 19 | +import {TenantAwareAuth} from './auth'; | 
|  | 20 | +import { | 
|  | 21 | +  Tenant, TenantServerResponse, ListTenantsResult, TenantOptions, | 
|  | 22 | +} from './tenant'; | 
|  | 23 | +import {AuthClientErrorCode, FirebaseAuthError} from '../utils/error'; | 
|  | 24 | +import * as validator from '../utils/validator'; | 
|  | 25 | + | 
|  | 26 | +/** | 
|  | 27 | + * Data structure used to help manage tenant related operations. | 
|  | 28 | + * This includes: | 
|  | 29 | + * - The ability to create, update, list, get and delete tenants for the underlying project. | 
|  | 30 | + * - Getting a TenantAwareAuth instance for running Auth related operations (user mgmt, provider config mgmt, etc) | 
|  | 31 | + *   in the context of a specified tenant. | 
|  | 32 | + */ | 
|  | 33 | +export class TenantManager { | 
|  | 34 | +  private readonly authRequestHandler: AuthRequestHandler; | 
|  | 35 | +  private readonly tenantsMap: {[key: string]: TenantAwareAuth}; | 
|  | 36 | + | 
|  | 37 | +  /** | 
|  | 38 | +   * Initializes a TenantManager instance for a specified FirebaseApp. | 
|  | 39 | +   * @param app The app for this TenantManager instance. | 
|  | 40 | +   */ | 
|  | 41 | +  constructor(private readonly app: FirebaseApp) { | 
|  | 42 | +    this.authRequestHandler = new AuthRequestHandler(app); | 
|  | 43 | +    this.tenantsMap = {}; | 
|  | 44 | +  } | 
|  | 45 | + | 
|  | 46 | +  /** | 
|  | 47 | +   * Returns a TenantAwareAuth instance for the corresponding tenant ID. | 
|  | 48 | +   * | 
|  | 49 | +   * @param tenantId The tenant ID whose TenantAwareAuth is to be returned. | 
|  | 50 | +   * @return The corresponding TenantAwareAuth instance. | 
|  | 51 | +   */ | 
|  | 52 | +  public authForTenant(tenantId: string): TenantAwareAuth { | 
|  | 53 | +    if (!validator.isNonEmptyString(tenantId)) { | 
|  | 54 | +      throw new FirebaseAuthError(AuthClientErrorCode.INVALID_TENANT_ID); | 
|  | 55 | +    } | 
|  | 56 | +    if (typeof this.tenantsMap[tenantId] === 'undefined') { | 
|  | 57 | +      this.tenantsMap[tenantId] = new TenantAwareAuth(this.app, tenantId); | 
|  | 58 | +    } | 
|  | 59 | +    return this.tenantsMap[tenantId]; | 
|  | 60 | +  } | 
|  | 61 | + | 
|  | 62 | +  /** | 
|  | 63 | +   * Looks up the tenant identified by the provided tenant ID and returns a promise that is | 
|  | 64 | +   * fulfilled with the corresponding tenant if it is found. | 
|  | 65 | +   * | 
|  | 66 | +   * @param tenantId The tenant ID of the tenant to look up. | 
|  | 67 | +   * @return A promise that resolves with the corresponding tenant. | 
|  | 68 | +   */ | 
|  | 69 | +  public getTenant(tenantId: string): Promise<Tenant> { | 
|  | 70 | +    return this.authRequestHandler.getTenant(tenantId) | 
|  | 71 | +      .then((response: TenantServerResponse) => { | 
|  | 72 | +        return new Tenant(response); | 
|  | 73 | +      }); | 
|  | 74 | +  } | 
|  | 75 | + | 
|  | 76 | +  /** | 
|  | 77 | +   * Exports a batch of tenant accounts. Batch size is determined by the maxResults argument. | 
|  | 78 | +   * Starting point of the batch is determined by the pageToken argument. | 
|  | 79 | +   * | 
|  | 80 | +   * @param maxResults The page size, 1000 if undefined. This is also the maximum | 
|  | 81 | +   *     allowed limit. | 
|  | 82 | +   * @param pageToken The next page token. If not specified, returns users starting | 
|  | 83 | +   *     without any offset. | 
|  | 84 | +   * @return A promise that resolves with | 
|  | 85 | +   *     the current batch of downloaded tenants and the next page token. For the last page, an | 
|  | 86 | +   *     empty list of tenants and no page token are returned. | 
|  | 87 | +   */ | 
|  | 88 | +  public listTenants( | 
|  | 89 | +      maxResults?: number, | 
|  | 90 | +      pageToken?: string): Promise<ListTenantsResult> { | 
|  | 91 | +    return this.authRequestHandler.listTenants(maxResults, pageToken) | 
|  | 92 | +      .then((response: {tenants: TenantServerResponse[], nextPageToken?: string}) => { | 
|  | 93 | +        // List of tenants to return. | 
|  | 94 | +        const tenants: Tenant[] = []; | 
|  | 95 | +        // Convert each user response to a Tenant. | 
|  | 96 | +        response.tenants.forEach((tenantResponse: TenantServerResponse) => { | 
|  | 97 | +          tenants.push(new Tenant(tenantResponse)); | 
|  | 98 | +        }); | 
|  | 99 | +        // Return list of tenants and the next page token if available. | 
|  | 100 | +        const result = { | 
|  | 101 | +          tenants, | 
|  | 102 | +          pageToken: response.nextPageToken, | 
|  | 103 | +        }; | 
|  | 104 | +        // Delete result.pageToken if undefined. | 
|  | 105 | +        if (typeof result.pageToken === 'undefined') { | 
|  | 106 | +          delete result.pageToken; | 
|  | 107 | +        } | 
|  | 108 | +        return result; | 
|  | 109 | +      }); | 
|  | 110 | +  } | 
|  | 111 | + | 
|  | 112 | +  /** | 
|  | 113 | +   * Deletes the tenant identified by the provided tenant ID and returns a promise that is | 
|  | 114 | +   * fulfilled when the tenant is found and successfully deleted. | 
|  | 115 | +   * | 
|  | 116 | +   * @param tenantId The tenant ID of the tenant to delete. | 
|  | 117 | +   * @return A promise that resolves when the tenant is successfully deleted. | 
|  | 118 | +   */ | 
|  | 119 | +  public deleteTenant(tenantId: string): Promise<void> { | 
|  | 120 | +    return this.authRequestHandler.deleteTenant(tenantId); | 
|  | 121 | +  } | 
|  | 122 | + | 
|  | 123 | +  /** | 
|  | 124 | +   * Creates a new tenant with the properties provided. | 
|  | 125 | +   * | 
|  | 126 | +   * @param tenantOptions The properties to set on the new tenant to be created. | 
|  | 127 | +   * @return A promise that resolves with the newly created tenant. | 
|  | 128 | +   */ | 
|  | 129 | +  public createTenant(tenantOptions: TenantOptions): Promise<Tenant> { | 
|  | 130 | +    return this.authRequestHandler.createTenant(tenantOptions) | 
|  | 131 | +      .then((response: TenantServerResponse) => { | 
|  | 132 | +        return new Tenant(response); | 
|  | 133 | +      }); | 
|  | 134 | +  } | 
|  | 135 | + | 
|  | 136 | +  /** | 
|  | 137 | +   * Updates an existing tenant identified by the tenant ID with the properties provided. | 
|  | 138 | +   * | 
|  | 139 | +   * @param tenantId The tenant identifier of the tenant to update. | 
|  | 140 | +   * @param tenantOptions The properties to update on the existing tenant. | 
|  | 141 | +   * @return A promise that resolves with the modified tenant. | 
|  | 142 | +   */ | 
|  | 143 | +  public updateTenant(tenantId: string, tenantOptions: TenantOptions): Promise<Tenant> { | 
|  | 144 | +    return this.authRequestHandler.updateTenant(tenantId, tenantOptions) | 
|  | 145 | +      .then((response: TenantServerResponse) => { | 
|  | 146 | +        return new Tenant(response); | 
|  | 147 | +      }); | 
|  | 148 | +  } | 
|  | 149 | +} | 
0 commit comments