UNPKG

9.46 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 */
18Object.defineProperty(exports, "__esModule", { value: true });
19exports.Tenant = void 0;
20var validator = require("../utils/validator");
21var deep_copy_1 = require("../utils/deep-copy");
22var error_1 = require("../utils/error");
23var auth_config_1 = require("./auth-config");
24/**
25 * Represents a tenant configuration.
26 *
27 * Multi-tenancy support requires Google Cloud's Identity Platform
28 * (GCIP). To learn more about GCIP, including pricing and features,
29 * see the {@link https://cloud.google.com/identity-platform | GCIP documentation}.
30 *
31 * Before multi-tenancy can be used on a Google Cloud Identity Platform project,
32 * tenants must be allowed on that project via the Cloud Console UI.
33 *
34 * A tenant configuration provides information such as the display name, tenant
35 * identifier and email authentication configuration.
36 * For OIDC/SAML provider configuration management, `TenantAwareAuth` instances should
37 * be used instead of a `Tenant` to retrieve the list of configured IdPs on a tenant.
38 * When configuring these providers, note that tenants will inherit
39 * whitelisted domains and authenticated redirect URIs of their parent project.
40 *
41 * All other settings of a tenant will also be inherited. These will need to be managed
42 * from the Cloud Console UI.
43 */
44var Tenant = /** @class */ (function () {
45 /**
46 * The Tenant object constructor.
47 *
48 * @param response - The server side response used to initialize the Tenant object.
49 * @constructor
50 * @internal
51 */
52 function Tenant(response) {
53 var tenantId = Tenant.getTenantIdFromResourceName(response.name);
54 if (!tenantId) {
55 throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INTERNAL_ERROR, 'INTERNAL ASSERT FAILED: Invalid tenant response');
56 }
57 this.tenantId = tenantId;
58 this.displayName = response.displayName;
59 try {
60 this.emailSignInConfig_ = new auth_config_1.EmailSignInConfig(response);
61 }
62 catch (e) {
63 // If allowPasswordSignup is undefined, it is disabled by default.
64 this.emailSignInConfig_ = new auth_config_1.EmailSignInConfig({
65 allowPasswordSignup: false,
66 });
67 }
68 this.anonymousSignInEnabled = !!response.enableAnonymousUser;
69 if (typeof response.mfaConfig !== 'undefined') {
70 this.multiFactorConfig_ = new auth_config_1.MultiFactorAuthConfig(response.mfaConfig);
71 }
72 if (typeof response.testPhoneNumbers !== 'undefined') {
73 this.testPhoneNumbers = deep_copy_1.deepCopy(response.testPhoneNumbers || {});
74 }
75 }
76 /**
77 * Builds the corresponding server request for a TenantOptions object.
78 *
79 * @param tenantOptions - The properties to convert to a server request.
80 * @param createRequest - Whether this is a create request.
81 * @returns The equivalent server request.
82 *
83 * @internal
84 */
85 Tenant.buildServerRequest = function (tenantOptions, createRequest) {
86 var _a;
87 Tenant.validate(tenantOptions, createRequest);
88 var request = {};
89 if (typeof tenantOptions.emailSignInConfig !== 'undefined') {
90 request = auth_config_1.EmailSignInConfig.buildServerRequest(tenantOptions.emailSignInConfig);
91 }
92 if (typeof tenantOptions.displayName !== 'undefined') {
93 request.displayName = tenantOptions.displayName;
94 }
95 if (typeof tenantOptions.anonymousSignInEnabled !== 'undefined') {
96 request.enableAnonymousUser = tenantOptions.anonymousSignInEnabled;
97 }
98 if (typeof tenantOptions.multiFactorConfig !== 'undefined') {
99 request.mfaConfig = auth_config_1.MultiFactorAuthConfig.buildServerRequest(tenantOptions.multiFactorConfig);
100 }
101 if (typeof tenantOptions.testPhoneNumbers !== 'undefined') {
102 // null will clear existing test phone numbers. Translate to empty object.
103 request.testPhoneNumbers = (_a = tenantOptions.testPhoneNumbers) !== null && _a !== void 0 ? _a : {};
104 }
105 return request;
106 };
107 /**
108 * Returns the tenant ID corresponding to the resource name if available.
109 *
110 * @param resourceName - The server side resource name
111 * @returns The tenant ID corresponding to the resource, null otherwise.
112 *
113 * @internal
114 */
115 Tenant.getTenantIdFromResourceName = function (resourceName) {
116 // name is of form projects/project1/tenants/tenant1
117 var matchTenantRes = resourceName.match(/\/tenants\/(.*)$/);
118 if (!matchTenantRes || matchTenantRes.length < 2) {
119 return null;
120 }
121 return matchTenantRes[1];
122 };
123 /**
124 * Validates a tenant options object. Throws an error on failure.
125 *
126 * @param request - The tenant options object to validate.
127 * @param createRequest - Whether this is a create request.
128 */
129 Tenant.validate = function (request, createRequest) {
130 var validKeys = {
131 displayName: true,
132 emailSignInConfig: true,
133 anonymousSignInEnabled: true,
134 multiFactorConfig: true,
135 testPhoneNumbers: true,
136 };
137 var label = createRequest ? 'CreateTenantRequest' : 'UpdateTenantRequest';
138 if (!validator.isNonNullObject(request)) {
139 throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, "\"" + label + "\" must be a valid non-null object.");
140 }
141 // Check for unsupported top level attributes.
142 for (var key in request) {
143 if (!(key in validKeys)) {
144 throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, "\"" + key + "\" is not a valid " + label + " parameter.");
145 }
146 }
147 // Validate displayName type if provided.
148 if (typeof request.displayName !== 'undefined' &&
149 !validator.isNonEmptyString(request.displayName)) {
150 throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, "\"" + label + ".displayName\" must be a valid non-empty string.");
151 }
152 // Validate emailSignInConfig type if provided.
153 if (typeof request.emailSignInConfig !== 'undefined') {
154 // This will throw an error if invalid.
155 auth_config_1.EmailSignInConfig.buildServerRequest(request.emailSignInConfig);
156 }
157 // Validate test phone numbers if provided.
158 if (typeof request.testPhoneNumbers !== 'undefined' &&
159 request.testPhoneNumbers !== null) {
160 auth_config_1.validateTestPhoneNumbers(request.testPhoneNumbers);
161 }
162 else if (request.testPhoneNumbers === null && createRequest) {
163 // null allowed only for update operations.
164 throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, "\"" + label + ".testPhoneNumbers\" must be a non-null object.");
165 }
166 // Validate multiFactorConfig type if provided.
167 if (typeof request.multiFactorConfig !== 'undefined') {
168 // This will throw an error if invalid.
169 auth_config_1.MultiFactorAuthConfig.buildServerRequest(request.multiFactorConfig);
170 }
171 };
172 Object.defineProperty(Tenant.prototype, "emailSignInConfig", {
173 /**
174 * The email sign in provider configuration.
175 */
176 get: function () {
177 return this.emailSignInConfig_;
178 },
179 enumerable: false,
180 configurable: true
181 });
182 Object.defineProperty(Tenant.prototype, "multiFactorConfig", {
183 /**
184 * The multi-factor auth configuration on the current tenant.
185 */
186 get: function () {
187 return this.multiFactorConfig_;
188 },
189 enumerable: false,
190 configurable: true
191 });
192 /**
193 * Returns a JSON-serializable representation of this object.
194 *
195 * @returns A JSON-serializable representation of this object.
196 */
197 Tenant.prototype.toJSON = function () {
198 var _a, _b;
199 var json = {
200 tenantId: this.tenantId,
201 displayName: this.displayName,
202 emailSignInConfig: (_a = this.emailSignInConfig_) === null || _a === void 0 ? void 0 : _a.toJSON(),
203 multiFactorConfig: (_b = this.multiFactorConfig_) === null || _b === void 0 ? void 0 : _b.toJSON(),
204 anonymousSignInEnabled: this.anonymousSignInEnabled,
205 testPhoneNumbers: this.testPhoneNumbers,
206 };
207 if (typeof json.multiFactorConfig === 'undefined') {
208 delete json.multiFactorConfig;
209 }
210 if (typeof json.testPhoneNumbers === 'undefined') {
211 delete json.testPhoneNumbers;
212 }
213 return json;
214 };
215 return Tenant;
216}());
217exports.Tenant = Tenant;