firebase-admin 10.2.0 → 11.0.1
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.
- package/README.md +1 -1
- package/lib/app/core.d.ts +1 -1
- package/lib/app/core.js +1 -1
- package/lib/app/credential-factory.d.ts +1 -1
- package/lib/app/credential-factory.js +8 -8
- package/lib/app/credential-internal.d.ts +7 -1
- package/lib/app/credential-internal.js +120 -106
- package/lib/app/credential.d.ts +1 -1
- package/lib/app/credential.js +1 -1
- package/lib/app/firebase-app.d.ts +1 -1
- package/lib/app/firebase-app.js +76 -91
- package/lib/app/firebase-namespace.d.ts +1 -1
- package/lib/app/firebase-namespace.js +209 -279
- package/lib/app/index.d.ts +1 -1
- package/lib/app/index.js +4 -4
- package/lib/app/lifecycle.d.ts +1 -1
- package/lib/app/lifecycle.js +37 -43
- package/lib/app-check/app-check-api-client-internal.d.ts +1 -1
- package/lib/app-check/app-check-api-client-internal.js +54 -73
- package/lib/app-check/app-check-api.d.ts +1 -1
- package/lib/app-check/app-check-api.js +1 -1
- package/lib/app-check/app-check-namespace.d.ts +1 -1
- package/lib/app-check/app-check-namespace.js +1 -1
- package/lib/app-check/app-check.d.ts +1 -1
- package/lib/app-check/app-check.js +17 -19
- package/lib/app-check/index.d.ts +1 -1
- package/lib/app-check/index.js +7 -7
- package/lib/app-check/token-generator.d.ts +1 -1
- package/lib/app-check/token-generator.js +47 -52
- package/lib/app-check/token-verifier.d.ts +1 -1
- package/lib/app-check/token-verifier.js +46 -50
- package/lib/auth/action-code-settings-builder.d.ts +1 -1
- package/lib/auth/action-code-settings-builder.js +10 -11
- package/lib/auth/auth-api-request.d.ts +1 -1
- package/lib/auth/auth-api-request.js +346 -393
- package/lib/auth/auth-config.d.ts +1 -1
- package/lib/auth/auth-config.js +95 -106
- package/lib/auth/auth-namespace.d.ts +1 -1
- package/lib/auth/auth-namespace.js +1 -1
- package/lib/auth/auth.d.ts +1 -1
- package/lib/auth/auth.js +20 -40
- package/lib/auth/base-auth.d.ts +1 -1
- package/lib/auth/base-auth.js +137 -144
- package/lib/auth/identifier.d.ts +1 -1
- package/lib/auth/identifier.js +2 -2
- package/lib/auth/index.d.ts +1 -1
- package/lib/auth/index.js +7 -7
- package/lib/auth/tenant-manager.d.ts +1 -1
- package/lib/auth/tenant-manager.js +49 -71
- package/lib/auth/tenant.d.ts +1 -1
- package/lib/auth/tenant.js +44 -55
- package/lib/auth/token-generator.d.ts +1 -1
- package/lib/auth/token-generator.js +49 -53
- package/lib/auth/token-verifier.d.ts +1 -1
- package/lib/auth/token-verifier.js +83 -91
- package/lib/auth/user-import-builder.d.ts +1 -1
- package/lib/auth/user-import-builder.js +70 -73
- package/lib/auth/user-record.d.ts +1 -1
- package/lib/auth/user-record.js +62 -84
- package/lib/credential/index.d.ts +1 -1
- package/lib/credential/index.js +2 -2
- package/lib/database/database-namespace.d.ts +1 -1
- package/lib/database/database-namespace.js +1 -1
- package/lib/database/database.d.ts +1 -1
- package/lib/database/database.js +92 -108
- package/lib/database/index.d.ts +1 -1
- package/lib/database/index.js +10 -10
- package/lib/default-namespace.d.ts +1 -1
- package/lib/default-namespace.js +2 -2
- package/lib/eventarc/cloudevent.d.ts +1 -1
- package/lib/eventarc/cloudevent.js +1 -1
- package/lib/eventarc/eventarc-client-internal.d.ts +1 -1
- package/lib/eventarc/eventarc-client-internal.js +63 -129
- package/lib/eventarc/eventarc-utils.d.ts +1 -1
- package/lib/eventarc/eventarc-utils.js +16 -32
- package/lib/eventarc/eventarc.d.ts +1 -1
- package/lib/eventarc/eventarc.js +51 -65
- package/lib/eventarc/index.d.ts +1 -1
- package/lib/eventarc/index.js +7 -7
- package/lib/firebase-namespace-api.d.ts +1 -1
- package/lib/firebase-namespace-api.js +8 -3
- package/lib/firestore/firestore-internal.d.ts +1 -1
- package/lib/firestore/firestore-internal.js +28 -37
- package/lib/firestore/firestore-namespace.d.ts +9 -1
- package/lib/firestore/firestore-namespace.js +2 -2
- package/lib/firestore/index.d.ts +2 -2
- package/lib/firestore/index.js +7 -7
- package/lib/functions/functions-api-client-internal.d.ts +19 -1
- package/lib/functions/functions-api-client-internal.js +83 -103
- package/lib/functions/functions-api.d.ts +1 -1
- package/lib/functions/functions-api.js +1 -1
- package/lib/functions/functions.d.ts +1 -1
- package/lib/functions/functions.js +13 -15
- package/lib/functions/index.d.ts +1 -1
- package/lib/functions/index.js +7 -7
- package/lib/index.d.ts +1 -1
- package/lib/index.js +14 -4
- package/lib/installations/index.d.ts +1 -1
- package/lib/installations/index.js +6 -6
- package/lib/installations/installations-namespace.d.ts +1 -1
- package/lib/installations/installations-namespace.js +1 -1
- package/lib/installations/installations-request-handler.d.ts +1 -1
- package/lib/installations/installations-request-handler.js +33 -36
- package/lib/installations/installations.d.ts +1 -1
- package/lib/installations/installations.js +17 -22
- package/lib/instance-id/index.d.ts +1 -1
- package/lib/instance-id/index.js +6 -6
- package/lib/instance-id/instance-id-namespace.d.ts +1 -1
- package/lib/instance-id/instance-id-namespace.js +1 -1
- package/lib/instance-id/instance-id.d.ts +1 -1
- package/lib/instance-id/instance-id.js +21 -26
- package/lib/machine-learning/index.d.ts +1 -1
- package/lib/machine-learning/index.js +7 -7
- package/lib/machine-learning/machine-learning-api-client.d.ts +1 -1
- package/lib/machine-learning/machine-learning-api-client.js +110 -124
- package/lib/machine-learning/machine-learning-namespace.d.ts +1 -1
- package/lib/machine-learning/machine-learning-namespace.js +1 -1
- package/lib/machine-learning/machine-learning-utils.d.ts +1 -1
- package/lib/machine-learning/machine-learning-utils.js +9 -24
- package/lib/machine-learning/machine-learning.d.ts +1 -1
- package/lib/machine-learning/machine-learning.js +146 -207
- package/lib/messaging/batch-request-internal.d.ts +1 -1
- package/lib/messaging/batch-request-internal.js +33 -35
- package/lib/messaging/index.d.ts +1 -1
- package/lib/messaging/index.js +7 -7
- package/lib/messaging/messaging-api-request-internal.d.ts +1 -1
- package/lib/messaging/messaging-api-request-internal.js +38 -40
- package/lib/messaging/messaging-api.d.ts +1 -1
- package/lib/messaging/messaging-api.js +1 -1
- package/lib/messaging/messaging-errors-internal.d.ts +1 -1
- package/lib/messaging/messaging-errors-internal.js +12 -13
- package/lib/messaging/messaging-internal.d.ts +1 -1
- package/lib/messaging/messaging-internal.js +51 -51
- package/lib/messaging/messaging-namespace.d.ts +1 -1
- package/lib/messaging/messaging-namespace.js +1 -1
- package/lib/messaging/messaging.d.ts +1 -1
- package/lib/messaging/messaging.js +184 -208
- package/lib/project-management/android-app.d.ts +1 -1
- package/lib/project-management/android-app.js +41 -43
- package/lib/project-management/app-metadata.d.ts +1 -1
- package/lib/project-management/app-metadata.js +1 -1
- package/lib/project-management/index.d.ts +1 -1
- package/lib/project-management/index.js +7 -7
- package/lib/project-management/ios-app.d.ts +1 -1
- package/lib/project-management/ios-app.js +25 -26
- package/lib/project-management/project-management-api-request-internal.d.ts +1 -1
- package/lib/project-management/project-management-api-request-internal.js +84 -89
- package/lib/project-management/project-management-namespace.d.ts +1 -1
- package/lib/project-management/project-management-namespace.js +1 -1
- package/lib/project-management/project-management.d.ts +1 -1
- package/lib/project-management/project-management.js +80 -87
- package/lib/remote-config/index.d.ts +1 -1
- package/lib/remote-config/index.js +7 -7
- package/lib/remote-config/remote-config-api-client-internal.d.ts +1 -1
- package/lib/remote-config/remote-config-api-client-internal.js +116 -154
- package/lib/remote-config/remote-config-api.d.ts +1 -1
- package/lib/remote-config/remote-config-api.js +1 -1
- package/lib/remote-config/remote-config-namespace.d.ts +1 -1
- package/lib/remote-config/remote-config-namespace.js +1 -1
- package/lib/remote-config/remote-config.d.ts +1 -1
- package/lib/remote-config/remote-config.js +51 -59
- package/lib/security-rules/index.d.ts +1 -1
- package/lib/security-rules/index.js +7 -7
- package/lib/security-rules/security-rules-api-client-internal.d.ts +3 -1
- package/lib/security-rules/security-rules-api-client-internal.js +118 -107
- package/lib/security-rules/security-rules-internal.d.ts +1 -1
- package/lib/security-rules/security-rules-internal.js +6 -21
- package/lib/security-rules/security-rules-namespace.d.ts +1 -1
- package/lib/security-rules/security-rules-namespace.js +1 -1
- package/lib/security-rules/security-rules.d.ts +1 -1
- package/lib/security-rules/security-rules.js +83 -92
- package/lib/storage/index.d.ts +1 -1
- package/lib/storage/index.js +7 -7
- package/lib/storage/storage-namespace.d.ts +1 -1
- package/lib/storage/storage-namespace.js +1 -1
- package/lib/storage/storage.d.ts +1 -1
- package/lib/storage/storage.js +25 -30
- package/lib/utils/api-request.d.ts +3 -1
- package/lib/utils/api-request.js +278 -356
- package/lib/utils/crypto-signer.d.ts +1 -1
- package/lib/utils/crypto-signer.js +58 -93
- package/lib/utils/deep-copy.d.ts +1 -1
- package/lib/utils/deep-copy.js +3 -3
- package/lib/utils/error.d.ts +1 -1
- package/lib/utils/error.js +611 -681
- package/lib/utils/index.d.ts +1 -1
- package/lib/utils/index.js +38 -43
- package/lib/utils/jwt.d.ts +1 -1
- package/lib/utils/jwt.js +97 -123
- package/lib/utils/validator.d.ts +1 -1
- package/lib/utils/validator.js +13 -13
- package/package.json +25 -10
package/README.md
CHANGED
|
@@ -59,7 +59,7 @@ requests, code review feedback, and also pull requests.
|
|
|
59
59
|
|
|
60
60
|
## Supported Environments
|
|
61
61
|
|
|
62
|
-
We support Node.js
|
|
62
|
+
We support Node.js 14 and higher.
|
|
63
63
|
|
|
64
64
|
Please also note that the Admin SDK should only
|
|
65
65
|
be used in server-side/back-end environments controlled by the app developer.
|
package/lib/app/core.d.ts
CHANGED
package/lib/app/core.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*! firebase-admin
|
|
1
|
+
/*! firebase-admin v11.0.1 */
|
|
2
2
|
"use strict";
|
|
3
3
|
/*!
|
|
4
4
|
* @license
|
|
@@ -18,10 +18,10 @@
|
|
|
18
18
|
*/
|
|
19
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
20
|
exports.clearGlobalAppDefaultCred = exports.refreshToken = exports.cert = exports.applicationDefault = void 0;
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
const credential_internal_1 = require("./credential-internal");
|
|
22
|
+
let globalAppDefaultCred;
|
|
23
|
+
const globalCertCreds = {};
|
|
24
|
+
const globalRefreshTokenCreds = {};
|
|
25
25
|
/**
|
|
26
26
|
* Returns a credential created from the
|
|
27
27
|
* {@link https://developers.google.com/identity/protocols/application-default-credentials |
|
|
@@ -52,7 +52,7 @@ var globalRefreshTokenCreds = {};
|
|
|
52
52
|
*/
|
|
53
53
|
function applicationDefault(httpAgent) {
|
|
54
54
|
if (typeof globalAppDefaultCred === 'undefined') {
|
|
55
|
-
globalAppDefaultCred = credential_internal_1.getApplicationDefault(httpAgent);
|
|
55
|
+
globalAppDefaultCred = (0, credential_internal_1.getApplicationDefault)(httpAgent);
|
|
56
56
|
}
|
|
57
57
|
return globalAppDefaultCred;
|
|
58
58
|
}
|
|
@@ -98,7 +98,7 @@ exports.applicationDefault = applicationDefault;
|
|
|
98
98
|
* provided service account that can be used to initialize an app.
|
|
99
99
|
*/
|
|
100
100
|
function cert(serviceAccountPathOrObject, httpAgent) {
|
|
101
|
-
|
|
101
|
+
const stringifiedServiceAccount = JSON.stringify(serviceAccountPathOrObject);
|
|
102
102
|
if (!(stringifiedServiceAccount in globalCertCreds)) {
|
|
103
103
|
globalCertCreds[stringifiedServiceAccount] = new credential_internal_1.ServiceAccountCredential(serviceAccountPathOrObject, httpAgent);
|
|
104
104
|
}
|
|
@@ -134,7 +134,7 @@ exports.cert = cert;
|
|
|
134
134
|
* provided service account that can be used to initialize an app.
|
|
135
135
|
*/
|
|
136
136
|
function refreshToken(refreshTokenPathOrObject, httpAgent) {
|
|
137
|
-
|
|
137
|
+
const stringifiedRefreshToken = JSON.stringify(refreshTokenPathOrObject);
|
|
138
138
|
if (!(stringifiedRefreshToken in globalRefreshTokenCreds)) {
|
|
139
139
|
globalRefreshTokenCreds[stringifiedRefreshToken] = new credential_internal_1.RefreshTokenCredential(refreshTokenPathOrObject, httpAgent);
|
|
140
140
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*! firebase-admin
|
|
1
|
+
/*! firebase-admin v11.0.1 */
|
|
2
2
|
/*!
|
|
3
3
|
* @license
|
|
4
4
|
* Copyright 2020 Google Inc.
|
|
@@ -54,6 +54,12 @@ export declare class ComputeEngineCredential implements Credential {
|
|
|
54
54
|
private accountId?;
|
|
55
55
|
constructor(httpAgent?: Agent);
|
|
56
56
|
getAccessToken(): Promise<GoogleOAuthAccessToken>;
|
|
57
|
+
/**
|
|
58
|
+
* getIDToken returns a OIDC token from the compute metadata service
|
|
59
|
+
* that can be used to make authenticated calls to audience
|
|
60
|
+
* @param audience the URL the returned ID token will be used to call.
|
|
61
|
+
*/
|
|
62
|
+
getIDToken(audience: string): Promise<string>;
|
|
57
63
|
getProjectId(): Promise<string>;
|
|
58
64
|
getServiceAccountEmail(): Promise<string>;
|
|
59
65
|
private buildRequest;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*! firebase-admin
|
|
1
|
+
/*! firebase-admin v11.0.1 */
|
|
2
2
|
"use strict";
|
|
3
3
|
/*!
|
|
4
4
|
* @license
|
|
@@ -18,39 +18,40 @@
|
|
|
18
18
|
*/
|
|
19
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
20
|
exports.getApplicationDefault = exports.isApplicationDefault = exports.RefreshTokenCredential = exports.ComputeEngineCredential = exports.ServiceAccountCredential = void 0;
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
21
|
+
const fs = require("fs");
|
|
22
|
+
const os = require("os");
|
|
23
|
+
const path = require("path");
|
|
24
|
+
const error_1 = require("../utils/error");
|
|
25
|
+
const api_request_1 = require("../utils/api-request");
|
|
26
|
+
const util = require("../utils/validator");
|
|
27
|
+
const GOOGLE_TOKEN_AUDIENCE = 'https://accounts.google.com/o/oauth2/token';
|
|
28
|
+
const GOOGLE_AUTH_TOKEN_HOST = 'accounts.google.com';
|
|
29
|
+
const GOOGLE_AUTH_TOKEN_PATH = '/o/oauth2/token';
|
|
30
30
|
// NOTE: the Google Metadata Service uses HTTP over a vlan
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
31
|
+
const GOOGLE_METADATA_SERVICE_HOST = 'metadata.google.internal';
|
|
32
|
+
const GOOGLE_METADATA_SERVICE_TOKEN_PATH = '/computeMetadata/v1/instance/service-accounts/default/token';
|
|
33
|
+
const GOOGLE_METADATA_SERVICE_IDENTITY_PATH = '/computeMetadata/v1/instance/service-accounts/default/identity';
|
|
34
|
+
const GOOGLE_METADATA_SERVICE_PROJECT_ID_PATH = '/computeMetadata/v1/project/project-id';
|
|
35
|
+
const GOOGLE_METADATA_SERVICE_ACCOUNT_ID_PATH = '/computeMetadata/v1/instance/service-accounts/default/email';
|
|
36
|
+
const configDir = (() => {
|
|
36
37
|
// Windows has a dedicated low-rights location for apps at ~/Application Data
|
|
37
|
-
|
|
38
|
+
const sys = os.platform();
|
|
38
39
|
if (sys && sys.length >= 3 && sys.substring(0, 3).toLowerCase() === 'win') {
|
|
39
40
|
return process.env.APPDATA;
|
|
40
41
|
}
|
|
41
42
|
// On *nix the gcloud cli creates a . dir.
|
|
42
43
|
return process.env.HOME && path.resolve(process.env.HOME, '.config');
|
|
43
44
|
})();
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
45
|
+
const GCLOUD_CREDENTIAL_SUFFIX = 'gcloud/application_default_credentials.json';
|
|
46
|
+
const GCLOUD_CREDENTIAL_PATH = configDir && path.resolve(configDir, GCLOUD_CREDENTIAL_SUFFIX);
|
|
47
|
+
const REFRESH_TOKEN_HOST = 'www.googleapis.com';
|
|
48
|
+
const REFRESH_TOKEN_PATH = '/oauth2/v4/token';
|
|
49
|
+
const ONE_HOUR_IN_SECONDS = 60 * 60;
|
|
50
|
+
const JWT_ALGORITHM = 'RS256';
|
|
50
51
|
/**
|
|
51
52
|
* Implementation of Credential that uses a service account.
|
|
52
53
|
*/
|
|
53
|
-
|
|
54
|
+
class ServiceAccountCredential {
|
|
54
55
|
/**
|
|
55
56
|
* Creates a new ServiceAccountCredential from the given parameters.
|
|
56
57
|
*
|
|
@@ -61,11 +62,10 @@ var ServiceAccountCredential = /** @class */ (function () {
|
|
|
61
62
|
*
|
|
62
63
|
* @constructor
|
|
63
64
|
*/
|
|
64
|
-
|
|
65
|
-
if (implicit === void 0) { implicit = false; }
|
|
65
|
+
constructor(serviceAccountPathOrObject, httpAgent, implicit = false) {
|
|
66
66
|
this.httpAgent = httpAgent;
|
|
67
67
|
this.implicit = implicit;
|
|
68
|
-
|
|
68
|
+
const serviceAccount = (typeof serviceAccountPathOrObject === 'string') ?
|
|
69
69
|
ServiceAccount.fromPath(serviceAccountPathOrObject)
|
|
70
70
|
: new ServiceAccount(serviceAccountPathOrObject);
|
|
71
71
|
this.projectId = serviceAccount.projectId;
|
|
@@ -73,13 +73,13 @@ var ServiceAccountCredential = /** @class */ (function () {
|
|
|
73
73
|
this.clientEmail = serviceAccount.clientEmail;
|
|
74
74
|
this.httpClient = new api_request_1.HttpClient();
|
|
75
75
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
76
|
+
getAccessToken() {
|
|
77
|
+
const token = this.createAuthJwt_();
|
|
78
|
+
const postData = 'grant_type=urn%3Aietf%3Aparams%3Aoauth%3A' +
|
|
79
79
|
'grant-type%3Ajwt-bearer&assertion=' + token;
|
|
80
|
-
|
|
80
|
+
const request = {
|
|
81
81
|
method: 'POST',
|
|
82
|
-
url:
|
|
82
|
+
url: `https://${GOOGLE_AUTH_TOKEN_HOST}${GOOGLE_AUTH_TOKEN_PATH}`,
|
|
83
83
|
headers: {
|
|
84
84
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
85
85
|
},
|
|
@@ -87,10 +87,10 @@ var ServiceAccountCredential = /** @class */ (function () {
|
|
|
87
87
|
httpAgent: this.httpAgent,
|
|
88
88
|
};
|
|
89
89
|
return requestAccessToken(this.httpClient, request);
|
|
90
|
-
}
|
|
90
|
+
}
|
|
91
91
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
92
|
-
|
|
93
|
-
|
|
92
|
+
createAuthJwt_() {
|
|
93
|
+
const claims = {
|
|
94
94
|
scope: [
|
|
95
95
|
'https://www.googleapis.com/auth/cloud-platform',
|
|
96
96
|
'https://www.googleapis.com/auth/firebase.database',
|
|
@@ -100,7 +100,7 @@ var ServiceAccountCredential = /** @class */ (function () {
|
|
|
100
100
|
].join(' '),
|
|
101
101
|
};
|
|
102
102
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
103
|
-
|
|
103
|
+
const jwt = require('jsonwebtoken');
|
|
104
104
|
// This method is actually synchronous so we can capture and return the buffer.
|
|
105
105
|
return jwt.sign(claims, this.privateKey, {
|
|
106
106
|
audience: GOOGLE_TOKEN_AUDIENCE,
|
|
@@ -108,22 +108,21 @@ var ServiceAccountCredential = /** @class */ (function () {
|
|
|
108
108
|
issuer: this.clientEmail,
|
|
109
109
|
algorithm: JWT_ALGORITHM,
|
|
110
110
|
});
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
}());
|
|
111
|
+
}
|
|
112
|
+
}
|
|
114
113
|
exports.ServiceAccountCredential = ServiceAccountCredential;
|
|
115
114
|
/**
|
|
116
115
|
* A struct containing the properties necessary to use service account JSON credentials.
|
|
117
116
|
*/
|
|
118
|
-
|
|
119
|
-
|
|
117
|
+
class ServiceAccount {
|
|
118
|
+
constructor(json) {
|
|
120
119
|
if (!util.isNonNullObject(json)) {
|
|
121
120
|
throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, 'Service account must be an object.');
|
|
122
121
|
}
|
|
123
122
|
copyAttr(this, json, 'projectId', 'project_id');
|
|
124
123
|
copyAttr(this, json, 'privateKey', 'private_key');
|
|
125
124
|
copyAttr(this, json, 'clientEmail', 'client_email');
|
|
126
|
-
|
|
125
|
+
let errorMessage;
|
|
127
126
|
if (!util.isNonEmptyString(this.projectId)) {
|
|
128
127
|
errorMessage = 'Service account object must contain a string "project_id" property.';
|
|
129
128
|
}
|
|
@@ -137,7 +136,7 @@ var ServiceAccount = /** @class */ (function () {
|
|
|
137
136
|
throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, errorMessage);
|
|
138
137
|
}
|
|
139
138
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
140
|
-
|
|
139
|
+
const forge = require('node-forge');
|
|
141
140
|
try {
|
|
142
141
|
forge.pki.privateKeyFromPem(this.privateKey);
|
|
143
142
|
}
|
|
@@ -145,7 +144,7 @@ var ServiceAccount = /** @class */ (function () {
|
|
|
145
144
|
throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, 'Failed to parse private key: ' + error);
|
|
146
145
|
}
|
|
147
146
|
}
|
|
148
|
-
|
|
147
|
+
static fromPath(filePath) {
|
|
149
148
|
try {
|
|
150
149
|
return new ServiceAccount(JSON.parse(fs.readFileSync(filePath, 'utf8')));
|
|
151
150
|
}
|
|
@@ -153,72 +152,77 @@ var ServiceAccount = /** @class */ (function () {
|
|
|
153
152
|
// Throw a nicely formed error message if the file contents cannot be parsed
|
|
154
153
|
throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, 'Failed to parse service account json file: ' + error);
|
|
155
154
|
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
}());
|
|
155
|
+
}
|
|
156
|
+
}
|
|
159
157
|
/**
|
|
160
158
|
* Implementation of Credential that gets access tokens from the metadata service available
|
|
161
159
|
* in the Google Cloud Platform. This authenticates the process as the default service account
|
|
162
160
|
* of an App Engine instance or Google Compute Engine machine.
|
|
163
161
|
*/
|
|
164
|
-
|
|
165
|
-
|
|
162
|
+
class ComputeEngineCredential {
|
|
163
|
+
constructor(httpAgent) {
|
|
166
164
|
this.httpClient = new api_request_1.HttpClient();
|
|
167
165
|
this.httpAgent = httpAgent;
|
|
168
166
|
}
|
|
169
|
-
|
|
170
|
-
|
|
167
|
+
getAccessToken() {
|
|
168
|
+
const request = this.buildRequest(GOOGLE_METADATA_SERVICE_TOKEN_PATH);
|
|
171
169
|
return requestAccessToken(this.httpClient, request);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* getIDToken returns a OIDC token from the compute metadata service
|
|
173
|
+
* that can be used to make authenticated calls to audience
|
|
174
|
+
* @param audience the URL the returned ID token will be used to call.
|
|
175
|
+
*/
|
|
176
|
+
getIDToken(audience) {
|
|
177
|
+
const request = this.buildRequest(`${GOOGLE_METADATA_SERVICE_IDENTITY_PATH}?audience=${audience}`);
|
|
178
|
+
return requestIDToken(this.httpClient, request);
|
|
179
|
+
}
|
|
180
|
+
getProjectId() {
|
|
175
181
|
if (this.projectId) {
|
|
176
182
|
return Promise.resolve(this.projectId);
|
|
177
183
|
}
|
|
178
|
-
|
|
184
|
+
const request = this.buildRequest(GOOGLE_METADATA_SERVICE_PROJECT_ID_PATH);
|
|
179
185
|
return this.httpClient.send(request)
|
|
180
|
-
.then(
|
|
181
|
-
|
|
182
|
-
return
|
|
186
|
+
.then((resp) => {
|
|
187
|
+
this.projectId = resp.text;
|
|
188
|
+
return this.projectId;
|
|
183
189
|
})
|
|
184
|
-
.catch(
|
|
185
|
-
|
|
186
|
-
throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL,
|
|
190
|
+
.catch((err) => {
|
|
191
|
+
const detail = (err instanceof api_request_1.HttpError) ? getDetailFromResponse(err.response) : err.message;
|
|
192
|
+
throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, `Failed to determine project ID: ${detail}`);
|
|
187
193
|
});
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
var _this = this;
|
|
194
|
+
}
|
|
195
|
+
getServiceAccountEmail() {
|
|
191
196
|
if (this.accountId) {
|
|
192
197
|
return Promise.resolve(this.accountId);
|
|
193
198
|
}
|
|
194
|
-
|
|
199
|
+
const request = this.buildRequest(GOOGLE_METADATA_SERVICE_ACCOUNT_ID_PATH);
|
|
195
200
|
return this.httpClient.send(request)
|
|
196
|
-
.then(
|
|
197
|
-
|
|
198
|
-
return
|
|
201
|
+
.then((resp) => {
|
|
202
|
+
this.accountId = resp.text;
|
|
203
|
+
return this.accountId;
|
|
199
204
|
})
|
|
200
|
-
.catch(
|
|
201
|
-
|
|
202
|
-
throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL,
|
|
205
|
+
.catch((err) => {
|
|
206
|
+
const detail = (err instanceof api_request_1.HttpError) ? getDetailFromResponse(err.response) : err.message;
|
|
207
|
+
throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, `Failed to determine service account email: ${detail}`);
|
|
203
208
|
});
|
|
204
|
-
}
|
|
205
|
-
|
|
209
|
+
}
|
|
210
|
+
buildRequest(urlPath) {
|
|
206
211
|
return {
|
|
207
212
|
method: 'GET',
|
|
208
|
-
url:
|
|
213
|
+
url: `http://${GOOGLE_METADATA_SERVICE_HOST}${urlPath}`,
|
|
209
214
|
headers: {
|
|
210
215
|
'Metadata-Flavor': 'Google',
|
|
211
216
|
},
|
|
212
217
|
httpAgent: this.httpAgent,
|
|
213
218
|
};
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
}());
|
|
219
|
+
}
|
|
220
|
+
}
|
|
217
221
|
exports.ComputeEngineCredential = ComputeEngineCredential;
|
|
218
222
|
/**
|
|
219
223
|
* Implementation of Credential that gets access tokens from refresh tokens.
|
|
220
224
|
*/
|
|
221
|
-
|
|
225
|
+
class RefreshTokenCredential {
|
|
222
226
|
/**
|
|
223
227
|
* Creates a new RefreshTokenCredential from the given parameters.
|
|
224
228
|
*
|
|
@@ -230,8 +234,7 @@ var RefreshTokenCredential = /** @class */ (function () {
|
|
|
230
234
|
*
|
|
231
235
|
* @constructor
|
|
232
236
|
*/
|
|
233
|
-
|
|
234
|
-
if (implicit === void 0) { implicit = false; }
|
|
237
|
+
constructor(refreshTokenPathOrObject, httpAgent, implicit = false) {
|
|
235
238
|
this.httpAgent = httpAgent;
|
|
236
239
|
this.implicit = implicit;
|
|
237
240
|
this.refreshToken = (typeof refreshTokenPathOrObject === 'string') ?
|
|
@@ -239,14 +242,14 @@ var RefreshTokenCredential = /** @class */ (function () {
|
|
|
239
242
|
: new RefreshToken(refreshTokenPathOrObject);
|
|
240
243
|
this.httpClient = new api_request_1.HttpClient();
|
|
241
244
|
}
|
|
242
|
-
|
|
243
|
-
|
|
245
|
+
getAccessToken() {
|
|
246
|
+
const postData = 'client_id=' + this.refreshToken.clientId + '&' +
|
|
244
247
|
'client_secret=' + this.refreshToken.clientSecret + '&' +
|
|
245
248
|
'refresh_token=' + this.refreshToken.refreshToken + '&' +
|
|
246
249
|
'grant_type=refresh_token';
|
|
247
|
-
|
|
250
|
+
const request = {
|
|
248
251
|
method: 'POST',
|
|
249
|
-
url:
|
|
252
|
+
url: `https://${REFRESH_TOKEN_HOST}${REFRESH_TOKEN_PATH}`,
|
|
250
253
|
headers: {
|
|
251
254
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
252
255
|
},
|
|
@@ -254,17 +257,16 @@ var RefreshTokenCredential = /** @class */ (function () {
|
|
|
254
257
|
httpAgent: this.httpAgent,
|
|
255
258
|
};
|
|
256
259
|
return requestAccessToken(this.httpClient, request);
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
}());
|
|
260
|
+
}
|
|
261
|
+
}
|
|
260
262
|
exports.RefreshTokenCredential = RefreshTokenCredential;
|
|
261
|
-
|
|
262
|
-
|
|
263
|
+
class RefreshToken {
|
|
264
|
+
constructor(json) {
|
|
263
265
|
copyAttr(this, json, 'clientId', 'client_id');
|
|
264
266
|
copyAttr(this, json, 'clientSecret', 'client_secret');
|
|
265
267
|
copyAttr(this, json, 'refreshToken', 'refresh_token');
|
|
266
268
|
copyAttr(this, json, 'type', 'type');
|
|
267
|
-
|
|
269
|
+
let errorMessage;
|
|
268
270
|
if (!util.isNonEmptyString(this.clientId)) {
|
|
269
271
|
errorMessage = 'Refresh token must contain a "client_id" property.';
|
|
270
272
|
}
|
|
@@ -285,7 +287,7 @@ var RefreshToken = /** @class */ (function () {
|
|
|
285
287
|
* Tries to load a RefreshToken from a path. Throws if the path doesn't exist or the
|
|
286
288
|
* data at the path is invalid.
|
|
287
289
|
*/
|
|
288
|
-
|
|
290
|
+
static fromPath(filePath) {
|
|
289
291
|
try {
|
|
290
292
|
return new RefreshToken(JSON.parse(fs.readFileSync(filePath, 'utf8')));
|
|
291
293
|
}
|
|
@@ -293,9 +295,8 @@ var RefreshToken = /** @class */ (function () {
|
|
|
293
295
|
// Throw a nicely formed error message if the file contents cannot be parsed
|
|
294
296
|
throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, 'Failed to parse refresh token file: ' + error);
|
|
295
297
|
}
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
}());
|
|
298
|
+
}
|
|
299
|
+
}
|
|
299
300
|
/**
|
|
300
301
|
* Checks if the given credential was loaded via the application default credentials mechanism. This
|
|
301
302
|
* includes all ComputeEngineCredential instances, and the ServiceAccountCredential and RefreshTokenCredential
|
|
@@ -316,7 +317,7 @@ function getApplicationDefault(httpAgent) {
|
|
|
316
317
|
}
|
|
317
318
|
// It is OK to not have this file. If it is present, it must be valid.
|
|
318
319
|
if (GCLOUD_CREDENTIAL_PATH) {
|
|
319
|
-
|
|
320
|
+
const refreshToken = readCredentialFile(GCLOUD_CREDENTIAL_PATH, true);
|
|
320
321
|
if (refreshToken) {
|
|
321
322
|
return new RefreshTokenCredential(refreshToken, httpAgent, true);
|
|
322
323
|
}
|
|
@@ -336,7 +337,7 @@ exports.getApplicationDefault = getApplicationDefault;
|
|
|
336
337
|
* @param alt - Alternative name of the property to copy.
|
|
337
338
|
*/
|
|
338
339
|
function copyAttr(to, from, key, alt) {
|
|
339
|
-
|
|
340
|
+
const tmp = from[key] || from[alt];
|
|
340
341
|
if (typeof tmp !== 'undefined') {
|
|
341
342
|
to[key] = tmp;
|
|
342
343
|
}
|
|
@@ -345,13 +346,26 @@ function copyAttr(to, from, key, alt) {
|
|
|
345
346
|
* Obtain a new OAuth2 token by making a remote service call.
|
|
346
347
|
*/
|
|
347
348
|
function requestAccessToken(client, request) {
|
|
348
|
-
return client.send(request).then(
|
|
349
|
-
|
|
349
|
+
return client.send(request).then((resp) => {
|
|
350
|
+
const json = resp.data;
|
|
350
351
|
if (!json.access_token || !json.expires_in) {
|
|
351
|
-
throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL,
|
|
352
|
+
throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, `Unexpected response while fetching access token: ${JSON.stringify(json)}`);
|
|
352
353
|
}
|
|
353
354
|
return json;
|
|
354
|
-
}).catch(
|
|
355
|
+
}).catch((err) => {
|
|
356
|
+
throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, getErrorMessage(err));
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Obtain a new OIDC token by making a remote service call.
|
|
361
|
+
*/
|
|
362
|
+
function requestIDToken(client, request) {
|
|
363
|
+
return client.send(request).then((resp) => {
|
|
364
|
+
if (!resp.text) {
|
|
365
|
+
throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, 'Unexpected response while fetching id token: response.text is undefined');
|
|
366
|
+
}
|
|
367
|
+
return resp.text;
|
|
368
|
+
}).catch((err) => {
|
|
355
369
|
throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, getErrorMessage(err));
|
|
356
370
|
});
|
|
357
371
|
}
|
|
@@ -359,8 +373,8 @@ function requestAccessToken(client, request) {
|
|
|
359
373
|
* Constructs a human-readable error message from the given Error.
|
|
360
374
|
*/
|
|
361
375
|
function getErrorMessage(err) {
|
|
362
|
-
|
|
363
|
-
return
|
|
376
|
+
const detail = (err instanceof api_request_1.HttpError) ? getDetailFromResponse(err.response) : err.message;
|
|
377
|
+
return `Error fetching access token: ${detail}`;
|
|
364
378
|
}
|
|
365
379
|
/**
|
|
366
380
|
* Extracts details from the given HTTP error response, and returns a human-readable description. If
|
|
@@ -369,8 +383,8 @@ function getErrorMessage(err) {
|
|
|
369
383
|
*/
|
|
370
384
|
function getDetailFromResponse(response) {
|
|
371
385
|
if (response.isJson() && response.data.error) {
|
|
372
|
-
|
|
373
|
-
|
|
386
|
+
const json = response.data;
|
|
387
|
+
let detail = json.error;
|
|
374
388
|
if (json.error_description) {
|
|
375
389
|
detail += ' (' + json.error_description + ')';
|
|
376
390
|
}
|
|
@@ -379,7 +393,7 @@ function getDetailFromResponse(response) {
|
|
|
379
393
|
return response.text || 'Missing error payload';
|
|
380
394
|
}
|
|
381
395
|
function credentialFromFile(filePath, httpAgent) {
|
|
382
|
-
|
|
396
|
+
const credentialsFile = readCredentialFile(filePath);
|
|
383
397
|
if (typeof credentialsFile !== 'object' || credentialsFile === null) {
|
|
384
398
|
throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, 'Failed to parse contents of the credentials file as an object');
|
|
385
399
|
}
|
|
@@ -392,7 +406,7 @@ function credentialFromFile(filePath, httpAgent) {
|
|
|
392
406
|
throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, 'Invalid contents in the credentials file');
|
|
393
407
|
}
|
|
394
408
|
function readCredentialFile(filePath, ignoreMissing) {
|
|
395
|
-
|
|
409
|
+
let fileText;
|
|
396
410
|
try {
|
|
397
411
|
fileText = fs.readFileSync(filePath, 'utf8');
|
|
398
412
|
}
|
|
@@ -400,7 +414,7 @@ function readCredentialFile(filePath, ignoreMissing) {
|
|
|
400
414
|
if (ignoreMissing) {
|
|
401
415
|
return null;
|
|
402
416
|
}
|
|
403
|
-
throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL,
|
|
417
|
+
throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, `Failed to read credentials from file ${filePath}: ` + error);
|
|
404
418
|
}
|
|
405
419
|
try {
|
|
406
420
|
return JSON.parse(fileText);
|
package/lib/app/credential.d.ts
CHANGED
package/lib/app/credential.js
CHANGED