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,391 @@
1
+ /*! firebase-admin v9.12.0 */
2
+ "use strict";
3
+ /*!
4
+ * Copyright 2020 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.getApplicationDefault = exports.isApplicationDefault = exports.RefreshTokenCredential = exports.ComputeEngineCredential = exports.ServiceAccountCredential = void 0;
20
+ var fs = require("fs");
21
+ var os = require("os");
22
+ var path = require("path");
23
+ var error_1 = require("../utils/error");
24
+ var api_request_1 = require("../utils/api-request");
25
+ var util = require("../utils/validator");
26
+ var GOOGLE_TOKEN_AUDIENCE = 'https://accounts.google.com/o/oauth2/token';
27
+ var GOOGLE_AUTH_TOKEN_HOST = 'accounts.google.com';
28
+ var GOOGLE_AUTH_TOKEN_PATH = '/o/oauth2/token';
29
+ // NOTE: the Google Metadata Service uses HTTP over a vlan
30
+ var GOOGLE_METADATA_SERVICE_HOST = 'metadata.google.internal';
31
+ var GOOGLE_METADATA_SERVICE_TOKEN_PATH = '/computeMetadata/v1/instance/service-accounts/default/token';
32
+ var GOOGLE_METADATA_SERVICE_PROJECT_ID_PATH = '/computeMetadata/v1/project/project-id';
33
+ var configDir = (function () {
34
+ // Windows has a dedicated low-rights location for apps at ~/Application Data
35
+ var sys = os.platform();
36
+ if (sys && sys.length >= 3 && sys.substring(0, 3).toLowerCase() === 'win') {
37
+ return process.env.APPDATA;
38
+ }
39
+ // On *nix the gcloud cli creates a . dir.
40
+ return process.env.HOME && path.resolve(process.env.HOME, '.config');
41
+ })();
42
+ var GCLOUD_CREDENTIAL_SUFFIX = 'gcloud/application_default_credentials.json';
43
+ var GCLOUD_CREDENTIAL_PATH = configDir && path.resolve(configDir, GCLOUD_CREDENTIAL_SUFFIX);
44
+ var REFRESH_TOKEN_HOST = 'www.googleapis.com';
45
+ var REFRESH_TOKEN_PATH = '/oauth2/v4/token';
46
+ var ONE_HOUR_IN_SECONDS = 60 * 60;
47
+ var JWT_ALGORITHM = 'RS256';
48
+ /**
49
+ * Implementation of Credential that uses a service account.
50
+ */
51
+ var ServiceAccountCredential = /** @class */ (function () {
52
+ /**
53
+ * Creates a new ServiceAccountCredential from the given parameters.
54
+ *
55
+ * @param serviceAccountPathOrObject Service account json object or path to a service account json file.
56
+ * @param httpAgent Optional http.Agent to use when calling the remote token server.
57
+ * @param implicit An optinal boolean indicating whether this credential was implicitly discovered from the
58
+ * environment, as opposed to being explicitly specified by the developer.
59
+ *
60
+ * @constructor
61
+ */
62
+ function ServiceAccountCredential(serviceAccountPathOrObject, httpAgent, implicit) {
63
+ if (implicit === void 0) { implicit = false; }
64
+ this.httpAgent = httpAgent;
65
+ this.implicit = implicit;
66
+ var serviceAccount = (typeof serviceAccountPathOrObject === 'string') ?
67
+ ServiceAccount.fromPath(serviceAccountPathOrObject)
68
+ : new ServiceAccount(serviceAccountPathOrObject);
69
+ this.projectId = serviceAccount.projectId;
70
+ this.privateKey = serviceAccount.privateKey;
71
+ this.clientEmail = serviceAccount.clientEmail;
72
+ this.httpClient = new api_request_1.HttpClient();
73
+ }
74
+ ServiceAccountCredential.prototype.getAccessToken = function () {
75
+ var token = this.createAuthJwt_();
76
+ var postData = 'grant_type=urn%3Aietf%3Aparams%3Aoauth%3A' +
77
+ 'grant-type%3Ajwt-bearer&assertion=' + token;
78
+ var request = {
79
+ method: 'POST',
80
+ url: "https://" + GOOGLE_AUTH_TOKEN_HOST + GOOGLE_AUTH_TOKEN_PATH,
81
+ headers: {
82
+ 'Content-Type': 'application/x-www-form-urlencoded',
83
+ },
84
+ data: postData,
85
+ httpAgent: this.httpAgent,
86
+ };
87
+ return requestAccessToken(this.httpClient, request);
88
+ };
89
+ ServiceAccountCredential.prototype.createAuthJwt_ = function () {
90
+ var claims = {
91
+ scope: [
92
+ 'https://www.googleapis.com/auth/cloud-platform',
93
+ 'https://www.googleapis.com/auth/firebase.database',
94
+ 'https://www.googleapis.com/auth/firebase.messaging',
95
+ 'https://www.googleapis.com/auth/identitytoolkit',
96
+ 'https://www.googleapis.com/auth/userinfo.email',
97
+ ].join(' '),
98
+ };
99
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
100
+ var jwt = require('jsonwebtoken');
101
+ // This method is actually synchronous so we can capture and return the buffer.
102
+ return jwt.sign(claims, this.privateKey, {
103
+ audience: GOOGLE_TOKEN_AUDIENCE,
104
+ expiresIn: ONE_HOUR_IN_SECONDS,
105
+ issuer: this.clientEmail,
106
+ algorithm: JWT_ALGORITHM,
107
+ });
108
+ };
109
+ return ServiceAccountCredential;
110
+ }());
111
+ exports.ServiceAccountCredential = ServiceAccountCredential;
112
+ /**
113
+ * A struct containing the properties necessary to use service account JSON credentials.
114
+ */
115
+ var ServiceAccount = /** @class */ (function () {
116
+ function ServiceAccount(json) {
117
+ if (!util.isNonNullObject(json)) {
118
+ throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, 'Service account must be an object.');
119
+ }
120
+ copyAttr(this, json, 'projectId', 'project_id');
121
+ copyAttr(this, json, 'privateKey', 'private_key');
122
+ copyAttr(this, json, 'clientEmail', 'client_email');
123
+ var errorMessage;
124
+ if (!util.isNonEmptyString(this.projectId)) {
125
+ errorMessage = 'Service account object must contain a string "project_id" property.';
126
+ }
127
+ else if (!util.isNonEmptyString(this.privateKey)) {
128
+ errorMessage = 'Service account object must contain a string "private_key" property.';
129
+ }
130
+ else if (!util.isNonEmptyString(this.clientEmail)) {
131
+ errorMessage = 'Service account object must contain a string "client_email" property.';
132
+ }
133
+ if (typeof errorMessage !== 'undefined') {
134
+ throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, errorMessage);
135
+ }
136
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
137
+ var forge = require('node-forge');
138
+ try {
139
+ forge.pki.privateKeyFromPem(this.privateKey);
140
+ }
141
+ catch (error) {
142
+ throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, 'Failed to parse private key: ' + error);
143
+ }
144
+ }
145
+ ServiceAccount.fromPath = function (filePath) {
146
+ try {
147
+ return new ServiceAccount(JSON.parse(fs.readFileSync(filePath, 'utf8')));
148
+ }
149
+ catch (error) {
150
+ // Throw a nicely formed error message if the file contents cannot be parsed
151
+ throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, 'Failed to parse service account json file: ' + error);
152
+ }
153
+ };
154
+ return ServiceAccount;
155
+ }());
156
+ /**
157
+ * Implementation of Credential that gets access tokens from the metadata service available
158
+ * in the Google Cloud Platform. This authenticates the process as the default service account
159
+ * of an App Engine instance or Google Compute Engine machine.
160
+ */
161
+ var ComputeEngineCredential = /** @class */ (function () {
162
+ function ComputeEngineCredential(httpAgent) {
163
+ this.httpClient = new api_request_1.HttpClient();
164
+ this.httpAgent = httpAgent;
165
+ }
166
+ ComputeEngineCredential.prototype.getAccessToken = function () {
167
+ var request = this.buildRequest(GOOGLE_METADATA_SERVICE_TOKEN_PATH);
168
+ return requestAccessToken(this.httpClient, request);
169
+ };
170
+ ComputeEngineCredential.prototype.getProjectId = function () {
171
+ var _this = this;
172
+ if (this.projectId) {
173
+ return Promise.resolve(this.projectId);
174
+ }
175
+ var request = this.buildRequest(GOOGLE_METADATA_SERVICE_PROJECT_ID_PATH);
176
+ return this.httpClient.send(request)
177
+ .then(function (resp) {
178
+ _this.projectId = resp.text;
179
+ return _this.projectId;
180
+ })
181
+ .catch(function (err) {
182
+ var detail = (err instanceof api_request_1.HttpError) ? getDetailFromResponse(err.response) : err.message;
183
+ throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, "Failed to determine project ID: " + detail);
184
+ });
185
+ };
186
+ ComputeEngineCredential.prototype.buildRequest = function (urlPath) {
187
+ return {
188
+ method: 'GET',
189
+ url: "http://" + GOOGLE_METADATA_SERVICE_HOST + urlPath,
190
+ headers: {
191
+ 'Metadata-Flavor': 'Google',
192
+ },
193
+ httpAgent: this.httpAgent,
194
+ };
195
+ };
196
+ return ComputeEngineCredential;
197
+ }());
198
+ exports.ComputeEngineCredential = ComputeEngineCredential;
199
+ /**
200
+ * Implementation of Credential that gets access tokens from refresh tokens.
201
+ */
202
+ var RefreshTokenCredential = /** @class */ (function () {
203
+ /**
204
+ * Creates a new RefreshTokenCredential from the given parameters.
205
+ *
206
+ * @param refreshTokenPathOrObject Refresh token json object or path to a refresh token (user credentials) json file.
207
+ * @param httpAgent Optional http.Agent to use when calling the remote token server.
208
+ * @param implicit An optinal boolean indicating whether this credential was implicitly discovered from the
209
+ * environment, as opposed to being explicitly specified by the developer.
210
+ *
211
+ * @constructor
212
+ */
213
+ function RefreshTokenCredential(refreshTokenPathOrObject, httpAgent, implicit) {
214
+ if (implicit === void 0) { implicit = false; }
215
+ this.httpAgent = httpAgent;
216
+ this.implicit = implicit;
217
+ this.refreshToken = (typeof refreshTokenPathOrObject === 'string') ?
218
+ RefreshToken.fromPath(refreshTokenPathOrObject)
219
+ : new RefreshToken(refreshTokenPathOrObject);
220
+ this.httpClient = new api_request_1.HttpClient();
221
+ }
222
+ RefreshTokenCredential.prototype.getAccessToken = function () {
223
+ var postData = 'client_id=' + this.refreshToken.clientId + '&' +
224
+ 'client_secret=' + this.refreshToken.clientSecret + '&' +
225
+ 'refresh_token=' + this.refreshToken.refreshToken + '&' +
226
+ 'grant_type=refresh_token';
227
+ var request = {
228
+ method: 'POST',
229
+ url: "https://" + REFRESH_TOKEN_HOST + REFRESH_TOKEN_PATH,
230
+ headers: {
231
+ 'Content-Type': 'application/x-www-form-urlencoded',
232
+ },
233
+ data: postData,
234
+ httpAgent: this.httpAgent,
235
+ };
236
+ return requestAccessToken(this.httpClient, request);
237
+ };
238
+ return RefreshTokenCredential;
239
+ }());
240
+ exports.RefreshTokenCredential = RefreshTokenCredential;
241
+ var RefreshToken = /** @class */ (function () {
242
+ function RefreshToken(json) {
243
+ copyAttr(this, json, 'clientId', 'client_id');
244
+ copyAttr(this, json, 'clientSecret', 'client_secret');
245
+ copyAttr(this, json, 'refreshToken', 'refresh_token');
246
+ copyAttr(this, json, 'type', 'type');
247
+ var errorMessage;
248
+ if (!util.isNonEmptyString(this.clientId)) {
249
+ errorMessage = 'Refresh token must contain a "client_id" property.';
250
+ }
251
+ else if (!util.isNonEmptyString(this.clientSecret)) {
252
+ errorMessage = 'Refresh token must contain a "client_secret" property.';
253
+ }
254
+ else if (!util.isNonEmptyString(this.refreshToken)) {
255
+ errorMessage = 'Refresh token must contain a "refresh_token" property.';
256
+ }
257
+ else if (!util.isNonEmptyString(this.type)) {
258
+ errorMessage = 'Refresh token must contain a "type" property.';
259
+ }
260
+ if (typeof errorMessage !== 'undefined') {
261
+ throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, errorMessage);
262
+ }
263
+ }
264
+ /*
265
+ * Tries to load a RefreshToken from a path. Throws if the path doesn't exist or the
266
+ * data at the path is invalid.
267
+ */
268
+ RefreshToken.fromPath = function (filePath) {
269
+ try {
270
+ return new RefreshToken(JSON.parse(fs.readFileSync(filePath, 'utf8')));
271
+ }
272
+ catch (error) {
273
+ // Throw a nicely formed error message if the file contents cannot be parsed
274
+ throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, 'Failed to parse refresh token file: ' + error);
275
+ }
276
+ };
277
+ return RefreshToken;
278
+ }());
279
+ /**
280
+ * Checks if the given credential was loaded via the application default credentials mechanism. This
281
+ * includes all ComputeEngineCredential instances, and the ServiceAccountCredential and RefreshTokenCredential
282
+ * instances that were loaded from well-known files or environment variables, rather than being explicitly
283
+ * instantiated.
284
+ *
285
+ * @param credential The credential instance to check.
286
+ */
287
+ function isApplicationDefault(credential) {
288
+ return credential instanceof ComputeEngineCredential ||
289
+ (credential instanceof ServiceAccountCredential && credential.implicit) ||
290
+ (credential instanceof RefreshTokenCredential && credential.implicit);
291
+ }
292
+ exports.isApplicationDefault = isApplicationDefault;
293
+ function getApplicationDefault(httpAgent) {
294
+ if (process.env.GOOGLE_APPLICATION_CREDENTIALS) {
295
+ return credentialFromFile(process.env.GOOGLE_APPLICATION_CREDENTIALS, httpAgent);
296
+ }
297
+ // It is OK to not have this file. If it is present, it must be valid.
298
+ if (GCLOUD_CREDENTIAL_PATH) {
299
+ var refreshToken = readCredentialFile(GCLOUD_CREDENTIAL_PATH, true);
300
+ if (refreshToken) {
301
+ return new RefreshTokenCredential(refreshToken, httpAgent, true);
302
+ }
303
+ }
304
+ return new ComputeEngineCredential(httpAgent);
305
+ }
306
+ exports.getApplicationDefault = getApplicationDefault;
307
+ /**
308
+ * Copies the specified property from one object to another.
309
+ *
310
+ * If no property exists by the given "key", looks for a property identified by "alt", and copies it instead.
311
+ * This can be used to implement behaviors such as "copy property myKey or my_key".
312
+ *
313
+ * @param to Target object to copy the property into.
314
+ * @param from Source object to copy the property from.
315
+ * @param key Name of the property to copy.
316
+ * @param alt Alternative name of the property to copy.
317
+ */
318
+ function copyAttr(to, from, key, alt) {
319
+ var tmp = from[key] || from[alt];
320
+ if (typeof tmp !== 'undefined') {
321
+ to[key] = tmp;
322
+ }
323
+ }
324
+ /**
325
+ * Obtain a new OAuth2 token by making a remote service call.
326
+ */
327
+ function requestAccessToken(client, request) {
328
+ return client.send(request).then(function (resp) {
329
+ var json = resp.data;
330
+ if (!json.access_token || !json.expires_in) {
331
+ throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, "Unexpected response while fetching access token: " + JSON.stringify(json));
332
+ }
333
+ return json;
334
+ }).catch(function (err) {
335
+ throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, getErrorMessage(err));
336
+ });
337
+ }
338
+ /**
339
+ * Constructs a human-readable error message from the given Error.
340
+ */
341
+ function getErrorMessage(err) {
342
+ var detail = (err instanceof api_request_1.HttpError) ? getDetailFromResponse(err.response) : err.message;
343
+ return "Error fetching access token: " + detail;
344
+ }
345
+ /**
346
+ * Extracts details from the given HTTP error response, and returns a human-readable description. If
347
+ * the response is JSON-formatted, looks up the error and error_description fields sent by the
348
+ * Google Auth servers. Otherwise returns the entire response payload as the error detail.
349
+ */
350
+ function getDetailFromResponse(response) {
351
+ if (response.isJson() && response.data.error) {
352
+ var json = response.data;
353
+ var detail = json.error;
354
+ if (json.error_description) {
355
+ detail += ' (' + json.error_description + ')';
356
+ }
357
+ return detail;
358
+ }
359
+ return response.text || 'Missing error payload';
360
+ }
361
+ function credentialFromFile(filePath, httpAgent) {
362
+ var credentialsFile = readCredentialFile(filePath);
363
+ if (typeof credentialsFile !== 'object' || credentialsFile === null) {
364
+ throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, 'Failed to parse contents of the credentials file as an object');
365
+ }
366
+ if (credentialsFile.type === 'service_account') {
367
+ return new ServiceAccountCredential(credentialsFile, httpAgent, true);
368
+ }
369
+ if (credentialsFile.type === 'authorized_user') {
370
+ return new RefreshTokenCredential(credentialsFile, httpAgent, true);
371
+ }
372
+ throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, 'Invalid contents in the credentials file');
373
+ }
374
+ function readCredentialFile(filePath, ignoreMissing) {
375
+ var fileText;
376
+ try {
377
+ fileText = fs.readFileSync(filePath, 'utf8');
378
+ }
379
+ catch (error) {
380
+ if (ignoreMissing) {
381
+ return null;
382
+ }
383
+ throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, "Failed to read credentials from file " + filePath + ": " + error);
384
+ }
385
+ try {
386
+ return JSON.parse(fileText);
387
+ }
388
+ catch (error) {
389
+ throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, 'Failed to parse contents of the credentials file as an object: ' + error);
390
+ }
391
+ }
@@ -0,0 +1,44 @@
1
+ /*! firebase-admin v9.12.0 */
2
+ "use strict";
3
+ /*!
4
+ * @license
5
+ * Copyright 2017 Google Inc.
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.refreshToken = exports.cert = exports.applicationDefault = void 0;
21
+ var credential_internal_1 = require("./credential-internal");
22
+ var globalAppDefaultCred;
23
+ var globalCertCreds = {};
24
+ var globalRefreshTokenCreds = {};
25
+ exports.applicationDefault = function (httpAgent) {
26
+ if (typeof globalAppDefaultCred === 'undefined') {
27
+ globalAppDefaultCred = credential_internal_1.getApplicationDefault(httpAgent);
28
+ }
29
+ return globalAppDefaultCred;
30
+ };
31
+ exports.cert = function (serviceAccountPathOrObject, httpAgent) {
32
+ var stringifiedServiceAccount = JSON.stringify(serviceAccountPathOrObject);
33
+ if (!(stringifiedServiceAccount in globalCertCreds)) {
34
+ globalCertCreds[stringifiedServiceAccount] = new credential_internal_1.ServiceAccountCredential(serviceAccountPathOrObject, httpAgent);
35
+ }
36
+ return globalCertCreds[stringifiedServiceAccount];
37
+ };
38
+ exports.refreshToken = function (refreshTokenPathOrObject, httpAgent) {
39
+ var stringifiedRefreshToken = JSON.stringify(refreshTokenPathOrObject);
40
+ if (!(stringifiedRefreshToken in globalRefreshTokenCreds)) {
41
+ globalRefreshTokenCreds[stringifiedRefreshToken] = new credential_internal_1.RefreshTokenCredential(refreshTokenPathOrObject, httpAgent);
42
+ }
43
+ return globalRefreshTokenCreds[stringifiedRefreshToken];
44
+ };
@@ -0,0 +1,169 @@
1
+ /*! firebase-admin v9.12.0 */
2
+ /*!
3
+ * Copyright 2020 Google Inc.
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ /// <reference types="node" />
18
+ import { Agent } from 'http';
19
+ export interface ServiceAccount {
20
+ projectId?: string;
21
+ clientEmail?: string;
22
+ privateKey?: string;
23
+ }
24
+ /**
25
+ * Interface for Google OAuth 2.0 access tokens.
26
+ */
27
+ export interface GoogleOAuthAccessToken {
28
+ access_token: string;
29
+ expires_in: number;
30
+ }
31
+ export declare namespace credential {
32
+ /**
33
+ * Interface that provides Google OAuth2 access tokens used to authenticate
34
+ * with Firebase services.
35
+ *
36
+ * In most cases, you will not need to implement this yourself and can instead
37
+ * use the default implementations provided by
38
+ * {@link credential `admin.credential`}.
39
+ */
40
+ interface Credential {
41
+ /**
42
+ * Returns a Google OAuth2 access token object used to authenticate with
43
+ * Firebase services.
44
+ *
45
+ * This object contains the following properties:
46
+ * * `access_token` (`string`): The actual Google OAuth2 access token.
47
+ * * `expires_in` (`number`): The number of seconds from when the token was
48
+ * issued that it expires.
49
+ *
50
+ * @return A Google OAuth2 access token object.
51
+ */
52
+ getAccessToken(): Promise<GoogleOAuthAccessToken>;
53
+ }
54
+ /**
55
+ * Returns a credential created from the
56
+ * {@link
57
+ * https://developers.google.com/identity/protocols/application-default-credentials
58
+ * Google Application Default Credentials}
59
+ * that grants admin access to Firebase services. This credential can be used
60
+ * in the call to
61
+ * {@link
62
+ * https://firebase.google.com/docs/reference/admin/node/admin#.initializeApp
63
+ * `admin.initializeApp()`}.
64
+ *
65
+ * Google Application Default Credentials are available on any Google
66
+ * infrastructure, such as Google App Engine and Google Compute Engine.
67
+ *
68
+ * See
69
+ * {@link
70
+ * https://firebase.google.com/docs/admin/setup#initialize_the_sdk
71
+ * Initialize the SDK}
72
+ * for more details.
73
+ *
74
+ * @example
75
+ * ```javascript
76
+ * admin.initializeApp({
77
+ * credential: admin.credential.applicationDefault(),
78
+ * databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
79
+ * });
80
+ * ```
81
+ *
82
+ * @param {!Object=} httpAgent Optional [HTTP Agent](https://nodejs.org/api/http.html#http_class_http_agent)
83
+ * to be used when retrieving access tokens from Google token servers.
84
+ *
85
+ * @return {!admin.credential.Credential} A credential authenticated via Google
86
+ * Application Default Credentials that can be used to initialize an app.
87
+ */
88
+ function applicationDefault(httpAgent?: Agent): Credential;
89
+ /**
90
+ * Returns a credential created from the provided service account that grants
91
+ * admin access to Firebase services. This credential can be used in the call
92
+ * to
93
+ * {@link
94
+ * https://firebase.google.com/docs/reference/admin/node/admin#.initializeApp
95
+ * `admin.initializeApp()`}.
96
+ *
97
+ * See
98
+ * {@link
99
+ * https://firebase.google.com/docs/admin/setup#initialize_the_sdk
100
+ * Initialize the SDK}
101
+ * for more details.
102
+ *
103
+ * @example
104
+ * ```javascript
105
+ * // Providing a path to a service account key JSON file
106
+ * var serviceAccount = require("path/to/serviceAccountKey.json");
107
+ * admin.initializeApp({
108
+ * credential: admin.credential.cert(serviceAccount),
109
+ * databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
110
+ * });
111
+ * ```
112
+ *
113
+ * @example
114
+ * ```javascript
115
+ * // Providing a service account object inline
116
+ * admin.initializeApp({
117
+ * credential: admin.credential.cert({
118
+ * projectId: "<PROJECT_ID>",
119
+ * clientEmail: "foo@<PROJECT_ID>.iam.gserviceaccount.com",
120
+ * privateKey: "-----BEGIN PRIVATE KEY-----<KEY>-----END PRIVATE KEY-----\n"
121
+ * }),
122
+ * databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
123
+ * });
124
+ * ```
125
+ *
126
+ * @param serviceAccountPathOrObject The path to a service
127
+ * account key JSON file or an object representing a service account key.
128
+ * @param httpAgent Optional [HTTP Agent](https://nodejs.org/api/http.html#http_class_http_agent)
129
+ * to be used when retrieving access tokens from Google token servers.
130
+ *
131
+ * @return A credential authenticated via the
132
+ * provided service account that can be used to initialize an app.
133
+ */
134
+ function cert(serviceAccountPathOrObject: string | ServiceAccount, httpAgent?: Agent): Credential;
135
+ /**
136
+ * Returns a credential created from the provided refresh token that grants
137
+ * admin access to Firebase services. This credential can be used in the call
138
+ * to
139
+ * {@link
140
+ * https://firebase.google.com/docs/reference/admin/node/admin#.initializeApp
141
+ * `admin.initializeApp()`}.
142
+ *
143
+ * See
144
+ * {@link
145
+ * https://firebase.google.com/docs/admin/setup#initialize_the_sdk
146
+ * Initialize the SDK}
147
+ * for more details.
148
+ *
149
+ * @example
150
+ * ```javascript
151
+ * // Providing a path to a refresh token JSON file
152
+ * var refreshToken = require("path/to/refreshToken.json");
153
+ * admin.initializeApp({
154
+ * credential: admin.credential.refreshToken(refreshToken),
155
+ * databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
156
+ * });
157
+ * ```
158
+ *
159
+ * @param refreshTokenPathOrObject The path to a Google
160
+ * OAuth2 refresh token JSON file or an object representing a Google OAuth2
161
+ * refresh token.
162
+ * @param httpAgent Optional [HTTP Agent](https://nodejs.org/api/http.html#http_class_http_agent)
163
+ * to be used when retrieving access tokens from Google token servers.
164
+ *
165
+ * @return A credential authenticated via the
166
+ * provided service account that can be used to initialize an app.
167
+ */
168
+ function refreshToken(refreshTokenPathOrObject: string | object, httpAgent?: Agent): Credential;
169
+ }
@@ -0,0 +1,23 @@
1
+ /*! firebase-admin v9.12.0 */
2
+ "use strict";
3
+ /*!
4
+ * Copyright 2020 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.credential = void 0;
20
+ /* eslint-disable @typescript-eslint/no-namespace */
21
+ var credential;
22
+ (function (credential) {
23
+ })(credential = exports.credential || (exports.credential = {}));