1 |
|
2 | "use strict";
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | var __extends = (this && this.__extends) || (function () {
|
20 | var extendStatics = function (d, b) {
|
21 | extendStatics = Object.setPrototypeOf ||
|
22 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
23 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
24 | return extendStatics(d, b);
|
25 | };
|
26 | return function (d, b) {
|
27 | extendStatics(d, b);
|
28 | function __() { this.constructor = d; }
|
29 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
30 | };
|
31 | })();
|
32 | Object.defineProperty(exports, "__esModule", { value: true });
|
33 | exports.UserRecord = exports.UserInfo = exports.UserMetadata = exports.MultiFactorSettings = exports.PhoneMultiFactorInfo = exports.MultiFactorInfo = void 0;
|
34 | var deep_copy_1 = require("../utils/deep-copy");
|
35 | var validator_1 = require("../utils/validator");
|
36 | var utils = require("../utils");
|
37 | var error_1 = require("../utils/error");
|
38 |
|
39 |
|
40 |
|
41 | var B64_REDACTED = Buffer.from('REDACTED').toString('base64');
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | function parseDate(time) {
|
49 | try {
|
50 | var date = new Date(parseInt(time, 10));
|
51 | if (!isNaN(date.getTime())) {
|
52 | return date.toUTCString();
|
53 | }
|
54 | }
|
55 | catch (e) {
|
56 |
|
57 | }
|
58 | return null;
|
59 | }
|
60 | var MultiFactorId;
|
61 | (function (MultiFactorId) {
|
62 | MultiFactorId["Phone"] = "phone";
|
63 | })(MultiFactorId || (MultiFactorId = {}));
|
64 |
|
65 |
|
66 |
|
67 | var MultiFactorInfo = (function () {
|
68 | |
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | function MultiFactorInfo(response) {
|
76 | this.initFromServerResponse(response);
|
77 | }
|
78 | |
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | MultiFactorInfo.initMultiFactorInfo = function (response) {
|
86 | var multiFactorInfo = null;
|
87 |
|
88 | try {
|
89 | multiFactorInfo = new PhoneMultiFactorInfo(response);
|
90 | }
|
91 | catch (e) {
|
92 |
|
93 | }
|
94 | return multiFactorInfo;
|
95 | };
|
96 | |
97 |
|
98 |
|
99 |
|
100 |
|
101 | MultiFactorInfo.prototype.toJSON = function () {
|
102 | return {
|
103 | uid: this.uid,
|
104 | displayName: this.displayName,
|
105 | factorId: this.factorId,
|
106 | enrollmentTime: this.enrollmentTime,
|
107 | };
|
108 | };
|
109 | |
110 |
|
111 |
|
112 |
|
113 |
|
114 | MultiFactorInfo.prototype.initFromServerResponse = function (response) {
|
115 | var factorId = response && this.getFactorId(response);
|
116 | if (!factorId || !response || !response.mfaEnrollmentId) {
|
117 | throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INTERNAL_ERROR, 'INTERNAL ASSERT FAILED: Invalid multi-factor info response');
|
118 | }
|
119 | utils.addReadonlyGetter(this, 'uid', response.mfaEnrollmentId);
|
120 | utils.addReadonlyGetter(this, 'factorId', factorId);
|
121 | utils.addReadonlyGetter(this, 'displayName', response.displayName);
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | if (response.enrolledAt) {
|
127 | utils.addReadonlyGetter(this, 'enrollmentTime', new Date(response.enrolledAt).toUTCString());
|
128 | }
|
129 | else {
|
130 | utils.addReadonlyGetter(this, 'enrollmentTime', null);
|
131 | }
|
132 | };
|
133 | return MultiFactorInfo;
|
134 | }());
|
135 | exports.MultiFactorInfo = MultiFactorInfo;
|
136 |
|
137 |
|
138 |
|
139 | var PhoneMultiFactorInfo = (function (_super) {
|
140 | __extends(PhoneMultiFactorInfo, _super);
|
141 | |
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | function PhoneMultiFactorInfo(response) {
|
149 | var _this = _super.call(this, response) || this;
|
150 | utils.addReadonlyGetter(_this, 'phoneNumber', response.phoneInfo);
|
151 | return _this;
|
152 | }
|
153 | |
154 |
|
155 |
|
156 | PhoneMultiFactorInfo.prototype.toJSON = function () {
|
157 | return Object.assign(_super.prototype.toJSON.call(this), {
|
158 | phoneNumber: this.phoneNumber,
|
159 | });
|
160 | };
|
161 | |
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 | PhoneMultiFactorInfo.prototype.getFactorId = function (response) {
|
171 | return (response && response.phoneInfo) ? MultiFactorId.Phone : null;
|
172 | };
|
173 | return PhoneMultiFactorInfo;
|
174 | }(MultiFactorInfo));
|
175 | exports.PhoneMultiFactorInfo = PhoneMultiFactorInfo;
|
176 |
|
177 |
|
178 |
|
179 | var MultiFactorSettings = (function () {
|
180 | |
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | function MultiFactorSettings(response) {
|
188 | var parsedEnrolledFactors = [];
|
189 | if (!validator_1.isNonNullObject(response)) {
|
190 | throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INTERNAL_ERROR, 'INTERNAL ASSERT FAILED: Invalid multi-factor response');
|
191 | }
|
192 | else if (response.mfaInfo) {
|
193 | response.mfaInfo.forEach(function (factorResponse) {
|
194 | var multiFactorInfo = MultiFactorInfo.initMultiFactorInfo(factorResponse);
|
195 | if (multiFactorInfo) {
|
196 | parsedEnrolledFactors.push(multiFactorInfo);
|
197 | }
|
198 | });
|
199 | }
|
200 |
|
201 | utils.addReadonlyGetter(this, 'enrolledFactors', Object.freeze(parsedEnrolledFactors));
|
202 | }
|
203 | |
204 |
|
205 |
|
206 |
|
207 |
|
208 | MultiFactorSettings.prototype.toJSON = function () {
|
209 | return {
|
210 | enrolledFactors: this.enrolledFactors.map(function (info) { return info.toJSON(); }),
|
211 | };
|
212 | };
|
213 | return MultiFactorSettings;
|
214 | }());
|
215 | exports.MultiFactorSettings = MultiFactorSettings;
|
216 |
|
217 |
|
218 |
|
219 | var UserMetadata = (function () {
|
220 | |
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 | function UserMetadata(response) {
|
227 |
|
228 |
|
229 |
|
230 |
|
231 | utils.addReadonlyGetter(this, 'creationTime', parseDate(response.createdAt));
|
232 | utils.addReadonlyGetter(this, 'lastSignInTime', parseDate(response.lastLoginAt));
|
233 | var lastRefreshAt = response.lastRefreshAt ? new Date(response.lastRefreshAt).toUTCString() : null;
|
234 | utils.addReadonlyGetter(this, 'lastRefreshTime', lastRefreshAt);
|
235 | }
|
236 | |
237 |
|
238 |
|
239 |
|
240 |
|
241 | UserMetadata.prototype.toJSON = function () {
|
242 | return {
|
243 | lastSignInTime: this.lastSignInTime,
|
244 | creationTime: this.creationTime,
|
245 | };
|
246 | };
|
247 | return UserMetadata;
|
248 | }());
|
249 | exports.UserMetadata = UserMetadata;
|
250 |
|
251 |
|
252 |
|
253 |
|
254 | var UserInfo = (function () {
|
255 | |
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 | function UserInfo(response) {
|
262 |
|
263 | if (!response.rawId || !response.providerId) {
|
264 | throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INTERNAL_ERROR, 'INTERNAL ASSERT FAILED: Invalid user info response');
|
265 | }
|
266 | utils.addReadonlyGetter(this, 'uid', response.rawId);
|
267 | utils.addReadonlyGetter(this, 'displayName', response.displayName);
|
268 | utils.addReadonlyGetter(this, 'email', response.email);
|
269 | utils.addReadonlyGetter(this, 'photoURL', response.photoUrl);
|
270 | utils.addReadonlyGetter(this, 'providerId', response.providerId);
|
271 | utils.addReadonlyGetter(this, 'phoneNumber', response.phoneNumber);
|
272 | }
|
273 | |
274 |
|
275 |
|
276 |
|
277 |
|
278 | UserInfo.prototype.toJSON = function () {
|
279 | return {
|
280 | uid: this.uid,
|
281 | displayName: this.displayName,
|
282 | email: this.email,
|
283 | photoURL: this.photoURL,
|
284 | providerId: this.providerId,
|
285 | phoneNumber: this.phoneNumber,
|
286 | };
|
287 | };
|
288 | return UserInfo;
|
289 | }());
|
290 | exports.UserInfo = UserInfo;
|
291 |
|
292 |
|
293 |
|
294 | var UserRecord = (function () {
|
295 | |
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 | function UserRecord(response) {
|
302 |
|
303 | if (!response.localId) {
|
304 | throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INTERNAL_ERROR, 'INTERNAL ASSERT FAILED: Invalid user response');
|
305 | }
|
306 | utils.addReadonlyGetter(this, 'uid', response.localId);
|
307 | utils.addReadonlyGetter(this, 'email', response.email);
|
308 | utils.addReadonlyGetter(this, 'emailVerified', !!response.emailVerified);
|
309 | utils.addReadonlyGetter(this, 'displayName', response.displayName);
|
310 | utils.addReadonlyGetter(this, 'photoURL', response.photoUrl);
|
311 | utils.addReadonlyGetter(this, 'phoneNumber', response.phoneNumber);
|
312 |
|
313 | utils.addReadonlyGetter(this, 'disabled', response.disabled || false);
|
314 | utils.addReadonlyGetter(this, 'metadata', new UserMetadata(response));
|
315 | var providerData = [];
|
316 | for (var _i = 0, _a = (response.providerUserInfo || []); _i < _a.length; _i++) {
|
317 | var entry = _a[_i];
|
318 | providerData.push(new UserInfo(entry));
|
319 | }
|
320 | utils.addReadonlyGetter(this, 'providerData', providerData);
|
321 |
|
322 |
|
323 |
|
324 | if (response.passwordHash === B64_REDACTED) {
|
325 | utils.addReadonlyGetter(this, 'passwordHash', undefined);
|
326 | }
|
327 | else {
|
328 | utils.addReadonlyGetter(this, 'passwordHash', response.passwordHash);
|
329 | }
|
330 | utils.addReadonlyGetter(this, 'passwordSalt', response.salt);
|
331 | if (response.customAttributes) {
|
332 | utils.addReadonlyGetter(this, 'customClaims', JSON.parse(response.customAttributes));
|
333 | }
|
334 | var validAfterTime = null;
|
335 |
|
336 | if (typeof response.validSince !== 'undefined') {
|
337 | validAfterTime = parseDate(parseInt(response.validSince, 10) * 1000);
|
338 | }
|
339 | utils.addReadonlyGetter(this, 'tokensValidAfterTime', validAfterTime || undefined);
|
340 | utils.addReadonlyGetter(this, 'tenantId', response.tenantId);
|
341 | var multiFactor = new MultiFactorSettings(response);
|
342 | if (multiFactor.enrolledFactors.length > 0) {
|
343 | utils.addReadonlyGetter(this, 'multiFactor', multiFactor);
|
344 | }
|
345 | }
|
346 | |
347 |
|
348 |
|
349 |
|
350 |
|
351 | UserRecord.prototype.toJSON = function () {
|
352 | var json = {
|
353 | uid: this.uid,
|
354 | email: this.email,
|
355 | emailVerified: this.emailVerified,
|
356 | displayName: this.displayName,
|
357 | photoURL: this.photoURL,
|
358 | phoneNumber: this.phoneNumber,
|
359 | disabled: this.disabled,
|
360 |
|
361 | metadata: this.metadata.toJSON(),
|
362 | passwordHash: this.passwordHash,
|
363 | passwordSalt: this.passwordSalt,
|
364 | customClaims: deep_copy_1.deepCopy(this.customClaims),
|
365 | tokensValidAfterTime: this.tokensValidAfterTime,
|
366 | tenantId: this.tenantId,
|
367 | };
|
368 | if (this.multiFactor) {
|
369 | json.multiFactor = this.multiFactor.toJSON();
|
370 | }
|
371 | json.providerData = [];
|
372 | for (var _i = 0, _a = this.providerData; _i < _a.length; _i++) {
|
373 | var entry = _a[_i];
|
374 |
|
375 | json.providerData.push(entry.toJSON());
|
376 | }
|
377 | return json;
|
378 | };
|
379 | return UserRecord;
|
380 | }());
|
381 | exports.UserRecord = UserRecord;
|