firebase-admin 9.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +90 -0
  3. package/lib/app-check/app-check-api-client-internal.js +197 -0
  4. package/lib/app-check/app-check.js +79 -0
  5. package/lib/app-check/index.d.ts +160 -0
  6. package/lib/app-check/index.js +19 -0
  7. package/lib/app-check/token-generator.js +161 -0
  8. package/lib/app-check/token-verifier.js +152 -0
  9. package/lib/auth/action-code-settings-builder.js +118 -0
  10. package/lib/auth/auth-api-request.js +1856 -0
  11. package/lib/auth/auth-config.js +636 -0
  12. package/lib/auth/auth.js +836 -0
  13. package/lib/auth/identifier.js +40 -0
  14. package/lib/auth/index.d.ts +1927 -0
  15. package/lib/auth/index.js +18 -0
  16. package/lib/auth/tenant-manager.js +140 -0
  17. package/lib/auth/tenant.js +171 -0
  18. package/lib/auth/token-generator.js +200 -0
  19. package/lib/auth/token-verifier.js +259 -0
  20. package/lib/auth/user-import-builder.js +387 -0
  21. package/lib/auth/user-record.js +346 -0
  22. package/lib/credential/credential-internal.js +391 -0
  23. package/lib/credential/credential.js +44 -0
  24. package/lib/credential/index.d.ts +169 -0
  25. package/lib/credential/index.js +23 -0
  26. package/lib/database/database-internal.js +266 -0
  27. package/lib/database/index.d.ts +89 -0
  28. package/lib/database/index.js +31 -0
  29. package/lib/default-namespace.js +31 -0
  30. package/lib/firebase-app.js +349 -0
  31. package/lib/firebase-namespace-api.d.ts +243 -0
  32. package/lib/firebase-namespace-api.js +18 -0
  33. package/lib/firebase-namespace.d.ts +31 -0
  34. package/lib/firebase-namespace.js +417 -0
  35. package/lib/firestore/firestore-internal.js +105 -0
  36. package/lib/firestore/index.d.ts +50 -0
  37. package/lib/firestore/index.js +47 -0
  38. package/lib/index.d.ts +24 -0
  39. package/lib/index.js +27 -0
  40. package/lib/installations/index.d.ts +81 -0
  41. package/lib/installations/index.js +18 -0
  42. package/lib/installations/installations-request-handler.js +117 -0
  43. package/lib/installations/installations.js +62 -0
  44. package/lib/instance-id/index.d.ts +83 -0
  45. package/lib/instance-id/index.js +18 -0
  46. package/lib/instance-id/instance-id.js +87 -0
  47. package/lib/machine-learning/index.d.ts +249 -0
  48. package/lib/machine-learning/index.js +18 -0
  49. package/lib/machine-learning/machine-learning-api-client.js +304 -0
  50. package/lib/machine-learning/machine-learning-utils.js +62 -0
  51. package/lib/machine-learning/machine-learning.js +364 -0
  52. package/lib/messaging/batch-request-internal.js +129 -0
  53. package/lib/messaging/index.d.ts +1174 -0
  54. package/lib/messaging/index.js +18 -0
  55. package/lib/messaging/messaging-api-request-internal.js +128 -0
  56. package/lib/messaging/messaging-errors-internal.js +106 -0
  57. package/lib/messaging/messaging-internal.js +484 -0
  58. package/lib/messaging/messaging.js +846 -0
  59. package/lib/project-management/android-app.js +176 -0
  60. package/lib/project-management/index.d.ts +363 -0
  61. package/lib/project-management/index.js +41 -0
  62. package/lib/project-management/ios-app.js +88 -0
  63. package/lib/project-management/project-management-api-request-internal.js +273 -0
  64. package/lib/project-management/project-management.js +254 -0
  65. package/lib/remote-config/index.d.ts +369 -0
  66. package/lib/remote-config/index.js +18 -0
  67. package/lib/remote-config/remote-config-api-client-internal.js +407 -0
  68. package/lib/remote-config/remote-config.js +304 -0
  69. package/lib/security-rules/index.d.ts +216 -0
  70. package/lib/security-rules/index.js +18 -0
  71. package/lib/security-rules/security-rules-api-client-internal.js +237 -0
  72. package/lib/security-rules/security-rules-internal.js +41 -0
  73. package/lib/security-rules/security-rules.js +310 -0
  74. package/lib/storage/index.d.ts +60 -0
  75. package/lib/storage/index.js +18 -0
  76. package/lib/storage/storage.js +123 -0
  77. package/lib/utils/api-request.js +845 -0
  78. package/lib/utils/crypto-signer.js +237 -0
  79. package/lib/utils/deep-copy.js +78 -0
  80. package/lib/utils/error.js +1063 -0
  81. package/lib/utils/index.js +217 -0
  82. package/lib/utils/jwt.js +355 -0
  83. package/lib/utils/validator.js +271 -0
  84. package/package.json +122 -0
