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,160 @@
1
+ /*! firebase-admin v9.12.0 */
2
+ /*!
3
+ * @license
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
+ import { app } from '../firebase-namespace-api';
19
+ /**
20
+ * Gets the {@link appCheck.AppCheck `AppCheck`} service for the
21
+ * default app or a given app.
22
+ *
23
+ * You can call `admin.appCheck()` with no arguments to access the default
24
+ * app's {@link appCheck.AppCheck `AppCheck`} service or as
25
+ * `admin.appCheck(app)` to access the
26
+ * {@link appCheck.AppCheck `AppCheck`} service associated with a
27
+ * specific app.
28
+ *
29
+ * @example
30
+ * ```javascript
31
+ * // Get the `AppCheck` service for the default app
32
+ * var defaultAppCheck = admin.appCheck();
33
+ * ```
34
+ *
35
+ * @example
36
+ * ```javascript
37
+ * // Get the `AppCheck` service for a given app
38
+ * var otherAppCheck = admin.appCheck(otherApp);
39
+ * ```
40
+ *
41
+ * @param app Optional app for which to return the `AppCheck` service.
42
+ * If not provided, the default `AppCheck` service is returned.
43
+ *
44
+ * @return The default `AppCheck` service if no
45
+ * app is provided, or the `AppCheck` service associated with the provided
46
+ * app.
47
+ */
48
+ export declare function appCheck(app?: app.App): appCheck.AppCheck;
49
+ export declare namespace appCheck {
50
+ /**
51
+ * The Firebase `AppCheck` service interface.
52
+ */
53
+ interface AppCheck {
54
+ app: app.App;
55
+ /**
56
+ * Creates a new {@link appCheck.AppCheckToken `AppCheckToken`} that can be sent
57
+ * back to a client.
58
+ *
59
+ * @param appId The App ID of the Firebase App the token belongs to.
60
+ * @param options Optional options object when creating a new App Check Token.
61
+ *
62
+ * @returns A promise that fulfills with a `AppCheckToken`.
63
+ */
64
+ createToken(appId: string, options?: AppCheckTokenOptions): Promise<AppCheckToken>;
65
+ /**
66
+ * Verifies a Firebase App Check token (JWT). If the token is valid, the promise is
67
+ * fulfilled with the token's decoded claims; otherwise, the promise is
68
+ * rejected.
69
+ *
70
+ * @param appCheckToken The App Check token to verify.
71
+ *
72
+ * @return A promise fulfilled with the
73
+ * token's decoded claims if the App Check token is valid; otherwise, a rejected
74
+ * promise.
75
+ */
76
+ verifyToken(appCheckToken: string): Promise<VerifyAppCheckTokenResponse>;
77
+ }
78
+ /**
79
+ * Interface representing an App Check token.
80
+ */
81
+ interface AppCheckToken {
82
+ /**
83
+ * The Firebase App Check token.
84
+ */
85
+ token: string;
86
+ /**
87
+ * The time-to-live duration of the token in milliseconds.
88
+ */
89
+ ttlMillis: number;
90
+ }
91
+ /**
92
+ * Interface representing App Check token options.
93
+ */
94
+ interface AppCheckTokenOptions {
95
+ /**
96
+ * The length of time, in milliseconds, for which the App Check token will
97
+ * be valid. This value must be between 30 minutes and 7 days, inclusive.
98
+ */
99
+ ttlMillis?: number;
100
+ }
101
+ /**
102
+ * Interface representing a decoded Firebase App Check token, returned from the
103
+ * {@link appCheck.AppCheck.verifyToken `verifyToken()`} method.
104
+ */
105
+ interface DecodedAppCheckToken {
106
+ /**
107
+ * The issuer identifier for the issuer of the response.
108
+ *
109
+ * This value is a URL with the format
110
+ * `https://firebaseappcheck.googleapis.com/<PROJECT_NUMBER>`, where `<PROJECT_NUMBER>` is the
111
+ * same project number specified in the [`aud`](#aud) property.
112
+ */
113
+ iss: string;
114
+ /**
115
+ * The Firebase App ID corresponding to the app the token belonged to.
116
+ *
117
+ * As a convenience, this value is copied over to the [`app_id`](#app_id) property.
118
+ */
119
+ sub: string;
120
+ /**
121
+ * The audience for which this token is intended.
122
+ *
123
+ * This value is a JSON array of two strings, the first is the project number of your
124
+ * Firebase project, and the second is the project ID of the same project.
125
+ */
126
+ aud: string[];
127
+ /**
128
+ * The App Check token's expiration time, in seconds since the Unix epoch. That is, the
129
+ * time at which this App Check token expires and should no longer be considered valid.
130
+ */
131
+ exp: number;
132
+ /**
133
+ * The App Check token's issued-at time, in seconds since the Unix epoch. That is, the
134
+ * time at which this App Check token was issued and should start to be considered
135
+ * valid.
136
+ */
137
+ iat: number;
138
+ /**
139
+ * The App ID corresponding to the App the App Check token belonged to.
140
+ *
141
+ * This value is not actually one of the JWT token claims. It is added as a
142
+ * convenience, and is set as the value of the [`sub`](#sub) property.
143
+ */
144
+ app_id: string;
145
+ [key: string]: any;
146
+ }
147
+ /**
148
+ * Interface representing a verified App Check token response.
149
+ */
150
+ interface VerifyAppCheckTokenResponse {
151
+ /**
152
+ * The App ID corresponding to the App the App Check token belonged to.
153
+ */
154
+ appId: string;
155
+ /**
156
+ * The decoded Firebase App Check token.
157
+ */
158
+ token: appCheck.DecodedAppCheckToken;
159
+ }
160
+ }
@@ -0,0 +1,19 @@
1
+ /*! firebase-admin v9.12.0 */
2
+ "use strict";
3
+ /*!
4
+ * @license
5
+ * Copyright 2021 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 });
@@ -0,0 +1,161 @@
1
+ /*! firebase-admin v9.12.0 */
2
+ "use strict";
3
+ /*!
4
+ * @license
5
+ * Copyright 2021 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
+ var __assign = (this && this.__assign) || function () {
20
+ __assign = Object.assign || function(t) {
21
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
22
+ s = arguments[i];
23
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
24
+ t[p] = s[p];
25
+ }
26
+ return t;
27
+ };
28
+ return __assign.apply(this, arguments);
29
+ };
30
+ Object.defineProperty(exports, "__esModule", { value: true });
31
+ exports.appCheckErrorFromCryptoSignerError = exports.AppCheckTokenGenerator = void 0;
32
+ var validator = require("../utils/validator");
33
+ var utils_1 = require("../utils");
34
+ var crypto_signer_1 = require("../utils/crypto-signer");
35
+ var app_check_api_client_internal_1 = require("./app-check-api-client-internal");
36
+ var ONE_MINUTE_IN_SECONDS = 60;
37
+ var ONE_MINUTE_IN_MILLIS = ONE_MINUTE_IN_SECONDS * 1000;
38
+ var ONE_DAY_IN_MILLIS = 24 * 60 * 60 * 1000;
39
+ // Audience to use for Firebase App Check Custom tokens
40
+ var FIREBASE_APP_CHECK_AUDIENCE = 'https://firebaseappcheck.googleapis.com/google.firebase.appcheck.v1beta.TokenExchangeService';
41
+ /**
42
+ * Class for generating Firebase App Check tokens.
43
+ *
44
+ * @internal
45
+ */
46
+ var AppCheckTokenGenerator = /** @class */ (function () {
47
+ /**
48
+ * The AppCheckTokenGenerator class constructor.
49
+ *
50
+ * @param signer The CryptoSigner instance for this token generator.
51
+ * @constructor
52
+ */
53
+ function AppCheckTokenGenerator(signer) {
54
+ if (!validator.isNonNullObject(signer)) {
55
+ throw new app_check_api_client_internal_1.FirebaseAppCheckError('invalid-argument', 'INTERNAL ASSERT: Must provide a CryptoSigner to use AppCheckTokenGenerator.');
56
+ }
57
+ this.signer = signer;
58
+ }
59
+ /**
60
+ * Creates a new custom token that can be exchanged to an App Check token.
61
+ *
62
+ * @param appId The Application ID to use for the generated token.
63
+ *
64
+ * @return A Promise fulfilled with a custom token signed with a service account key
65
+ * that can be exchanged to an App Check token.
66
+ */
67
+ AppCheckTokenGenerator.prototype.createCustomToken = function (appId, options) {
68
+ var _this = this;
69
+ if (!validator.isNonEmptyString(appId)) {
70
+ throw new app_check_api_client_internal_1.FirebaseAppCheckError('invalid-argument', '`appId` must be a non-empty string.');
71
+ }
72
+ var customOptions = {};
73
+ if (typeof options !== 'undefined') {
74
+ customOptions = this.validateTokenOptions(options);
75
+ }
76
+ return this.signer.getAccountId().then(function (account) {
77
+ var header = {
78
+ alg: _this.signer.algorithm,
79
+ typ: 'JWT',
80
+ };
81
+ var iat = Math.floor(Date.now() / 1000);
82
+ var body = __assign({ iss: account, sub: account,
83
+ // eslint-disable-next-line @typescript-eslint/camelcase
84
+ app_id: appId, aud: FIREBASE_APP_CHECK_AUDIENCE, exp: iat + (ONE_MINUTE_IN_SECONDS * 5), iat: iat }, customOptions);
85
+ var token = _this.encodeSegment(header) + "." + _this.encodeSegment(body);
86
+ return _this.signer.sign(Buffer.from(token))
87
+ .then(function (signature) {
88
+ return token + "." + _this.encodeSegment(signature);
89
+ });
90
+ }).catch(function (err) {
91
+ throw appCheckErrorFromCryptoSignerError(err);
92
+ });
93
+ };
94
+ AppCheckTokenGenerator.prototype.encodeSegment = function (segment) {
95
+ var buffer = (segment instanceof Buffer) ? segment : Buffer.from(JSON.stringify(segment));
96
+ return utils_1.toWebSafeBase64(buffer).replace(/=+$/, '');
97
+ };
98
+ /**
99
+ * Checks if a given `AppCheckTokenOptions` object is valid. If successful, returns an object with
100
+ * custom properties.
101
+ *
102
+ * @param options An options object to be validated.
103
+ * @returns A custom object with ttl converted to protobuf Duration string format.
104
+ */
105
+ AppCheckTokenGenerator.prototype.validateTokenOptions = function (options) {
106
+ if (!validator.isNonNullObject(options)) {
107
+ throw new app_check_api_client_internal_1.FirebaseAppCheckError('invalid-argument', 'AppCheckTokenOptions must be a non-null object.');
108
+ }
109
+ if (typeof options.ttlMillis !== 'undefined') {
110
+ if (!validator.isNumber(options.ttlMillis)) {
111
+ throw new app_check_api_client_internal_1.FirebaseAppCheckError('invalid-argument', 'ttlMillis must be a duration in milliseconds.');
112
+ }
113
+ // ttlMillis must be between 30 minutes and 7 days (inclusive)
114
+ if (options.ttlMillis < (ONE_MINUTE_IN_MILLIS * 30) || options.ttlMillis > (ONE_DAY_IN_MILLIS * 7)) {
115
+ throw new app_check_api_client_internal_1.FirebaseAppCheckError('invalid-argument', 'ttlMillis must be a duration in milliseconds between 30 minutes and 7 days (inclusive).');
116
+ }
117
+ return { ttl: utils_1.transformMillisecondsToSecondsString(options.ttlMillis) };
118
+ }
119
+ return {};
120
+ };
121
+ return AppCheckTokenGenerator;
122
+ }());
123
+ exports.AppCheckTokenGenerator = AppCheckTokenGenerator;
124
+ /**
125
+ * Creates a new FirebaseAppCheckError by extracting the error code, message and other relevant
126
+ * details from a CryptoSignerError.
127
+ *
128
+ * @param err The Error to convert into a FirebaseAppCheckError error
129
+ * @return A Firebase App Check error that can be returned to the user.
130
+ */
131
+ function appCheckErrorFromCryptoSignerError(err) {
132
+ if (!(err instanceof crypto_signer_1.CryptoSignerError)) {
133
+ return err;
134
+ }
135
+ if (err.code === crypto_signer_1.CryptoSignerErrorCode.SERVER_ERROR && validator.isNonNullObject(err.cause)) {
136
+ var httpError = err.cause;
137
+ var errorResponse = httpError.response.data;
138
+ if (errorResponse === null || errorResponse === void 0 ? void 0 : errorResponse.error) {
139
+ var status = errorResponse.error.status;
140
+ var description = errorResponse.error.message || JSON.stringify(httpError.response);
141
+ var code = 'unknown-error';
142
+ if (status && status in app_check_api_client_internal_1.APP_CHECK_ERROR_CODE_MAPPING) {
143
+ code = app_check_api_client_internal_1.APP_CHECK_ERROR_CODE_MAPPING[status];
144
+ }
145
+ return new app_check_api_client_internal_1.FirebaseAppCheckError(code, "Error returned from server while signing a custom token: " + description);
146
+ }
147
+ return new app_check_api_client_internal_1.FirebaseAppCheckError('internal-error', 'Error returned from server: ' + JSON.stringify(errorResponse) + '.');
148
+ }
149
+ return new app_check_api_client_internal_1.FirebaseAppCheckError(mapToAppCheckErrorCode(err.code), err.message);
150
+ }
151
+ exports.appCheckErrorFromCryptoSignerError = appCheckErrorFromCryptoSignerError;
152
+ function mapToAppCheckErrorCode(code) {
153
+ switch (code) {
154
+ case crypto_signer_1.CryptoSignerErrorCode.INVALID_CREDENTIAL:
155
+ return 'invalid-credential';
156
+ case crypto_signer_1.CryptoSignerErrorCode.INVALID_ARGUMENT:
157
+ return 'invalid-argument';
158
+ default:
159
+ return 'internal-error';
160
+ }
161
+ }
@@ -0,0 +1,152 @@
1
+ /*! firebase-admin v9.12.0 */
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
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.AppCheckTokenVerifier = void 0;
20
+ var validator = require("../utils/validator");
21
+ var util = require("../utils/index");
22
+ var app_check_api_client_internal_1 = require("./app-check-api-client-internal");
23
+ var jwt_1 = require("../utils/jwt");
24
+ var APP_CHECK_ISSUER = 'https://firebaseappcheck.googleapis.com/';
25
+ var JWKS_URL = 'https://firebaseappcheck.googleapis.com/v1beta/jwks';
26
+ /**
27
+ * Class for verifying Firebase App Check tokens.
28
+ *
29
+ * @internal
30
+ */
31
+ var AppCheckTokenVerifier = /** @class */ (function () {
32
+ function AppCheckTokenVerifier(app) {
33
+ this.app = app;
34
+ this.signatureVerifier = jwt_1.PublicKeySignatureVerifier.withJwksUrl(JWKS_URL);
35
+ }
36
+ /**
37
+ * Verifies the format and signature of a Firebase App Check token.
38
+ *
39
+ * @param token The Firebase Auth JWT token to verify.
40
+ * @return A promise fulfilled with the decoded claims of the Firebase App Check token.
41
+ */
42
+ AppCheckTokenVerifier.prototype.verifyToken = function (token) {
43
+ var _this = this;
44
+ if (!validator.isString(token)) {
45
+ throw new app_check_api_client_internal_1.FirebaseAppCheckError('invalid-argument', 'App check token must be a non-null string.');
46
+ }
47
+ return this.ensureProjectId()
48
+ .then(function (projectId) {
49
+ return _this.decodeAndVerify(token, projectId);
50
+ })
51
+ .then(function (decoded) {
52
+ var decodedAppCheckToken = decoded.payload;
53
+ // eslint-disable-next-line @typescript-eslint/camelcase
54
+ decodedAppCheckToken.app_id = decodedAppCheckToken.sub;
55
+ return decodedAppCheckToken;
56
+ });
57
+ };
58
+ AppCheckTokenVerifier.prototype.ensureProjectId = function () {
59
+ return util.findProjectId(this.app)
60
+ .then(function (projectId) {
61
+ if (!validator.isNonEmptyString(projectId)) {
62
+ throw new app_check_api_client_internal_1.FirebaseAppCheckError('invalid-credential', 'Must initialize app with a cert credential or set your Firebase project ID as the ' +
63
+ 'GOOGLE_CLOUD_PROJECT environment variable to verify an App Check token.');
64
+ }
65
+ return projectId;
66
+ });
67
+ };
68
+ AppCheckTokenVerifier.prototype.decodeAndVerify = function (token, projectId) {
69
+ var _this = this;
70
+ return this.safeDecode(token)
71
+ .then(function (decodedToken) {
72
+ _this.verifyContent(decodedToken, projectId);
73
+ return _this.verifySignature(token)
74
+ .then(function () { return decodedToken; });
75
+ });
76
+ };
77
+ AppCheckTokenVerifier.prototype.safeDecode = function (jwtToken) {
78
+ return jwt_1.decodeJwt(jwtToken)
79
+ .catch(function () {
80
+ var errorMessage = 'Decoding App Check token failed. Make sure you passed ' +
81
+ 'the entire string JWT which represents the Firebase App Check token.';
82
+ throw new app_check_api_client_internal_1.FirebaseAppCheckError('invalid-argument', errorMessage);
83
+ });
84
+ };
85
+ /**
86
+ * Verifies the content of a Firebase App Check JWT.
87
+ *
88
+ * @param fullDecodedToken The decoded JWT.
89
+ * @param projectId The Firebase Project Id.
90
+ */
91
+ AppCheckTokenVerifier.prototype.verifyContent = function (fullDecodedToken, projectId) {
92
+ var header = fullDecodedToken.header;
93
+ var payload = fullDecodedToken.payload;
94
+ var projectIdMatchMessage = ' Make sure the App Check token comes from the same ' +
95
+ 'Firebase project as the service account used to authenticate this SDK.';
96
+ var scopedProjectId = "projects/" + projectId;
97
+ var errorMessage;
98
+ if (header.alg !== jwt_1.ALGORITHM_RS256) {
99
+ errorMessage = 'The provided App Check token has incorrect algorithm. Expected "' +
100
+ jwt_1.ALGORITHM_RS256 + '" but got ' + '"' + header.alg + '".';
101
+ }
102
+ else if (!validator.isNonEmptyArray(payload.aud) || !payload.aud.includes(scopedProjectId)) {
103
+ errorMessage = 'The provided App Check token has incorrect "aud" (audience) claim. Expected "' +
104
+ scopedProjectId + '" but got "' + payload.aud + '".' + projectIdMatchMessage;
105
+ }
106
+ else if (typeof payload.iss !== 'string' || !payload.iss.startsWith(APP_CHECK_ISSUER)) {
107
+ errorMessage = 'The provided App Check token has incorrect "iss" (issuer) claim.';
108
+ }
109
+ else if (typeof payload.sub !== 'string') {
110
+ errorMessage = 'The provided App Check token has no "sub" (subject) claim.';
111
+ }
112
+ else if (payload.sub === '') {
113
+ errorMessage = 'The provided App Check token has an empty string "sub" (subject) claim.';
114
+ }
115
+ if (errorMessage) {
116
+ throw new app_check_api_client_internal_1.FirebaseAppCheckError('invalid-argument', errorMessage);
117
+ }
118
+ };
119
+ AppCheckTokenVerifier.prototype.verifySignature = function (jwtToken) {
120
+ var _this = this;
121
+ return this.signatureVerifier.verify(jwtToken)
122
+ .catch(function (error) {
123
+ throw _this.mapJwtErrorToAppCheckError(error);
124
+ });
125
+ };
126
+ /**
127
+ * Maps JwtError to FirebaseAppCheckError
128
+ *
129
+ * @param error JwtError to be mapped.
130
+ * @returns FirebaseAppCheckError instance.
131
+ */
132
+ AppCheckTokenVerifier.prototype.mapJwtErrorToAppCheckError = function (error) {
133
+ if (error.code === jwt_1.JwtErrorCode.TOKEN_EXPIRED) {
134
+ var errorMessage = 'The provided App Check token has expired. Get a fresh App Check token' +
135
+ ' from your client app and try again.';
136
+ return new app_check_api_client_internal_1.FirebaseAppCheckError('app-check-token-expired', errorMessage);
137
+ }
138
+ else if (error.code === jwt_1.JwtErrorCode.INVALID_SIGNATURE) {
139
+ var errorMessage = 'The provided App Check token has invalid signature.';
140
+ return new app_check_api_client_internal_1.FirebaseAppCheckError('invalid-argument', errorMessage);
141
+ }
142
+ else if (error.code === jwt_1.JwtErrorCode.NO_MATCHING_KID) {
143
+ var errorMessage = 'The provided App Check token has "kid" claim which does not ' +
144
+ 'correspond to a known public key. Most likely the provided App Check token ' +
145
+ 'is expired, so get a fresh token from your client app and try again.';
146
+ return new app_check_api_client_internal_1.FirebaseAppCheckError('invalid-argument', errorMessage);
147
+ }
148
+ return new app_check_api_client_internal_1.FirebaseAppCheckError('invalid-argument', error.message);
149
+ };
150
+ return AppCheckTokenVerifier;
151
+ }());
152
+ exports.AppCheckTokenVerifier = AppCheckTokenVerifier;
@@ -0,0 +1,118 @@
1
+ /*! firebase-admin v9.12.0 */
2
+ "use strict";
3
+ /*!
4
+ * Copyright 2018 Google Inc.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.ActionCodeSettingsBuilder = void 0;
20
+ var validator = require("../utils/validator");
21
+ var error_1 = require("../utils/error");
22
+ /**
23
+ * Defines the ActionCodeSettings builder class used to convert the
24
+ * ActionCodeSettings object to its corresponding server request.
25
+ */
26
+ var ActionCodeSettingsBuilder = /** @class */ (function () {
27
+ /**
28
+ * ActionCodeSettingsBuilder constructor.
29
+ *
30
+ * @param {ActionCodeSettings} actionCodeSettings The ActionCodeSettings
31
+ * object used to initiliaze this server request builder.
32
+ * @constructor
33
+ */
34
+ function ActionCodeSettingsBuilder(actionCodeSettings) {
35
+ if (!validator.isNonNullObject(actionCodeSettings)) {
36
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, '"ActionCodeSettings" must be a non-null object.');
37
+ }
38
+ if (typeof actionCodeSettings.url === 'undefined') {
39
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.MISSING_CONTINUE_URI);
40
+ }
41
+ else if (!validator.isURL(actionCodeSettings.url)) {
42
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_CONTINUE_URI);
43
+ }
44
+ this.continueUrl = actionCodeSettings.url;
45
+ if (typeof actionCodeSettings.handleCodeInApp !== 'undefined' &&
46
+ !validator.isBoolean(actionCodeSettings.handleCodeInApp)) {
47
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, '"ActionCodeSettings.handleCodeInApp" must be a boolean.');
48
+ }
49
+ this.canHandleCodeInApp = actionCodeSettings.handleCodeInApp || false;
50
+ if (typeof actionCodeSettings.dynamicLinkDomain !== 'undefined' &&
51
+ !validator.isNonEmptyString(actionCodeSettings.dynamicLinkDomain)) {
52
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_DYNAMIC_LINK_DOMAIN);
53
+ }
54
+ this.dynamicLinkDomain = actionCodeSettings.dynamicLinkDomain;
55
+ if (typeof actionCodeSettings.iOS !== 'undefined') {
56
+ if (!validator.isNonNullObject(actionCodeSettings.iOS)) {
57
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, '"ActionCodeSettings.iOS" must be a valid non-null object.');
58
+ }
59
+ else if (typeof actionCodeSettings.iOS.bundleId === 'undefined') {
60
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.MISSING_IOS_BUNDLE_ID);
61
+ }
62
+ else if (!validator.isNonEmptyString(actionCodeSettings.iOS.bundleId)) {
63
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, '"ActionCodeSettings.iOS.bundleId" must be a valid non-empty string.');
64
+ }
65
+ this.ibi = actionCodeSettings.iOS.bundleId;
66
+ }
67
+ if (typeof actionCodeSettings.android !== 'undefined') {
68
+ if (!validator.isNonNullObject(actionCodeSettings.android)) {
69
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, '"ActionCodeSettings.android" must be a valid non-null object.');
70
+ }
71
+ else if (typeof actionCodeSettings.android.packageName === 'undefined') {
72
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.MISSING_ANDROID_PACKAGE_NAME);
73
+ }
74
+ else if (!validator.isNonEmptyString(actionCodeSettings.android.packageName)) {
75
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, '"ActionCodeSettings.android.packageName" must be a valid non-empty string.');
76
+ }
77
+ else if (typeof actionCodeSettings.android.minimumVersion !== 'undefined' &&
78
+ !validator.isNonEmptyString(actionCodeSettings.android.minimumVersion)) {
79
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, '"ActionCodeSettings.android.minimumVersion" must be a valid non-empty string.');
80
+ }
81
+ else if (typeof actionCodeSettings.android.installApp !== 'undefined' &&
82
+ !validator.isBoolean(actionCodeSettings.android.installApp)) {
83
+ throw new error_1.FirebaseAuthError(error_1.AuthClientErrorCode.INVALID_ARGUMENT, '"ActionCodeSettings.android.installApp" must be a valid boolean.');
84
+ }
85
+ this.apn = actionCodeSettings.android.packageName;
86
+ this.amv = actionCodeSettings.android.minimumVersion;
87
+ this.installApp = actionCodeSettings.android.installApp || false;
88
+ }
89
+ }
90
+ /**
91
+ * Returns the corresponding constructed server request corresponding to the
92
+ * current ActionCodeSettings.
93
+ *
94
+ * @return {EmailActionCodeRequest} The constructed EmailActionCodeRequest request.
95
+ */
96
+ ActionCodeSettingsBuilder.prototype.buildRequest = function () {
97
+ var request = {
98
+ continueUrl: this.continueUrl,
99
+ canHandleCodeInApp: this.canHandleCodeInApp,
100
+ dynamicLinkDomain: this.dynamicLinkDomain,
101
+ androidPackageName: this.apn,
102
+ androidMinimumVersion: this.amv,
103
+ androidInstallApp: this.installApp,
104
+ iOSBundleId: this.ibi,
105
+ };
106
+ // Remove all null and undefined fields from request.
107
+ for (var key in request) {
108
+ if (Object.prototype.hasOwnProperty.call(request, key)) {
109
+ if (typeof request[key] === 'undefined' || request[key] === null) {
110
+ delete request[key];
111
+ }
112
+ }
113
+ }
114
+ return request;
115
+ };
116
+ return ActionCodeSettingsBuilder;
117
+ }());
118
+ exports.ActionCodeSettingsBuilder = ActionCodeSettingsBuilder;