UNPKG

10.8 kBJavaScriptView Raw
1/*! firebase-admin v10.0.0 */
2"use strict";
3/*!
4 * Copyright 2019 Google Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18var __extends = (this && this.__extends) || (function () {
19 var extendStatics = function (d, b) {
20 extendStatics = Object.setPrototypeOf ||
21 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
22 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
23 return extendStatics(d, b);
24 };
25 return function (d, b) {
26 extendStatics(d, b);
27 function __() { this.constructor = d; }
28 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
29 };
30})();
31Object.defineProperty(exports, "__esModule", { value: true });
32exports.TenantManager = exports.TenantAwareAuth = void 0;
33var validator = require("../utils/validator");
34var utils = require("../utils/index");
35var error_1 = require("../utils/error");
36var base_auth_1 = require("./base-auth");
37var tenant_1 = require("./tenant");
38var auth_api_request_1 = require("./auth-api-request");
39/**
40 * Tenant-aware `Auth` interface used for managing users, configuring SAML/OIDC providers,
41 * generating email links for password reset, email verification, etc for specific tenants.
42 *
43 * Multi-tenancy support requires Google Cloud's Identity Platform
44 * (GCIP). To learn more about GCIP, including pricing and features,
45 * see the {@link https://cloud.google.com/identity-platform | GCIP documentation}.
46 *
47 * Each tenant contains its own identity providers, settings and sets of users.
48 * Using `TenantAwareAuth`, users for a specific tenant and corresponding OIDC/SAML
49 * configurations can also be managed, ID tokens for users signed in to a specific tenant
50 * can be verified, and email action links can also be generated for users belonging to the
51 * tenant.
52 *
53 * `TenantAwareAuth` instances for a specific `tenantId` can be instantiated by calling
54 * {@link TenantManager.authForTenant}.
55 */
56var TenantAwareAuth = /** @class */ (function (_super) {
57 __extends(TenantAwareAuth, _super);
58 /**
59 * The TenantAwareAuth class constructor.
60 *
61 * @param app - The app that created this tenant.
62 * @param tenantId - The corresponding tenant ID.
63 * @constructor
64 * @internal
65 */
66 function TenantAwareAuth(app, tenantId) {
67 var _this = _super.call(this, app, new auth_api_request_1.TenantAwareAuthRequestHandler(app, tenantId), base_auth_1.createFirebaseTokenGenerator(app, tenantId)) || this;
68 utils.addReadonlyGetter(_this, 'tenantId', tenantId);
69 return _this;
70 }
71 /**
72 * {@inheritdoc BaseAuth.verifyIdToken}
73 */
74 TenantAwareAuth.prototype.verifyIdToken = function (idToken, checkRevoked) {
75 var _this = this;
76 if (checkRevoked === void 0) { checkRevoked = false; }
77 return _super.prototype.verifyIdToken.call(this, idToken, checkRevoked)
78 .then(function (decodedClaims) {
79 // Validate tenant ID.
80 if (decodedClaims.firebase.tenant !== _this.tenantId) {
81 throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.MISMATCHING_TENANT_ID);
82 }
83 return decodedClaims;
84 });
85 };
86 /**
87 * {@inheritdoc BaseAuth.createSessionCookie}
88 */
89 TenantAwareAuth.prototype.createSessionCookie = function (idToken, sessionCookieOptions) {
90 var _this = this;
91 // Validate arguments before processing.
92 if (!validator.isNonEmptyString(idToken)) {
93 return Promise.reject(new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ID_TOKEN));
94 }
95 if (!validator.isNonNullObject(sessionCookieOptions) ||
96 !validator.isNumber(sessionCookieOptions.expiresIn)) {
97 return Promise.reject(new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_SESSION_COOKIE_DURATION));
98 }
99 // This will verify the ID token and then match the tenant ID before creating the session cookie.
100 return this.verifyIdToken(idToken)
101 .then(function () {
102 return _super.prototype.createSessionCookie.call(_this, idToken, sessionCookieOptions);
103 });
104 };
105 /**
106 * {@inheritdoc BaseAuth.verifySessionCookie}
107 */
108 TenantAwareAuth.prototype.verifySessionCookie = function (sessionCookie, checkRevoked) {
109 var _this = this;
110 if (checkRevoked === void 0) { checkRevoked = false; }
111 return _super.prototype.verifySessionCookie.call(this, sessionCookie, checkRevoked)
112 .then(function (decodedClaims) {
113 if (decodedClaims.firebase.tenant !== _this.tenantId) {
114 throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.MISMATCHING_TENANT_ID);
115 }
116 return decodedClaims;
117 });
118 };
119 return TenantAwareAuth;
120}(base_auth_1.BaseAuth));
121exports.TenantAwareAuth = TenantAwareAuth;
122/**
123 * Defines the tenant manager used to help manage tenant related operations.
124 * This includes:
125 * <ul>
126 * <li>The ability to create, update, list, get and delete tenants for the underlying
127 * project.</li>
128 * <li>Getting a `TenantAwareAuth` instance for running Auth related operations
129 * (user management, provider configuration management, token verification,
130 * email link generation, etc) in the context of a specified tenant.</li>
131 * </ul>
132 */
133var TenantManager = /** @class */ (function () {
134 /**
135 * Initializes a TenantManager instance for a specified FirebaseApp.
136 *
137 * @param app - The app for this TenantManager instance.
138 *
139 * @constructor
140 * @internal
141 */
142 function TenantManager(app) {
143 this.app = app;
144 this.authRequestHandler = new auth_api_request_1.AuthRequestHandler(app);
145 this.tenantsMap = {};
146 }
147 /**
148 * Returns a `TenantAwareAuth` instance bound to the given tenant ID.
149 *
150 * @param tenantId - The tenant ID whose `TenantAwareAuth` instance is to be returned.
151 *
152 * @returns The `TenantAwareAuth` instance corresponding to this tenant identifier.
153 */
154 TenantManager.prototype.authForTenant = function (tenantId) {
155 if (!validator.isNonEmptyString(tenantId)) {
156 throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_TENANT_ID);
157 }
158 if (typeof this.tenantsMap[tenantId] === 'undefined') {
159 this.tenantsMap[tenantId] = new TenantAwareAuth(this.app, tenantId);
160 }
161 return this.tenantsMap[tenantId];
162 };
163 /**
164 * Gets the tenant configuration for the tenant corresponding to a given `tenantId`.
165 *
166 * @param tenantId - The tenant identifier corresponding to the tenant whose data to fetch.
167 *
168 * @returns A promise fulfilled with the tenant configuration to the provided `tenantId`.
169 */
170 TenantManager.prototype.getTenant = function (tenantId) {
171 return this.authRequestHandler.getTenant(tenantId)
172 .then(function (response) {
173 return new tenant_1.Tenant(response);
174 });
175 };
176 /**
177 * Retrieves a list of tenants (single batch only) with a size of `maxResults`
178 * starting from the offset as specified by `pageToken`. This is used to
179 * retrieve all the tenants of a specified project in batches.
180 *
181 * @param maxResults - The page size, 1000 if undefined. This is also
182 * the maximum allowed limit.
183 * @param pageToken - The next page token. If not specified, returns
184 * tenants starting without any offset.
185 *
186 * @returns A promise that resolves with
187 * a batch of downloaded tenants and the next page token.
188 */
189 TenantManager.prototype.listTenants = function (maxResults, pageToken) {
190 return this.authRequestHandler.listTenants(maxResults, pageToken)
191 .then(function (response) {
192 // List of tenants to return.
193 var tenants = [];
194 // Convert each user response to a Tenant.
195 response.tenants.forEach(function (tenantResponse) {
196 tenants.push(new tenant_1.Tenant(tenantResponse));
197 });
198 // Return list of tenants and the next page token if available.
199 var result = {
200 tenants: tenants,
201 pageToken: response.nextPageToken,
202 };
203 // Delete result.pageToken if undefined.
204 if (typeof result.pageToken === 'undefined') {
205 delete result.pageToken;
206 }
207 return result;
208 });
209 };
210 /**
211 * Deletes an existing tenant.
212 *
213 * @param tenantId - The `tenantId` corresponding to the tenant to delete.
214 *
215 * @returns An empty promise fulfilled once the tenant has been deleted.
216 */
217 TenantManager.prototype.deleteTenant = function (tenantId) {
218 return this.authRequestHandler.deleteTenant(tenantId);
219 };
220 /**
221 * Creates a new tenant.
222 * When creating new tenants, tenants that use separate billing and quota will require their
223 * own project and must be defined as `full_service`.
224 *
225 * @param tenantOptions - The properties to set on the new tenant configuration to be created.
226 *
227 * @returns A promise fulfilled with the tenant configuration corresponding to the newly
228 * created tenant.
229 */
230 TenantManager.prototype.createTenant = function (tenantOptions) {
231 return this.authRequestHandler.createTenant(tenantOptions)
232 .then(function (response) {
233 return new tenant_1.Tenant(response);
234 });
235 };
236 /**
237 * Updates an existing tenant configuration.
238 *
239 * @param tenantId - The `tenantId` corresponding to the tenant to delete.
240 * @param tenantOptions - The properties to update on the provided tenant.
241 *
242 * @returns A promise fulfilled with the update tenant data.
243 */
244 TenantManager.prototype.updateTenant = function (tenantId, tenantOptions) {
245 return this.authRequestHandler.updateTenant(tenantId, tenantOptions)
246 .then(function (response) {
247 return new tenant_1.Tenant(response);
248 });
249 };
250 return TenantManager;
251}());
252exports.TenantManager = TenantManager;