@@ -0,0 +1,636 @@
1
+ /*! firebase-admin v9.12.0 */
2
+ "use strict";
3
+ /*!
4
+ * Copyright 2018 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.OIDCConfig = exports.SAMLConfig = exports.EmailSignInConfig = exports.validateTestPhoneNumbers = exports.MultiFactorAuthConfig = exports.MAXIMUM_TEST_PHONE_NUMBERS = 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
+ /** A maximum of 10 test phone number / code pairs can be configured. */
24
+ exports.MAXIMUM_TEST_PHONE_NUMBERS = 10;
25
+ /** Client Auth factor type to server auth factor type mapping. */
26
+ var AUTH_FACTOR_CLIENT_TO_SERVER_TYPE = {
27
+ phone: 'PHONE_SMS',
28
+ };
29
+ /** Server Auth factor type to client auth factor type mapping. */
30
+ var AUTH_FACTOR_SERVER_TO_CLIENT_TYPE = Object.keys(AUTH_FACTOR_CLIENT_TO_SERVER_TYPE)
31
+ .reduce(function (res, key) {
32
+ res[AUTH_FACTOR_CLIENT_TO_SERVER_TYPE[key]] = key;
33
+ return res;
34
+ }, {});
35
+ /**
36
+ * Defines the multi-factor config class used to convert client side MultiFactorConfig
37
+ * to a format that is understood by the Auth server.
38
+ */
39
+ var MultiFactorAuthConfig = /** @class */ (function () {
40
+ /**
41
+ * The MultiFactorAuthConfig constructor.
42
+ *
43
+ * @param response The server side response used to initialize the
44
+ * MultiFactorAuthConfig object.
45
+ * @constructor
46
+ */
47
+ function MultiFactorAuthConfig(response) {
48
+ var _this = this;
49
+ if (typeof response.state === 'undefined') {
50
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INTERNAL_ERROR, 'INTERNAL ASSERT FAILED: Invalid multi-factor configuration response');
51
+ }
52
+ this.state = response.state;
53
+ this.factorIds = [];
54
+ (response.enabledProviders || []).forEach(function (enabledProvider) {
55
+ // Ignore unsupported types. It is possible the current admin SDK version is
56
+ // not up to date and newer backend types are supported.
57
+ if (typeof AUTH_FACTOR_SERVER_TO_CLIENT_TYPE[enabledProvider] !== 'undefined') {
58
+ _this.factorIds.push(AUTH_FACTOR_SERVER_TO_CLIENT_TYPE[enabledProvider]);
59
+ }
60
+ });
61
+ }
62
+ /**
63
+ * Static method to convert a client side request to a MultiFactorAuthServerConfig.
64
+ * Throws an error if validation fails.
65
+ *
66
+ * @param options The options object to convert to a server request.
67
+ * @return The resulting server request.
68
+ */
69
+ MultiFactorAuthConfig.buildServerRequest = function (options) {
70
+ var request = {};
71
+ MultiFactorAuthConfig.validate(options);
72
+ if (Object.prototype.hasOwnProperty.call(options, 'state')) {
73
+ request.state = options.state;
74
+ }
75
+ if (Object.prototype.hasOwnProperty.call(options, 'factorIds')) {
76
+ (options.factorIds || []).forEach(function (factorId) {
77
+ if (typeof request.enabledProviders === 'undefined') {
78
+ request.enabledProviders = [];
79
+ }
80
+ request.enabledProviders.push(AUTH_FACTOR_CLIENT_TO_SERVER_TYPE[factorId]);
81
+ });
82
+ // In case an empty array is passed. Ensure it gets populated so the array is cleared.
83
+ if (options.factorIds && options.factorIds.length === 0) {
84
+ request.enabledProviders = [];
85
+ }
86
+ }
87
+ return request;
88
+ };
89
+ /**
90
+ * Validates the MultiFactorConfig options object. Throws an error on failure.
91
+ *
92
+ * @param options The options object to validate.
93
+ */
94
+ MultiFactorAuthConfig.validate = function (options) {
95
+ var validKeys = {
96
+ state: true,
97
+ factorIds: true,
98
+ };
99
+ if (!validator.isNonNullObject(options)) {
100
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_CONFIG, '"MultiFactorConfig" must be a non-null object.');
101
+ }
102
+ // Check for unsupported top level attributes.
103
+ for (var key in options) {
104
+ if (!(key in validKeys)) {
105
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_CONFIG, "\"" + key + "\" is not a valid MultiFactorConfig parameter.");
106
+ }
107
+ }
108
+ // Validate content.
109
+ if (typeof options.state !== 'undefined' &&
110
+ options.state !== 'ENABLED' &&
111
+ options.state !== 'DISABLED') {
112
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_CONFIG, '"MultiFactorConfig.state" must be either "ENABLED" or "DISABLED".');
113
+ }
114
+ if (typeof options.factorIds !== 'undefined') {
115
+ if (!validator.isArray(options.factorIds)) {
116
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_CONFIG, '"MultiFactorConfig.factorIds" must be an array of valid "AuthFactorTypes".');
117
+ }
118
+ // Validate content of array.
119
+ options.factorIds.forEach(function (factorId) {
120
+ if (typeof AUTH_FACTOR_CLIENT_TO_SERVER_TYPE[factorId] === 'undefined') {
121
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_CONFIG, "\"" + factorId + "\" is not a valid \"AuthFactorType\".");
122
+ }
123
+ });
124
+ }
125
+ };
126
+ /** @return The plain object representation of the multi-factor config instance. */
127
+ MultiFactorAuthConfig.prototype.toJSON = function () {
128
+ return {
129
+ state: this.state,
130
+ factorIds: this.factorIds,
131
+ };
132
+ };
133
+ return MultiFactorAuthConfig;
134
+ }());
135
+ exports.MultiFactorAuthConfig = MultiFactorAuthConfig;
136
+ /**
137
+ * Validates the provided map of test phone number / code pairs.
138
+ * @param testPhoneNumbers The phone number / code pairs to validate.
139
+ */
140
+ function validateTestPhoneNumbers(testPhoneNumbers) {
141
+ if (!validator.isObject(testPhoneNumbers)) {
142
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, '"testPhoneNumbers" must be a map of phone number / code pairs.');
143
+ }
144
+ if (Object.keys(testPhoneNumbers).length > exports.MAXIMUM_TEST_PHONE_NUMBERS) {
145
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.MAXIMUM_TEST_PHONE_NUMBER_EXCEEDED);
146
+ }
147
+ for (var phoneNumber in testPhoneNumbers) {
148
+ // Validate phone number.
149
+ if (!validator.isPhoneNumber(phoneNumber)) {
150
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_TESTING_PHONE_NUMBER, "\"" + phoneNumber + "\" is not a valid E.164 standard compliant phone number.");
151
+ }
152
+ // Validate code.
153
+ if (!validator.isString(testPhoneNumbers[phoneNumber]) ||
154
+ !/^[\d]{6}$/.test(testPhoneNumbers[phoneNumber])) {
155
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_TESTING_PHONE_NUMBER, "\"" + testPhoneNumbers[phoneNumber] + "\" is not a valid 6 digit code string.");
156
+ }
157
+ }
158
+ }
159
+ exports.validateTestPhoneNumbers = validateTestPhoneNumbers;
160
+ /**
161
+ * Defines the email sign-in config class used to convert client side EmailSignInConfig
162
+ * to a format that is understood by the Auth server.
163
+ */
164
+ var EmailSignInConfig = /** @class */ (function () {
165
+ /**
166
+ * The EmailSignInConfig constructor.
167
+ *
168
+ * @param {any} response The server side response used to initialize the
169
+ * EmailSignInConfig object.
170
+ * @constructor
171
+ */
172
+ function EmailSignInConfig(response) {
173
+ if (typeof response.allowPasswordSignup === 'undefined') {
174
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INTERNAL_ERROR, 'INTERNAL ASSERT FAILED: Invalid email sign-in configuration response');
175
+ }
176
+ this.enabled = response.allowPasswordSignup;
177
+ this.passwordRequired = !response.enableEmailLinkSignin;
178
+ }
179
+ /**
180
+ * Static method to convert a client side request to a EmailSignInConfigServerRequest.
181
+ * Throws an error if validation fails.
182
+ *
183
+ * @param {any} options The options object to convert to a server request.
184
+ * @return {EmailSignInConfigServerRequest} The resulting server request.
185
+ */
186
+ EmailSignInConfig.buildServerRequest = function (options) {
187
+ var request = {};
188
+ EmailSignInConfig.validate(options);
189
+ if (Object.prototype.hasOwnProperty.call(options, 'enabled')) {
190
+ request.allowPasswordSignup = options.enabled;
191
+ }
192
+ if (Object.prototype.hasOwnProperty.call(options, 'passwordRequired')) {
193
+ request.enableEmailLinkSignin = !options.passwordRequired;
194
+ }
195
+ return request;
196
+ };
197
+ /**
198
+ * Validates the EmailSignInConfig options object. Throws an error on failure.
199
+ *
200
+ * @param {any} options The options object to validate.
201
+ */
202
+ EmailSignInConfig.validate = function (options) {
203
+ // TODO: Validate the request.
204
+ var validKeys = {
205
+ enabled: true,
206
+ passwordRequired: true,
207
+ };
208
+ if (!validator.isNonNullObject(options)) {
209
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, '"EmailSignInConfig" must be a non-null object.');
210
+ }
211
+ // Check for unsupported top level attributes.
212
+ for (var key in options) {
213
+ if (!(key in validKeys)) {
214
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, "\"" + key + "\" is not a valid EmailSignInConfig parameter.");
215
+ }
216
+ }
217
+ // Validate content.
218
+ if (typeof options.enabled !== 'undefined' &&
219
+ !validator.isBoolean(options.enabled)) {
220
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, '"EmailSignInConfig.enabled" must be a boolean.');
221
+ }
222
+ if (typeof options.passwordRequired !== 'undefined' &&
223
+ !validator.isBoolean(options.passwordRequired)) {
224
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, '"EmailSignInConfig.passwordRequired" must be a boolean.');
225
+ }
226
+ };
227
+ /** @return {object} The plain object representation of the email sign-in config. */
228
+ EmailSignInConfig.prototype.toJSON = function () {
229
+ return {
230
+ enabled: this.enabled,
231
+ passwordRequired: this.passwordRequired,
232
+ };
233
+ };
234
+ return EmailSignInConfig;
235
+ }());
236
+ exports.EmailSignInConfig = EmailSignInConfig;
237
+ /**
238
+ * Defines the SAMLConfig class used to convert a client side configuration to its
239
+ * server side representation.
240
+ */
241
+ var SAMLConfig = /** @class */ (function () {
242
+ /**
243
+ * The SAMLConfig constructor.
244
+ *
245
+ * @param {any} response The server side response used to initialize the SAMLConfig object.
246
+ * @constructor
247
+ */
248
+ function SAMLConfig(response) {
249
+ if (!response ||
250
+ !response.idpConfig ||
251
+ !response.idpConfig.idpEntityId ||
252
+ !response.idpConfig.ssoUrl ||
253
+ !response.spConfig ||
254
+ !response.spConfig.spEntityId ||
255
+ !response.name ||
256
+ !(validator.isString(response.name) &&
257
+ SAMLConfig.getProviderIdFromResourceName(response.name))) {
258
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INTERNAL_ERROR, 'INTERNAL ASSERT FAILED: Invalid SAML configuration response');
259
+ }
260
+ var providerId = SAMLConfig.getProviderIdFromResourceName(response.name);
261
+ if (!providerId) {
262
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INTERNAL_ERROR, 'INTERNAL ASSERT FAILED: Invalid SAML configuration response');
263
+ }
264
+ this.providerId = providerId;
265
+ // RP config.
266
+ this.rpEntityId = response.spConfig.spEntityId;
267
+ this.callbackURL = response.spConfig.callbackUri;
268
+ // IdP config.
269
+ this.idpEntityId = response.idpConfig.idpEntityId;
270
+ this.ssoURL = response.idpConfig.ssoUrl;
271
+ this.enableRequestSigning = !!response.idpConfig.signRequest;
272
+ var x509Certificates = [];
273
+ for (var _i = 0, _a = (response.idpConfig.idpCertificates || []); _i < _a.length; _i++) {
274
+ var cert = _a[_i];
275
+ if (cert.x509Certificate) {
276
+ x509Certificates.push(cert.x509Certificate);
277
+ }
278
+ }
279
+ this.x509Certificates = x509Certificates;
280
+ // When enabled is undefined, it takes its default value of false.
281
+ this.enabled = !!response.enabled;
282
+ this.displayName = response.displayName;
283
+ }
284
+ /**
285
+ * Converts a client side request to a SAMLConfigServerRequest which is the format
286
+ * accepted by the backend server.
287
+ * Throws an error if validation fails. If the request is not a SAMLConfig request,
288
+ * returns null.
289
+ *
290
+ * @param {SAMLAuthProviderRequest} options The options object to convert to a server request.
291
+ * @param {boolean=} ignoreMissingFields Whether to ignore missing fields.
292
+ * @return {?SAMLConfigServerRequest} The resulting server request or null if not valid.
293
+ */
294
+ SAMLConfig.buildServerRequest = function (options, ignoreMissingFields) {
295
+ if (ignoreMissingFields === void 0) { ignoreMissingFields = false; }
296
+ var makeRequest = validator.isNonNullObject(options) &&
297
+ (options.providerId || ignoreMissingFields);
298
+ if (!makeRequest) {
299
+ return null;
300
+ }
301
+ var request = {};
302
+ // Validate options.
303
+ SAMLConfig.validate(options, ignoreMissingFields);
304
+ request.enabled = options.enabled;
305
+ request.displayName = options.displayName;
306
+ // IdP config.
307
+ if (options.idpEntityId || options.ssoURL || options.x509Certificates) {
308
+ request.idpConfig = {
309
+ idpEntityId: options.idpEntityId,
310
+ ssoUrl: options.ssoURL,
311
+ signRequest: options.enableRequestSigning,
312
+ idpCertificates: typeof options.x509Certificates === 'undefined' ? undefined : [],
313
+ };
314
+ if (options.x509Certificates) {
315
+ for (var _i = 0, _a = (options.x509Certificates || []); _i < _a.length; _i++) {
316
+ var cert = _a[_i];
317
+ request.idpConfig.idpCertificates.push({ x509Certificate: cert });
318
+ }
319
+ }
320
+ }
321
+ // RP config.
322
+ if (options.callbackURL || options.rpEntityId) {
323
+ request.spConfig = {
324
+ spEntityId: options.rpEntityId,
325
+ callbackUri: options.callbackURL,
326
+ };
327
+ }
328
+ return request;
329
+ };
330
+ /**
331
+ * Returns the provider ID corresponding to the resource name if available.
332
+ *
333
+ * @param {string} resourceName The server side resource name.
334
+ * @return {?string} The provider ID corresponding to the resource, null otherwise.
335
+ */
336
+ SAMLConfig.getProviderIdFromResourceName = function (resourceName) {
337
+ // name is of form projects/project1/inboundSamlConfigs/providerId1
338
+ var matchProviderRes = resourceName.match(/\/inboundSamlConfigs\/(saml\..*)$/);
339
+ if (!matchProviderRes || matchProviderRes.length < 2) {
340
+ return null;
341
+ }
342
+ return matchProviderRes[1];
343
+ };
344
+ /**
345
+ * @param {any} providerId The provider ID to check.
346
+ * @return {boolean} Whether the provider ID corresponds to a SAML provider.
347
+ */
348
+ SAMLConfig.isProviderId = function (providerId) {
349
+ return validator.isNonEmptyString(providerId) && providerId.indexOf('saml.') === 0;
350
+ };
351
+ /**
352
+ * Validates the SAMLConfig options object. Throws an error on failure.
353
+ *
354
+ * @param {SAMLAuthProviderRequest} options The options object to validate.
355
+ * @param {boolean=} ignoreMissingFields Whether to ignore missing fields.
356
+ */
357
+ SAMLConfig.validate = function (options, ignoreMissingFields) {
358
+ if (ignoreMissingFields === void 0) { ignoreMissingFields = false; }
359
+ var validKeys = {
360
+ enabled: true,
361
+ displayName: true,
362
+ providerId: true,
363
+ idpEntityId: true,
364
+ ssoURL: true,
365
+ x509Certificates: true,
366
+ rpEntityId: true,
367
+ callbackURL: true,
368
+ enableRequestSigning: true,
369
+ };
370
+ if (!validator.isNonNullObject(options)) {
371
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_CONFIG, '"SAMLAuthProviderConfig" must be a valid non-null object.');
372
+ }
373
+ // Check for unsupported top level attributes.
374
+ for (var key in options) {
375
+ if (!(key in validKeys)) {
376
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_CONFIG, "\"" + key + "\" is not a valid SAML config parameter.");
377
+ }
378
+ }
379
+ // Required fields.
380
+ if (validator.isNonEmptyString(options.providerId)) {
381
+ if (options.providerId.indexOf('saml.') !== 0) {
382
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_PROVIDER_ID, '"SAMLAuthProviderConfig.providerId" must be a valid non-empty string prefixed with "saml.".');
383
+ }
384
+ }
385
+ else if (!ignoreMissingFields) {
386
+ // providerId is required and not provided correctly.
387
+ throw new error_1.FirebaseAuthError(!options.providerId ? error_1.AuthClientErrorCode.MISSING_PROVIDER_ID : error_1.AuthClientErrorCode.INVALID_PROVIDER_ID, '"SAMLAuthProviderConfig.providerId" must be a valid non-empty string prefixed with "saml.".');
388
+ }
389
+ if (!(ignoreMissingFields && typeof options.idpEntityId === 'undefined') &&
390
+ !validator.isNonEmptyString(options.idpEntityId)) {
391
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_CONFIG, '"SAMLAuthProviderConfig.idpEntityId" must be a valid non-empty string.');
392
+ }
393
+ if (!(ignoreMissingFields && typeof options.ssoURL === 'undefined') &&
394
+ !validator.isURL(options.ssoURL)) {
395
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_CONFIG, '"SAMLAuthProviderConfig.ssoURL" must be a valid URL string.');
396
+ }
397
+ if (!(ignoreMissingFields && typeof options.rpEntityId === 'undefined') &&
398
+ !validator.isNonEmptyString(options.rpEntityId)) {
399
+ throw new error_1.FirebaseAuthError(!options.rpEntityId ? error_1.AuthClientErrorCode.MISSING_SAML_RELYING_PARTY_CONFIG :
400
+ error_1.AuthClientErrorCode.INVALID_CONFIG, '"SAMLAuthProviderConfig.rpEntityId" must be a valid non-empty string.');
401
+ }
402
+ if (!(ignoreMissingFields && typeof options.callbackURL === 'undefined') &&
403
+ !validator.isURL(options.callbackURL)) {
404
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_CONFIG, '"SAMLAuthProviderConfig.callbackURL" must be a valid URL string.');
405
+ }
406
+ if (!(ignoreMissingFields && typeof options.x509Certificates === 'undefined') &&
407
+ !validator.isArray(options.x509Certificates)) {
408
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_CONFIG, '"SAMLAuthProviderConfig.x509Certificates" must be a valid array of X509 certificate strings.');
409
+ }
410
+ (options.x509Certificates || []).forEach(function (cert) {
411
+ if (!validator.isNonEmptyString(cert)) {
412
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_CONFIG, '"SAMLAuthProviderConfig.x509Certificates" must be a valid array of X509 certificate strings.');
413
+ }
414
+ });
415
+ if (typeof options.enableRequestSigning !== 'undefined' &&
416
+ !validator.isBoolean(options.enableRequestSigning)) {
417
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_CONFIG, '"SAMLAuthProviderConfig.enableRequestSigning" must be a boolean.');
418
+ }
419
+ if (typeof options.enabled !== 'undefined' &&
420
+ !validator.isBoolean(options.enabled)) {
421
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_CONFIG, '"SAMLAuthProviderConfig.enabled" must be a boolean.');
422
+ }
423
+ if (typeof options.displayName !== 'undefined' &&
424
+ !validator.isString(options.displayName)) {
425
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_CONFIG, '"SAMLAuthProviderConfig.displayName" must be a valid string.');
426
+ }
427
+ };
428
+ /** @return The plain object representation of the SAMLConfig. */
429
+ SAMLConfig.prototype.toJSON = function () {
430
+ return {
431
+ enabled: this.enabled,
432
+ displayName: this.displayName,
433
+ providerId: this.providerId,
434
+ idpEntityId: this.idpEntityId,
435
+ ssoURL: this.ssoURL,
436
+ x509Certificates: deep_copy_1.deepCopy(this.x509Certificates),
437
+ rpEntityId: this.rpEntityId,
438
+ callbackURL: this.callbackURL,
439
+ enableRequestSigning: this.enableRequestSigning,
440
+ };
441
+ };
442
+ return SAMLConfig;
443
+ }());
444
+ exports.SAMLConfig = SAMLConfig;
445
+ /**
446
+ * Defines the OIDCConfig class used to convert a client side configuration to its
447
+ * server side representation.
448
+ */
449
+ var OIDCConfig = /** @class */ (function () {
450
+ /**
451
+ * The OIDCConfig constructor.
452
+ *
453
+ * @param {any} response The server side response used to initialize the OIDCConfig object.
454
+ * @constructor
455
+ */
456
+ function OIDCConfig(response) {
457
+ if (!response ||
458
+ !response.issuer ||
459
+ !response.clientId ||
460
+ !response.name ||
461
+ !(validator.isString(response.name) &&
462
+ OIDCConfig.getProviderIdFromResourceName(response.name))) {
463
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INTERNAL_ERROR, 'INTERNAL ASSERT FAILED: Invalid OIDC configuration response');
464
+ }
465
+ var providerId = OIDCConfig.getProviderIdFromResourceName(response.name);
466
+ if (!providerId) {
467
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INTERNAL_ERROR, 'INTERNAL ASSERT FAILED: Invalid SAML configuration response');
468
+ }
469
+ this.providerId = providerId;
470
+ this.clientId = response.clientId;
471
+ this.issuer = response.issuer;
472
+ // When enabled is undefined, it takes its default value of false.
473
+ this.enabled = !!response.enabled;
474
+ this.displayName = response.displayName;
475
+ if (typeof response.clientSecret !== 'undefined') {
476
+ this.clientSecret = response.clientSecret;
477
+ }
478
+ if (typeof response.responseType !== 'undefined') {
479
+ this.responseType = response.responseType;
480
+ }
481
+ }
482
+ /**
483
+ * Converts a client side request to a OIDCConfigServerRequest which is the format
484
+ * accepted by the backend server.
485
+ * Throws an error if validation fails. If the request is not a OIDCConfig request,
486
+ * returns null.
487
+ *
488
+ * @param options The options object to convert to a server request.
489
+ * @param ignoreMissingFields Whether to ignore missing fields.
490
+ * @return The resulting server request or null if not valid.
491
+ */
492
+ OIDCConfig.buildServerRequest = function (options, ignoreMissingFields) {
493
+ if (ignoreMissingFields === void 0) { ignoreMissingFields = false; }
494
+ var makeRequest = validator.isNonNullObject(options) &&
495
+ (options.providerId || ignoreMissingFields);
496
+ if (!makeRequest) {
497
+ return null;
498
+ }
499
+ var request = {};
500
+ // Validate options.
501
+ OIDCConfig.validate(options, ignoreMissingFields);
502
+ request.enabled = options.enabled;
503
+ request.displayName = options.displayName;
504
+ request.issuer = options.issuer;
505
+ request.clientId = options.clientId;
506
+ if (typeof options.clientSecret !== 'undefined') {
507
+ request.clientSecret = options.clientSecret;
508
+ }
509
+ if (typeof options.responseType !== 'undefined') {
510
+ request.responseType = options.responseType;
511
+ }
512
+ return request;
513
+ };
514
+ /**
515
+ * Returns the provider ID corresponding to the resource name if available.
516
+ *
517
+ * @param {string} resourceName The server side resource name
518
+ * @return {?string} The provider ID corresponding to the resource, null otherwise.
519
+ */
520
+ OIDCConfig.getProviderIdFromResourceName = function (resourceName) {
521
+ // name is of form projects/project1/oauthIdpConfigs/providerId1
522
+ var matchProviderRes = resourceName.match(/\/oauthIdpConfigs\/(oidc\..*)$/);
523
+ if (!matchProviderRes || matchProviderRes.length < 2) {
524
+ return null;
525
+ }
526
+ return matchProviderRes[1];
527
+ };
528
+ /**
529
+ * @param {any} providerId The provider ID to check.
530
+ * @return {boolean} Whether the provider ID corresponds to an OIDC provider.
531
+ */
532
+ OIDCConfig.isProviderId = function (providerId) {
533
+ return validator.isNonEmptyString(providerId) && providerId.indexOf('oidc.') === 0;
534
+ };
535
+ /**
536
+ * Validates the OIDCConfig options object. Throws an error on failure.
537
+ *
538
+ * @param options The options object to validate.
539
+ * @param ignoreMissingFields Whether to ignore missing fields.
540
+ */
541
+ OIDCConfig.validate = function (options, ignoreMissingFields) {
542
+ if (ignoreMissingFields === void 0) { ignoreMissingFields = false; }
543
+ var validKeys = {
544
+ enabled: true,
545
+ displayName: true,
546
+ providerId: true,
547
+ clientId: true,
548
+ issuer: true,
549
+ clientSecret: true,
550
+ responseType: true,
551
+ };
552
+ var validResponseTypes = {
553
+ idToken: true,
554
+ code: true,
555
+ };
556
+ if (!validator.isNonNullObject(options)) {
557
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_CONFIG, '"OIDCAuthProviderConfig" must be a valid non-null object.');
558
+ }
559
+ // Check for unsupported top level attributes.
560
+ for (var key in options) {
561
+ if (!(key in validKeys)) {
562
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_CONFIG, "\"" + key + "\" is not a valid OIDC config parameter.");
563
+ }
564
+ }
565
+ // Required fields.
566
+ if (validator.isNonEmptyString(options.providerId)) {
567
+ if (options.providerId.indexOf('oidc.') !== 0) {
568
+ throw new error_1.FirebaseAuthError(!options.providerId ? error_1.AuthClientErrorCode.MISSING_PROVIDER_ID : error_1.AuthClientErrorCode.INVALID_PROVIDER_ID, '"OIDCAuthProviderConfig.providerId" must be a valid non-empty string prefixed with "oidc.".');
569
+ }
570
+ }
571
+ else if (!ignoreMissingFields) {
572
+ throw new error_1.FirebaseAuthError(!options.providerId ? error_1.AuthClientErrorCode.MISSING_PROVIDER_ID : error_1.AuthClientErrorCode.INVALID_PROVIDER_ID, '"OIDCAuthProviderConfig.providerId" must be a valid non-empty string prefixed with "oidc.".');
573
+ }
574
+ if (!(ignoreMissingFields && typeof options.clientId === 'undefined') &&
575
+ !validator.isNonEmptyString(options.clientId)) {
576
+ throw new error_1.FirebaseAuthError(!options.clientId ? error_1.AuthClientErrorCode.MISSING_OAUTH_CLIENT_ID : error_1.AuthClientErrorCode.INVALID_OAUTH_CLIENT_ID, '"OIDCAuthProviderConfig.clientId" must be a valid non-empty string.');
577
+ }
578
+ if (!(ignoreMissingFields && typeof options.issuer === 'undefined') &&
579
+ !validator.isURL(options.issuer)) {
580
+ throw new error_1.FirebaseAuthError(!options.issuer ? error_1.AuthClientErrorCode.MISSING_ISSUER : error_1.AuthClientErrorCode.INVALID_CONFIG, '"OIDCAuthProviderConfig.issuer" must be a valid URL string.');
581
+ }
582
+ if (typeof options.enabled !== 'undefined' &&
583
+ !validator.isBoolean(options.enabled)) {
584
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_CONFIG, '"OIDCAuthProviderConfig.enabled" must be a boolean.');
585
+ }
586
+ if (typeof options.displayName !== 'undefined' &&
587
+ !validator.isString(options.displayName)) {
588
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_CONFIG, '"OIDCAuthProviderConfig.displayName" must be a valid string.');
589
+ }
590
+ if (typeof options.clientSecret !== 'undefined' &&
591
+ !validator.isNonEmptyString(options.clientSecret)) {
592
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_CONFIG, '"OIDCAuthProviderConfig.clientSecret" must be a valid string.');
593
+ }
594
+ if (validator.isNonNullObject(options.responseType) && typeof options.responseType !== 'undefined') {
595
+ Object.keys(options.responseType).forEach(function (key) {
596
+ if (!(key in validResponseTypes)) {
597
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_CONFIG, "\"" + key + "\" is not a valid OAuthResponseType parameter.");
598
+ }
599
+ });
600
+ var idToken = options.responseType.idToken;
601
+ if (typeof idToken !== 'undefined' && !validator.isBoolean(idToken)) {
602
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, '"OIDCAuthProviderConfig.responseType.idToken" must be a boolean.');
603
+ }
604
+ var code = options.responseType.code;
605
+ if (typeof code !== 'undefined') {
606
+ if (!validator.isBoolean(code)) {
607
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, '"OIDCAuthProviderConfig.responseType.code" must be a boolean.');
608
+ }
609
+ // If code flow is enabled, client secret must be provided.
610
+ if (code && typeof options.clientSecret === 'undefined') {
611
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.MISSING_OAUTH_CLIENT_SECRET, 'The OAuth configuration client secret is required to enable OIDC code flow.');
612
+ }
613
+ }
614
+ var allKeys = Object.keys(options.responseType).length;
615
+ var enabledCount = Object.values(options.responseType).filter(Boolean).length;
616
+ // Only one of OAuth response types can be set to true.
617
+ if (allKeys > 1 && enabledCount != 1) {
618
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_OAUTH_RESPONSETYPE, 'Only exactly one OAuth responseType should be set to true.');
619
+ }
620
+ }
621
+ };
622
+ /** @return {OIDCAuthProviderConfig} The plain object representation of the OIDCConfig. */
623
+ OIDCConfig.prototype.toJSON = function () {
624
+ return {
625
+ enabled: this.enabled,
626
+ displayName: this.displayName,
627
+ providerId: this.providerId,
628
+ issuer: this.issuer,
629
+ clientId: this.clientId,
630
+ clientSecret: deep_copy_1.deepCopy(this.clientSecret),
631
+ responseType: deep_copy_1.deepCopy(this.responseType),
632
+ };
633
+ };
634
+ return OIDCConfig;
635
+ }());
636
+ exports.OIDCConfig = OIDCConfig;