1 |
|
2 | "use strict";
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | Object.defineProperty(exports, "__esModule", { value: true });
|
19 | exports.Tenant = void 0;
|
20 | var validator = require("../utils/validator");
|
21 | var deep_copy_1 = require("../utils/deep-copy");
|
22 | var error_1 = require("../utils/error");
|
23 | var auth_config_1 = require("./auth-config");
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | var Tenant = (function () {
|
45 | |
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
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 |
|
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 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
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 |
|
103 | request.testPhoneNumbers = (_a = tenantOptions.testPhoneNumbers) !== null && _a !== void 0 ? _a : {};
|
104 | }
|
105 | return request;
|
106 | };
|
107 | |
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | Tenant.getTenantIdFromResourceName = function (resourceName) {
|
116 |
|
117 | var matchTenantRes = resourceName.match(/\/tenants\/(.*)$/);
|
118 | if (!matchTenantRes || matchTenantRes.length < 2) {
|
119 | return null;
|
120 | }
|
121 | return matchTenantRes[1];
|
122 | };
|
123 | |
124 |
|
125 |
|
126 |
|
127 |
|
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 |
|
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 |
|
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 |
|
153 | if (typeof request.emailSignInConfig !== 'undefined') {
|
154 |
|
155 | auth_config_1.EmailSignInConfig.buildServerRequest(request.emailSignInConfig);
|
156 | }
|
157 |
|
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 |
|
164 | throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, "\"" + label + ".testPhoneNumbers\" must be a non-null object.");
|
165 | }
|
166 |
|
167 | if (typeof request.multiFactorConfig !== 'undefined') {
|
168 |
|
169 | auth_config_1.MultiFactorAuthConfig.buildServerRequest(request.multiFactorConfig);
|
170 | }
|
171 | };
|
172 | Object.defineProperty(Tenant.prototype, "emailSignInConfig", {
|
173 | |
174 |
|
175 |
|
176 | get: function () {
|
177 | return this.emailSignInConfig_;
|
178 | },
|
179 | enumerable: false,
|
180 | configurable: true
|
181 | });
|
182 | Object.defineProperty(Tenant.prototype, "multiFactorConfig", {
|
183 | |
184 |
|
185 |
|
186 | get: function () {
|
187 | return this.multiFactorConfig_;
|
188 | },
|
189 | enumerable: false,
|
190 | configurable: true
|
191 | });
|
192 | |
193 |
|
194 |
|
195 |
|
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 | }());
|
217 | exports.Tenant = Tenant;
|