firebase-admin 9.100.0-alpha.0 → 10.0.2
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 +7 -3
- package/lib/app/core.d.ts +15 -22
- package/lib/app/core.js +1 -1
- package/lib/app/credential-factory.d.ts +17 -33
- package/lib/app/credential-factory.js +17 -33
- package/lib/app/credential-internal.d.ts +10 -9
- package/lib/app/credential-internal.js +15 -13
- package/lib/app/credential.d.ts +3 -9
- package/lib/app/credential.js +1 -1
- package/lib/app/firebase-app.d.ts +6 -30
- package/lib/app/firebase-app.js +72 -216
- package/lib/app/firebase-namespace.d.ts +28 -34
- package/lib/app/firebase-namespace.js +119 -99
- package/lib/app/index.d.ts +6 -1
- package/lib/app/index.js +1 -1
- package/lib/app/lifecycle.d.ts +24 -3
- package/lib/app/lifecycle.js +119 -25
- package/lib/app-check/app-check-api-client-internal.d.ts +32 -0
- package/lib/app-check/app-check-api-client-internal.js +197 -0
- package/lib/app-check/app-check-api.d.ts +95 -0
- package/lib/app-check/app-check-api.js +19 -0
- package/lib/app-check/app-check-namespace.d.ts +65 -0
- package/lib/app-check/app-check-namespace.js +18 -0
- package/lib/app-check/app-check.d.ts +49 -0
- package/lib/app-check/app-check.js +82 -0
- package/lib/app-check/index.d.ts +53 -0
- package/lib/app-check/index.js +63 -0
- package/lib/app-check/token-generator.d.ts +25 -0
- package/lib/app-check/token-generator.js +159 -0
- package/lib/app-check/token-verifier.d.ts +17 -0
- package/lib/app-check/token-verifier.js +151 -0
- package/lib/auth/action-code-settings-builder.d.ts +1 -1
- package/lib/auth/action-code-settings-builder.js +2 -2
- package/lib/auth/auth-api-request.d.ts +28 -32
- package/lib/auth/auth-api-request.js +167 -125
- package/lib/auth/auth-config.d.ts +123 -21
- package/lib/auth/auth-config.js +85 -34
- package/lib/auth/auth-namespace.d.ts +157 -4
- package/lib/auth/auth-namespace.js +1 -1
- package/lib/auth/auth.d.ts +5 -3
- package/lib/auth/auth.js +6 -4
- package/lib/auth/base-auth.d.ts +134 -116
- package/lib/auth/base-auth.js +213 -143
- package/lib/auth/identifier.d.ts +5 -5
- package/lib/auth/identifier.js +1 -1
- package/lib/auth/index.d.ts +10 -5
- package/lib/auth/index.js +9 -4
- package/lib/auth/tenant-manager.d.ts +19 -19
- package/lib/auth/tenant-manager.js +21 -25
- package/lib/auth/tenant.d.ts +14 -5
- package/lib/auth/tenant.js +19 -11
- package/lib/auth/token-generator.d.ts +7 -108
- package/lib/auth/token-generator.js +52 -145
- package/lib/auth/token-verifier.d.ts +3 -4
- package/lib/auth/token-verifier.js +96 -145
- package/lib/auth/user-import-builder.d.ts +11 -11
- package/lib/auth/user-import-builder.js +9 -9
- package/lib/auth/user-record.d.ts +23 -15
- package/lib/auth/user-record.js +30 -20
- package/lib/credential/index.d.ts +18 -35
- package/lib/credential/index.js +17 -33
- package/lib/database/database-namespace.d.ts +39 -10
- package/lib/database/database-namespace.js +1 -1
- package/lib/database/database.d.ts +16 -6
- package/lib/database/database.js +61 -6
- package/lib/database/index.d.ts +19 -18
- package/lib/database/index.js +21 -23
- package/lib/default-namespace.d.ts +7 -1
- package/lib/default-namespace.js +4 -4
- package/lib/esm/app/index.js +10 -0
- package/lib/esm/app-check/index.js +4 -0
- package/lib/esm/auth/index.js +14 -0
- package/lib/esm/database/index.js +6 -0
- package/lib/esm/firestore/index.js +24 -0
- package/lib/esm/installations/index.js +4 -0
- package/lib/esm/instance-id/index.js +4 -0
- package/lib/esm/machine-learning/index.js +5 -0
- package/lib/esm/messaging/index.js +4 -0
- package/lib/esm/package.json +1 -0
- package/lib/esm/project-management/index.js +8 -0
- package/lib/esm/remote-config/index.js +4 -0
- package/lib/esm/security-rules/index.js +6 -0
- package/lib/esm/storage/index.js +4 -0
- package/lib/firebase-namespace-api.d.ts +12 -6
- package/lib/firebase-namespace-api.js +5 -1
- package/lib/firestore/firestore-internal.d.ts +2 -2
- package/lib/firestore/firestore-internal.js +2 -2
- package/lib/firestore/firestore-namespace.d.ts +6 -1
- package/lib/firestore/firestore-namespace.js +2 -1
- package/lib/firestore/index.d.ts +36 -4
- package/lib/firestore/index.js +29 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js +1 -1
- package/lib/installations/index.d.ts +52 -0
- package/lib/installations/index.js +62 -0
- package/lib/installations/installations-namespace.d.ts +55 -0
- package/lib/installations/installations-namespace.js +18 -0
- package/lib/{instance-id/instance-id-request-internal.d.ts → installations/installations-request-handler.d.ts} +8 -8
- package/lib/{instance-id/instance-id-request-internal.js → installations/installations-request-handler.js} +23 -23
- package/lib/installations/installations.d.ts +38 -0
- package/lib/installations/installations.js +63 -0
- package/lib/instance-id/index.d.ts +20 -9
- package/lib/instance-id/index.js +20 -9
- package/lib/instance-id/instance-id-namespace.d.ts +9 -8
- package/lib/instance-id/instance-id-namespace.js +1 -1
- package/lib/instance-id/instance-id.d.ts +8 -6
- package/lib/instance-id/instance-id.js +20 -11
- package/lib/machine-learning/index.d.ts +29 -27
- package/lib/machine-learning/index.js +29 -27
- package/lib/machine-learning/machine-learning-api-client.d.ts +1 -1
- package/lib/machine-learning/machine-learning-api-client.js +4 -4
- package/lib/machine-learning/machine-learning-namespace.d.ts +33 -8
- 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 +1 -1
- package/lib/machine-learning/machine-learning.d.ts +19 -19
- package/lib/machine-learning/machine-learning.js +20 -20
- package/lib/messaging/batch-request-internal.d.ts +3 -3
- package/lib/messaging/batch-request-internal.js +9 -9
- package/lib/messaging/index.d.ts +11 -9
- package/lib/messaging/index.js +11 -9
- package/lib/messaging/messaging-api-request-internal.d.ts +8 -8
- package/lib/messaging/messaging-api-request-internal.js +8 -8
- package/lib/messaging/messaging-api.d.ts +79 -75
- package/lib/messaging/messaging-api.js +1 -1
- package/lib/messaging/messaging-errors-internal.d.ts +5 -5
- package/lib/messaging/messaging-errors-internal.js +7 -7
- package/lib/messaging/messaging-internal.d.ts +1 -1
- package/lib/messaging/messaging-internal.js +6 -31
- package/lib/messaging/messaging-namespace.d.ts +105 -8
- package/lib/messaging/messaging-namespace.js +1 -1
- package/lib/messaging/messaging.d.ts +70 -86
- package/lib/messaging/messaging.js +76 -89
- package/lib/project-management/android-app.d.ts +11 -12
- package/lib/project-management/android-app.js +13 -14
- 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 +11 -9
- package/lib/project-management/index.js +11 -9
- package/lib/project-management/ios-app.d.ts +6 -7
- package/lib/project-management/ios-app.js +6 -7
- package/lib/project-management/project-management-api-request-internal.d.ts +1 -80
- package/lib/project-management/project-management-api-request-internal.js +14 -14
- package/lib/project-management/project-management-namespace.d.ts +31 -9
- package/lib/project-management/project-management-namespace.js +1 -1
- package/lib/project-management/project-management.d.ts +18 -21
- package/lib/project-management/project-management.js +19 -22
- package/lib/remote-config/index.d.ts +12 -10
- package/lib/remote-config/index.js +11 -9
- package/lib/remote-config/remote-config-api-client-internal.d.ts +1 -1
- package/lib/remote-config/remote-config-api-client-internal.js +2 -2
- package/lib/remote-config/remote-config-api.d.ts +12 -2
- package/lib/remote-config/remote-config-api.js +1 -1
- package/lib/remote-config/remote-config-namespace.d.ts +50 -9
- package/lib/remote-config/remote-config-namespace.js +1 -1
- package/lib/remote-config/remote-config.d.ts +19 -21
- package/lib/remote-config/remote-config.js +25 -25
- package/lib/security-rules/index.d.ts +12 -10
- package/lib/security-rules/index.js +12 -10
- package/lib/security-rules/security-rules-api-client-internal.d.ts +1 -1
- package/lib/security-rules/security-rules-api-client-internal.js +1 -1
- package/lib/security-rules/security-rules-internal.d.ts +1 -1
- package/lib/security-rules/security-rules-internal.js +1 -1
- package/lib/security-rules/security-rules-namespace.d.ts +43 -28
- package/lib/security-rules/security-rules-namespace.js +1 -1
- package/lib/security-rules/security-rules.d.ts +39 -42
- package/lib/security-rules/security-rules.js +38 -39
- package/lib/storage/index.d.ts +9 -7
- package/lib/storage/index.js +9 -7
- package/lib/storage/storage-namespace.d.ts +7 -6
- package/lib/storage/storage-namespace.js +1 -1
- package/lib/storage/storage.d.ts +3 -3
- package/lib/storage/storage.js +14 -4
- package/lib/utils/api-request.d.ts +24 -24
- package/lib/utils/api-request.js +25 -25
- package/lib/utils/crypto-signer.d.ts +128 -0
- package/lib/utils/crypto-signer.js +237 -0
- package/lib/utils/deep-copy.d.ts +6 -6
- package/lib/utils/deep-copy.js +6 -6
- package/lib/utils/error.d.ts +69 -36
- package/lib/utils/error.js +98 -43
- package/lib/utils/index.d.ts +30 -19
- package/lib/utils/index.js +47 -20
- package/lib/utils/jwt.d.ts +131 -0
- package/lib/utils/jwt.js +355 -0
- package/lib/utils/validator.d.ts +37 -37
- package/lib/utils/validator.js +37 -37
- package/package.json +126 -53
package/lib/utils/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*! firebase-admin
|
|
1
|
+
/*! firebase-admin v10.0.2 */
|
|
2
2
|
"use strict";
|
|
3
3
|
/*!
|
|
4
4
|
* @license
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
* limitations under the License.
|
|
18
18
|
*/
|
|
19
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
-
exports.generateUpdateMask = exports.formatString = exports.toWebSafeBase64 = exports.findProjectId = exports.getExplicitProjectId = exports.addReadonlyGetter = exports.renameProperties = exports.getSdkVersion = void 0;
|
|
20
|
+
exports.transformMillisecondsToSecondsString = exports.generateUpdateMask = exports.formatString = exports.toWebSafeBase64 = exports.findProjectId = exports.getExplicitProjectId = exports.addReadonlyGetter = exports.renameProperties = exports.getSdkVersion = void 0;
|
|
21
21
|
var credential_internal_1 = require("../app/credential-internal");
|
|
22
22
|
var validator = require("./validator");
|
|
23
23
|
var sdkVersion;
|
|
@@ -35,8 +35,8 @@ exports.getSdkVersion = getSdkVersion;
|
|
|
35
35
|
*
|
|
36
36
|
* For example, this can be used to map underscore_cased properties to camelCase.
|
|
37
37
|
*
|
|
38
|
-
* @param
|
|
39
|
-
* @param
|
|
38
|
+
* @param obj - The object whose properties to rename.
|
|
39
|
+
* @param keyMap - The mapping from old to new property names.
|
|
40
40
|
*/
|
|
41
41
|
function renameProperties(obj, keyMap) {
|
|
42
42
|
Object.keys(keyMap).forEach(function (oldKey) {
|
|
@@ -52,9 +52,9 @@ exports.renameProperties = renameProperties;
|
|
|
52
52
|
/**
|
|
53
53
|
* Defines a new read-only property directly on an object and returns the object.
|
|
54
54
|
*
|
|
55
|
-
* @param
|
|
56
|
-
* @param
|
|
57
|
-
* @param
|
|
55
|
+
* @param obj - The object on which to define the property.
|
|
56
|
+
* @param prop - The name of the property to be defined or modified.
|
|
57
|
+
* @param value - The value associated with the property.
|
|
58
58
|
*/
|
|
59
59
|
function addReadonlyGetter(obj, prop, value) {
|
|
60
60
|
Object.defineProperty(obj, prop, {
|
|
@@ -71,9 +71,9 @@ exports.addReadonlyGetter = addReadonlyGetter;
|
|
|
71
71
|
* specified in either the Firebase app options, credentials or the local environment.
|
|
72
72
|
* Otherwise returns null.
|
|
73
73
|
*
|
|
74
|
-
* @param app A Firebase app to get the project ID from.
|
|
74
|
+
* @param app - A Firebase app to get the project ID from.
|
|
75
75
|
*
|
|
76
|
-
* @
|
|
76
|
+
* @returns A project ID string or null.
|
|
77
77
|
*/
|
|
78
78
|
function getExplicitProjectId(app) {
|
|
79
79
|
var options = app.options;
|
|
@@ -98,9 +98,9 @@ exports.getExplicitProjectId = getExplicitProjectId;
|
|
|
98
98
|
* configured, but the SDK has been initialized with ComputeEngineCredentials, this
|
|
99
99
|
* method attempts to discover the project ID from the local metadata service.
|
|
100
100
|
*
|
|
101
|
-
* @param app A Firebase app to get the project ID from.
|
|
101
|
+
* @param app - A Firebase app to get the project ID from.
|
|
102
102
|
*
|
|
103
|
-
* @
|
|
103
|
+
* @returns A project ID string or null.
|
|
104
104
|
*/
|
|
105
105
|
function findProjectId(app) {
|
|
106
106
|
var projectId = getExplicitProjectId(app);
|
|
@@ -117,8 +117,8 @@ exports.findProjectId = findProjectId;
|
|
|
117
117
|
/**
|
|
118
118
|
* Encodes data using web-safe-base64.
|
|
119
119
|
*
|
|
120
|
-
* @param
|
|
121
|
-
* @
|
|
120
|
+
* @param data - The raw data byte input.
|
|
121
|
+
* @returns The base64-encoded result.
|
|
122
122
|
*/
|
|
123
123
|
function toWebSafeBase64(data) {
|
|
124
124
|
return data.toString('base64').replace(/\//g, '_').replace(/\+/g, '-');
|
|
@@ -129,11 +129,11 @@ exports.toWebSafeBase64 = toWebSafeBase64;
|
|
|
129
129
|
* with corresponding arguments {projectId: '1234', api: 'resource'}
|
|
130
130
|
* and returns output: 'project/1234/resource'.
|
|
131
131
|
*
|
|
132
|
-
* @param
|
|
132
|
+
* @param str - The original string where the param need to be
|
|
133
133
|
* replaced.
|
|
134
|
-
* @param
|
|
134
|
+
* @param params - The optional parameters to replace in the
|
|
135
135
|
* string.
|
|
136
|
-
* @
|
|
136
|
+
* @returns The resulting formatted string.
|
|
137
137
|
*/
|
|
138
138
|
function formatString(str, params) {
|
|
139
139
|
var formatted = str;
|
|
@@ -147,12 +147,12 @@ exports.formatString = formatString;
|
|
|
147
147
|
* Generates the update mask for the provided object.
|
|
148
148
|
* Note this will ignore the last key with value undefined.
|
|
149
149
|
*
|
|
150
|
-
* @param obj The object to generate the update mask for.
|
|
151
|
-
* @param terminalPaths The optional map of keys for maximum paths to traverse.
|
|
150
|
+
* @param obj - The object to generate the update mask for.
|
|
151
|
+
* @param terminalPaths - The optional map of keys for maximum paths to traverse.
|
|
152
152
|
* Nested objects beyond that path will be ignored. This is useful for
|
|
153
153
|
* keys with variable object values.
|
|
154
|
-
* @param root The path so far.
|
|
155
|
-
* @
|
|
154
|
+
* @param root - The path so far.
|
|
155
|
+
* @returns The computed update mask list.
|
|
156
156
|
*/
|
|
157
157
|
function generateUpdateMask(obj, terminalPaths, root) {
|
|
158
158
|
if (terminalPaths === void 0) { terminalPaths = []; }
|
|
@@ -189,3 +189,30 @@ function generateUpdateMask(obj, terminalPaths, root) {
|
|
|
189
189
|
return updateMask;
|
|
190
190
|
}
|
|
191
191
|
exports.generateUpdateMask = generateUpdateMask;
|
|
192
|
+
/**
|
|
193
|
+
* Transforms milliseconds to a protobuf Duration type string.
|
|
194
|
+
* Returns the duration in seconds with up to nine fractional
|
|
195
|
+
* digits, terminated by 's'. Example: "3 seconds 0 nano seconds as 3s,
|
|
196
|
+
* 3 seconds 1 nano seconds as 3.000000001s".
|
|
197
|
+
*
|
|
198
|
+
* @param milliseconds - The duration in milliseconds.
|
|
199
|
+
* @returns The resulting formatted string in seconds with up to nine fractional
|
|
200
|
+
* digits, terminated by 's'.
|
|
201
|
+
*/
|
|
202
|
+
function transformMillisecondsToSecondsString(milliseconds) {
|
|
203
|
+
var duration;
|
|
204
|
+
var seconds = Math.floor(milliseconds / 1000);
|
|
205
|
+
var nanos = Math.floor((milliseconds - seconds * 1000) * 1000000);
|
|
206
|
+
if (nanos > 0) {
|
|
207
|
+
var nanoString = nanos.toString();
|
|
208
|
+
while (nanoString.length < 9) {
|
|
209
|
+
nanoString = '0' + nanoString;
|
|
210
|
+
}
|
|
211
|
+
duration = seconds + "." + nanoString + "s";
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
duration = seconds + "s";
|
|
215
|
+
}
|
|
216
|
+
return duration;
|
|
217
|
+
}
|
|
218
|
+
exports.transformMillisecondsToSecondsString = transformMillisecondsToSecondsString;
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/*! firebase-admin v10.0.2 */
|
|
2
|
+
/*!
|
|
3
|
+
* Copyright 2021 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 * as jwt from 'jsonwebtoken';
|
|
19
|
+
import { Agent } from 'http';
|
|
20
|
+
export declare const ALGORITHM_RS256: jwt.Algorithm;
|
|
21
|
+
export declare type Dictionary = {
|
|
22
|
+
[key: string]: any;
|
|
23
|
+
};
|
|
24
|
+
export declare type DecodedToken = {
|
|
25
|
+
header: Dictionary;
|
|
26
|
+
payload: Dictionary;
|
|
27
|
+
};
|
|
28
|
+
export interface SignatureVerifier {
|
|
29
|
+
verify(token: string): Promise<void>;
|
|
30
|
+
}
|
|
31
|
+
interface KeyFetcher {
|
|
32
|
+
fetchPublicKeys(): Promise<{
|
|
33
|
+
[key: string]: string;
|
|
34
|
+
}>;
|
|
35
|
+
}
|
|
36
|
+
export declare class JwksFetcher implements KeyFetcher {
|
|
37
|
+
private publicKeys;
|
|
38
|
+
private publicKeysExpireAt;
|
|
39
|
+
private client;
|
|
40
|
+
constructor(jwksUrl: string);
|
|
41
|
+
fetchPublicKeys(): Promise<{
|
|
42
|
+
[key: string]: string;
|
|
43
|
+
}>;
|
|
44
|
+
private shouldRefresh;
|
|
45
|
+
private refresh;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Class to fetch public keys from a client certificates URL.
|
|
49
|
+
*/
|
|
50
|
+
export declare class UrlKeyFetcher implements KeyFetcher {
|
|
51
|
+
private clientCertUrl;
|
|
52
|
+
private readonly httpAgent?;
|
|
53
|
+
private publicKeys;
|
|
54
|
+
private publicKeysExpireAt;
|
|
55
|
+
constructor(clientCertUrl: string, httpAgent?: Agent | undefined);
|
|
56
|
+
/**
|
|
57
|
+
* Fetches the public keys for the Google certs.
|
|
58
|
+
*
|
|
59
|
+
* @returns A promise fulfilled with public keys for the Google certs.
|
|
60
|
+
*/
|
|
61
|
+
fetchPublicKeys(): Promise<{
|
|
62
|
+
[key: string]: string;
|
|
63
|
+
}>;
|
|
64
|
+
/**
|
|
65
|
+
* Checks if the cached public keys need to be refreshed.
|
|
66
|
+
*
|
|
67
|
+
* @returns Whether the keys should be fetched from the client certs url or not.
|
|
68
|
+
*/
|
|
69
|
+
private shouldRefresh;
|
|
70
|
+
private refresh;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Class for verifying JWT signature with a public key.
|
|
74
|
+
*/
|
|
75
|
+
export declare class PublicKeySignatureVerifier implements SignatureVerifier {
|
|
76
|
+
private keyFetcher;
|
|
77
|
+
constructor(keyFetcher: KeyFetcher);
|
|
78
|
+
static withCertificateUrl(clientCertUrl: string, httpAgent?: Agent): PublicKeySignatureVerifier;
|
|
79
|
+
static withJwksUrl(jwksUrl: string): PublicKeySignatureVerifier;
|
|
80
|
+
verify(token: string): Promise<void>;
|
|
81
|
+
private verifyWithoutKid;
|
|
82
|
+
private verifyWithAllKeys;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Class for verifying unsigned (emulator) JWTs.
|
|
86
|
+
*/
|
|
87
|
+
export declare class EmulatorSignatureVerifier implements SignatureVerifier {
|
|
88
|
+
verify(token: string): Promise<void>;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Verifies the signature of a JWT using the provided secret or a function to fetch
|
|
92
|
+
* the secret or public key.
|
|
93
|
+
*
|
|
94
|
+
* @param token - The JWT to be verified.
|
|
95
|
+
* @param secretOrPublicKey - The secret or a function to fetch the secret or public key.
|
|
96
|
+
* @param options - JWT verification options.
|
|
97
|
+
* @returns A Promise resolving for a token with a valid signature.
|
|
98
|
+
*/
|
|
99
|
+
export declare function verifyJwtSignature(token: string, secretOrPublicKey: jwt.Secret | jwt.GetPublicKeyOrSecret, options?: jwt.VerifyOptions): Promise<void>;
|
|
100
|
+
/**
|
|
101
|
+
* Decodes general purpose Firebase JWTs.
|
|
102
|
+
*
|
|
103
|
+
* @param jwtToken - JWT token to be decoded.
|
|
104
|
+
* @returns Decoded token containing the header and payload.
|
|
105
|
+
*/
|
|
106
|
+
export declare function decodeJwt(jwtToken: string): Promise<DecodedToken>;
|
|
107
|
+
/**
|
|
108
|
+
* Jwt error code structure.
|
|
109
|
+
*
|
|
110
|
+
* @param code - The error code.
|
|
111
|
+
* @param message - The error message.
|
|
112
|
+
* @constructor
|
|
113
|
+
*/
|
|
114
|
+
export declare class JwtError extends Error {
|
|
115
|
+
readonly code: JwtErrorCode;
|
|
116
|
+
readonly message: string;
|
|
117
|
+
constructor(code: JwtErrorCode, message: string);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* JWT error codes.
|
|
121
|
+
*/
|
|
122
|
+
export declare enum JwtErrorCode {
|
|
123
|
+
INVALID_ARGUMENT = "invalid-argument",
|
|
124
|
+
INVALID_CREDENTIAL = "invalid-credential",
|
|
125
|
+
TOKEN_EXPIRED = "token-expired",
|
|
126
|
+
INVALID_SIGNATURE = "invalid-token",
|
|
127
|
+
NO_MATCHING_KID = "no-matching-kid-error",
|
|
128
|
+
NO_KID_IN_HEADER = "no-kid-error",
|
|
129
|
+
KEY_FETCH_ERROR = "key-fetch-error"
|
|
130
|
+
}
|
|
131
|
+
export {};
|
package/lib/utils/jwt.js
ADDED
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
/*! firebase-admin v10.0.2 */
|
|
2
|
+
"use strict";
|
|
3
|
+
/*!
|
|
4
|
+
* Copyright 2021 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
|
+
var __extends = (this && this.__extends) || (function () {
|
|
19
|
+
var extendStatics = function (d, b) {
|
|
20
|
+
extendStatics = Object.setPrototypeOf ||
|
|
21
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
22
|
+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
|
23
|
+
return extendStatics(d, b);
|
|
24
|
+
};
|
|
25
|
+
return function (d, b) {
|
|
26
|
+
extendStatics(d, b);
|
|
27
|
+
function __() { this.constructor = d; }
|
|
28
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
29
|
+
};
|
|
30
|
+
})();
|
|
31
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
32
|
+
exports.JwtErrorCode = exports.JwtError = exports.decodeJwt = exports.verifyJwtSignature = exports.EmulatorSignatureVerifier = exports.PublicKeySignatureVerifier = exports.UrlKeyFetcher = exports.JwksFetcher = exports.ALGORITHM_RS256 = void 0;
|
|
33
|
+
var validator = require("./validator");
|
|
34
|
+
var jwt = require("jsonwebtoken");
|
|
35
|
+
var jwks = require("jwks-rsa");
|
|
36
|
+
var api_request_1 = require("../utils/api-request");
|
|
37
|
+
exports.ALGORITHM_RS256 = 'RS256';
|
|
38
|
+
// `jsonwebtoken` converts errors from the `getKey` callback to its own `JsonWebTokenError` type
|
|
39
|
+
// and prefixes the error message with the following. Use the prefix to identify errors thrown
|
|
40
|
+
// from the key provider callback.
|
|
41
|
+
// https://github.com/auth0/node-jsonwebtoken/blob/d71e383862fc735991fd2e759181480f066bf138/verify.js#L96
|
|
42
|
+
var JWT_CALLBACK_ERROR_PREFIX = 'error in secret or public key callback: ';
|
|
43
|
+
var NO_MATCHING_KID_ERROR_MESSAGE = 'no-matching-kid-error';
|
|
44
|
+
var NO_KID_IN_HEADER_ERROR_MESSAGE = 'no-kid-in-header-error';
|
|
45
|
+
var HOUR_IN_SECONDS = 3600;
|
|
46
|
+
var JwksFetcher = /** @class */ (function () {
|
|
47
|
+
function JwksFetcher(jwksUrl) {
|
|
48
|
+
this.publicKeysExpireAt = 0;
|
|
49
|
+
if (!validator.isURL(jwksUrl)) {
|
|
50
|
+
throw new Error('The provided JWKS URL is not a valid URL.');
|
|
51
|
+
}
|
|
52
|
+
this.client = jwks({
|
|
53
|
+
jwksUri: jwksUrl,
|
|
54
|
+
cache: false,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
JwksFetcher.prototype.fetchPublicKeys = function () {
|
|
58
|
+
if (this.shouldRefresh()) {
|
|
59
|
+
return this.refresh();
|
|
60
|
+
}
|
|
61
|
+
return Promise.resolve(this.publicKeys);
|
|
62
|
+
};
|
|
63
|
+
JwksFetcher.prototype.shouldRefresh = function () {
|
|
64
|
+
return !this.publicKeys || this.publicKeysExpireAt <= Date.now();
|
|
65
|
+
};
|
|
66
|
+
JwksFetcher.prototype.refresh = function () {
|
|
67
|
+
var _this = this;
|
|
68
|
+
return this.client.getSigningKeys()
|
|
69
|
+
.then(function (signingKeys) {
|
|
70
|
+
// reset expire at from previous set of keys.
|
|
71
|
+
_this.publicKeysExpireAt = 0;
|
|
72
|
+
var newKeys = signingKeys.reduce(function (map, signingKey) {
|
|
73
|
+
map[signingKey.kid] = signingKey.getPublicKey();
|
|
74
|
+
return map;
|
|
75
|
+
}, {});
|
|
76
|
+
_this.publicKeysExpireAt = Date.now() + (HOUR_IN_SECONDS * 6 * 1000);
|
|
77
|
+
_this.publicKeys = newKeys;
|
|
78
|
+
return newKeys;
|
|
79
|
+
}).catch(function (err) {
|
|
80
|
+
throw new Error("Error fetching Json Web Keys: " + err.message);
|
|
81
|
+
});
|
|
82
|
+
};
|
|
83
|
+
return JwksFetcher;
|
|
84
|
+
}());
|
|
85
|
+
exports.JwksFetcher = JwksFetcher;
|
|
86
|
+
/**
|
|
87
|
+
* Class to fetch public keys from a client certificates URL.
|
|
88
|
+
*/
|
|
89
|
+
var UrlKeyFetcher = /** @class */ (function () {
|
|
90
|
+
function UrlKeyFetcher(clientCertUrl, httpAgent) {
|
|
91
|
+
this.clientCertUrl = clientCertUrl;
|
|
92
|
+
this.httpAgent = httpAgent;
|
|
93
|
+
this.publicKeysExpireAt = 0;
|
|
94
|
+
if (!validator.isURL(clientCertUrl)) {
|
|
95
|
+
throw new Error('The provided public client certificate URL is not a valid URL.');
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Fetches the public keys for the Google certs.
|
|
100
|
+
*
|
|
101
|
+
* @returns A promise fulfilled with public keys for the Google certs.
|
|
102
|
+
*/
|
|
103
|
+
UrlKeyFetcher.prototype.fetchPublicKeys = function () {
|
|
104
|
+
if (this.shouldRefresh()) {
|
|
105
|
+
return this.refresh();
|
|
106
|
+
}
|
|
107
|
+
return Promise.resolve(this.publicKeys);
|
|
108
|
+
};
|
|
109
|
+
/**
|
|
110
|
+
* Checks if the cached public keys need to be refreshed.
|
|
111
|
+
*
|
|
112
|
+
* @returns Whether the keys should be fetched from the client certs url or not.
|
|
113
|
+
*/
|
|
114
|
+
UrlKeyFetcher.prototype.shouldRefresh = function () {
|
|
115
|
+
return !this.publicKeys || this.publicKeysExpireAt <= Date.now();
|
|
116
|
+
};
|
|
117
|
+
UrlKeyFetcher.prototype.refresh = function () {
|
|
118
|
+
var _this = this;
|
|
119
|
+
var client = new api_request_1.HttpClient();
|
|
120
|
+
var request = {
|
|
121
|
+
method: 'GET',
|
|
122
|
+
url: this.clientCertUrl,
|
|
123
|
+
httpAgent: this.httpAgent,
|
|
124
|
+
};
|
|
125
|
+
return client.send(request).then(function (resp) {
|
|
126
|
+
if (!resp.isJson() || resp.data.error) {
|
|
127
|
+
// Treat all non-json messages and messages with an 'error' field as
|
|
128
|
+
// error responses.
|
|
129
|
+
throw new api_request_1.HttpError(resp);
|
|
130
|
+
}
|
|
131
|
+
// reset expire at from previous set of keys.
|
|
132
|
+
_this.publicKeysExpireAt = 0;
|
|
133
|
+
if (Object.prototype.hasOwnProperty.call(resp.headers, 'cache-control')) {
|
|
134
|
+
var cacheControlHeader = resp.headers['cache-control'];
|
|
135
|
+
var parts = cacheControlHeader.split(',');
|
|
136
|
+
parts.forEach(function (part) {
|
|
137
|
+
var subParts = part.trim().split('=');
|
|
138
|
+
if (subParts[0] === 'max-age') {
|
|
139
|
+
var maxAge = +subParts[1];
|
|
140
|
+
_this.publicKeysExpireAt = Date.now() + (maxAge * 1000);
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
_this.publicKeys = resp.data;
|
|
145
|
+
return resp.data;
|
|
146
|
+
}).catch(function (err) {
|
|
147
|
+
if (err instanceof api_request_1.HttpError) {
|
|
148
|
+
var errorMessage = 'Error fetching public keys for Google certs: ';
|
|
149
|
+
var resp = err.response;
|
|
150
|
+
if (resp.isJson() && resp.data.error) {
|
|
151
|
+
errorMessage += "" + resp.data.error;
|
|
152
|
+
if (resp.data.error_description) {
|
|
153
|
+
errorMessage += ' (' + resp.data.error_description + ')';
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
errorMessage += "" + resp.text;
|
|
158
|
+
}
|
|
159
|
+
throw new Error(errorMessage);
|
|
160
|
+
}
|
|
161
|
+
throw err;
|
|
162
|
+
});
|
|
163
|
+
};
|
|
164
|
+
return UrlKeyFetcher;
|
|
165
|
+
}());
|
|
166
|
+
exports.UrlKeyFetcher = UrlKeyFetcher;
|
|
167
|
+
/**
|
|
168
|
+
* Class for verifying JWT signature with a public key.
|
|
169
|
+
*/
|
|
170
|
+
var PublicKeySignatureVerifier = /** @class */ (function () {
|
|
171
|
+
function PublicKeySignatureVerifier(keyFetcher) {
|
|
172
|
+
this.keyFetcher = keyFetcher;
|
|
173
|
+
if (!validator.isNonNullObject(keyFetcher)) {
|
|
174
|
+
throw new Error('The provided key fetcher is not an object or null.');
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
PublicKeySignatureVerifier.withCertificateUrl = function (clientCertUrl, httpAgent) {
|
|
178
|
+
return new PublicKeySignatureVerifier(new UrlKeyFetcher(clientCertUrl, httpAgent));
|
|
179
|
+
};
|
|
180
|
+
PublicKeySignatureVerifier.withJwksUrl = function (jwksUrl) {
|
|
181
|
+
return new PublicKeySignatureVerifier(new JwksFetcher(jwksUrl));
|
|
182
|
+
};
|
|
183
|
+
PublicKeySignatureVerifier.prototype.verify = function (token) {
|
|
184
|
+
var _this = this;
|
|
185
|
+
if (!validator.isString(token)) {
|
|
186
|
+
return Promise.reject(new JwtError(JwtErrorCode.INVALID_ARGUMENT, 'The provided token must be a string.'));
|
|
187
|
+
}
|
|
188
|
+
return verifyJwtSignature(token, getKeyCallback(this.keyFetcher), { algorithms: [exports.ALGORITHM_RS256] })
|
|
189
|
+
.catch(function (error) {
|
|
190
|
+
if (error.code === JwtErrorCode.NO_KID_IN_HEADER) {
|
|
191
|
+
// No kid in JWT header. Try with all the public keys.
|
|
192
|
+
return _this.verifyWithoutKid(token);
|
|
193
|
+
}
|
|
194
|
+
throw error;
|
|
195
|
+
});
|
|
196
|
+
};
|
|
197
|
+
PublicKeySignatureVerifier.prototype.verifyWithoutKid = function (token) {
|
|
198
|
+
var _this = this;
|
|
199
|
+
return this.keyFetcher.fetchPublicKeys()
|
|
200
|
+
.then(function (publicKeys) { return _this.verifyWithAllKeys(token, publicKeys); });
|
|
201
|
+
};
|
|
202
|
+
PublicKeySignatureVerifier.prototype.verifyWithAllKeys = function (token, keys) {
|
|
203
|
+
var promises = [];
|
|
204
|
+
Object.values(keys).forEach(function (key) {
|
|
205
|
+
var result = verifyJwtSignature(token, key)
|
|
206
|
+
.then(function () { return true; })
|
|
207
|
+
.catch(function (error) {
|
|
208
|
+
if (error.code === JwtErrorCode.TOKEN_EXPIRED) {
|
|
209
|
+
throw error;
|
|
210
|
+
}
|
|
211
|
+
return false;
|
|
212
|
+
});
|
|
213
|
+
promises.push(result);
|
|
214
|
+
});
|
|
215
|
+
return Promise.all(promises)
|
|
216
|
+
.then(function (result) {
|
|
217
|
+
if (result.every(function (r) { return r === false; })) {
|
|
218
|
+
throw new JwtError(JwtErrorCode.INVALID_SIGNATURE, 'Invalid token signature.');
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
};
|
|
222
|
+
return PublicKeySignatureVerifier;
|
|
223
|
+
}());
|
|
224
|
+
exports.PublicKeySignatureVerifier = PublicKeySignatureVerifier;
|
|
225
|
+
/**
|
|
226
|
+
* Class for verifying unsigned (emulator) JWTs.
|
|
227
|
+
*/
|
|
228
|
+
var EmulatorSignatureVerifier = /** @class */ (function () {
|
|
229
|
+
function EmulatorSignatureVerifier() {
|
|
230
|
+
}
|
|
231
|
+
EmulatorSignatureVerifier.prototype.verify = function (token) {
|
|
232
|
+
// Signature checks skipped for emulator; no need to fetch public keys.
|
|
233
|
+
return verifyJwtSignature(token, '');
|
|
234
|
+
};
|
|
235
|
+
return EmulatorSignatureVerifier;
|
|
236
|
+
}());
|
|
237
|
+
exports.EmulatorSignatureVerifier = EmulatorSignatureVerifier;
|
|
238
|
+
/**
|
|
239
|
+
* Provides a callback to fetch public keys.
|
|
240
|
+
*
|
|
241
|
+
* @param fetcher - KeyFetcher to fetch the keys from.
|
|
242
|
+
* @returns A callback function that can be used to get keys in `jsonwebtoken`.
|
|
243
|
+
*/
|
|
244
|
+
function getKeyCallback(fetcher) {
|
|
245
|
+
return function (header, callback) {
|
|
246
|
+
if (!header.kid) {
|
|
247
|
+
callback(new Error(NO_KID_IN_HEADER_ERROR_MESSAGE));
|
|
248
|
+
}
|
|
249
|
+
var kid = header.kid || '';
|
|
250
|
+
fetcher.fetchPublicKeys().then(function (publicKeys) {
|
|
251
|
+
if (!Object.prototype.hasOwnProperty.call(publicKeys, kid)) {
|
|
252
|
+
callback(new Error(NO_MATCHING_KID_ERROR_MESSAGE));
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
callback(null, publicKeys[kid]);
|
|
256
|
+
}
|
|
257
|
+
})
|
|
258
|
+
.catch(function (error) {
|
|
259
|
+
callback(error);
|
|
260
|
+
});
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Verifies the signature of a JWT using the provided secret or a function to fetch
|
|
265
|
+
* the secret or public key.
|
|
266
|
+
*
|
|
267
|
+
* @param token - The JWT to be verified.
|
|
268
|
+
* @param secretOrPublicKey - The secret or a function to fetch the secret or public key.
|
|
269
|
+
* @param options - JWT verification options.
|
|
270
|
+
* @returns A Promise resolving for a token with a valid signature.
|
|
271
|
+
*/
|
|
272
|
+
function verifyJwtSignature(token, secretOrPublicKey, options) {
|
|
273
|
+
if (!validator.isString(token)) {
|
|
274
|
+
return Promise.reject(new JwtError(JwtErrorCode.INVALID_ARGUMENT, 'The provided token must be a string.'));
|
|
275
|
+
}
|
|
276
|
+
return new Promise(function (resolve, reject) {
|
|
277
|
+
jwt.verify(token, secretOrPublicKey, options, function (error) {
|
|
278
|
+
if (!error) {
|
|
279
|
+
return resolve();
|
|
280
|
+
}
|
|
281
|
+
if (error.name === 'TokenExpiredError') {
|
|
282
|
+
return reject(new JwtError(JwtErrorCode.TOKEN_EXPIRED, 'The provided token has expired. Get a fresh token from your ' +
|
|
283
|
+
'client app and try again.'));
|
|
284
|
+
}
|
|
285
|
+
else if (error.name === 'JsonWebTokenError') {
|
|
286
|
+
if (error.message && error.message.includes(JWT_CALLBACK_ERROR_PREFIX)) {
|
|
287
|
+
var message = error.message.split(JWT_CALLBACK_ERROR_PREFIX).pop() || 'Error fetching public keys.';
|
|
288
|
+
var code = JwtErrorCode.KEY_FETCH_ERROR;
|
|
289
|
+
if (message === NO_MATCHING_KID_ERROR_MESSAGE) {
|
|
290
|
+
code = JwtErrorCode.NO_MATCHING_KID;
|
|
291
|
+
}
|
|
292
|
+
else if (message === NO_KID_IN_HEADER_ERROR_MESSAGE) {
|
|
293
|
+
code = JwtErrorCode.NO_KID_IN_HEADER;
|
|
294
|
+
}
|
|
295
|
+
return reject(new JwtError(code, message));
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
return reject(new JwtError(JwtErrorCode.INVALID_SIGNATURE, error.message));
|
|
299
|
+
});
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
exports.verifyJwtSignature = verifyJwtSignature;
|
|
303
|
+
/**
|
|
304
|
+
* Decodes general purpose Firebase JWTs.
|
|
305
|
+
*
|
|
306
|
+
* @param jwtToken - JWT token to be decoded.
|
|
307
|
+
* @returns Decoded token containing the header and payload.
|
|
308
|
+
*/
|
|
309
|
+
function decodeJwt(jwtToken) {
|
|
310
|
+
if (!validator.isString(jwtToken)) {
|
|
311
|
+
return Promise.reject(new JwtError(JwtErrorCode.INVALID_ARGUMENT, 'The provided token must be a string.'));
|
|
312
|
+
}
|
|
313
|
+
var fullDecodedToken = jwt.decode(jwtToken, {
|
|
314
|
+
complete: true,
|
|
315
|
+
});
|
|
316
|
+
if (!fullDecodedToken) {
|
|
317
|
+
return Promise.reject(new JwtError(JwtErrorCode.INVALID_ARGUMENT, 'Decoding token failed.'));
|
|
318
|
+
}
|
|
319
|
+
var header = fullDecodedToken === null || fullDecodedToken === void 0 ? void 0 : fullDecodedToken.header;
|
|
320
|
+
var payload = fullDecodedToken === null || fullDecodedToken === void 0 ? void 0 : fullDecodedToken.payload;
|
|
321
|
+
return Promise.resolve({ header: header, payload: payload });
|
|
322
|
+
}
|
|
323
|
+
exports.decodeJwt = decodeJwt;
|
|
324
|
+
/**
|
|
325
|
+
* Jwt error code structure.
|
|
326
|
+
*
|
|
327
|
+
* @param code - The error code.
|
|
328
|
+
* @param message - The error message.
|
|
329
|
+
* @constructor
|
|
330
|
+
*/
|
|
331
|
+
var JwtError = /** @class */ (function (_super) {
|
|
332
|
+
__extends(JwtError, _super);
|
|
333
|
+
function JwtError(code, message) {
|
|
334
|
+
var _this = _super.call(this, message) || this;
|
|
335
|
+
_this.code = code;
|
|
336
|
+
_this.message = message;
|
|
337
|
+
_this.__proto__ = JwtError.prototype;
|
|
338
|
+
return _this;
|
|
339
|
+
}
|
|
340
|
+
return JwtError;
|
|
341
|
+
}(Error));
|
|
342
|
+
exports.JwtError = JwtError;
|
|
343
|
+
/**
|
|
344
|
+
* JWT error codes.
|
|
345
|
+
*/
|
|
346
|
+
var JwtErrorCode;
|
|
347
|
+
(function (JwtErrorCode) {
|
|
348
|
+
JwtErrorCode["INVALID_ARGUMENT"] = "invalid-argument";
|
|
349
|
+
JwtErrorCode["INVALID_CREDENTIAL"] = "invalid-credential";
|
|
350
|
+
JwtErrorCode["TOKEN_EXPIRED"] = "token-expired";
|
|
351
|
+
JwtErrorCode["INVALID_SIGNATURE"] = "invalid-token";
|
|
352
|
+
JwtErrorCode["NO_MATCHING_KID"] = "no-matching-kid-error";
|
|
353
|
+
JwtErrorCode["NO_KID_IN_HEADER"] = "no-kid-error";
|
|
354
|
+
JwtErrorCode["KEY_FETCH_ERROR"] = "key-fetch-error";
|
|
355
|
+
})(JwtErrorCode = exports.JwtErrorCode || (exports.JwtErrorCode = {}));
|