tenant-manager.js 9.1 KB

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