UNPKG

7.76 kBJavaScriptView Raw
1/*! firebase-admin v10.0.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 */
19var __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};
30Object.defineProperty(exports, "__esModule", { value: true });
31exports.appCheckErrorFromCryptoSignerError = exports.AppCheckTokenGenerator = void 0;
32var validator = require("../utils/validator");
33var utils_1 = require("../utils");
34var crypto_signer_1 = require("../utils/crypto-signer");
35var app_check_api_client_internal_1 = require("./app-check-api-client-internal");
36var ONE_MINUTE_IN_SECONDS = 60;
37var ONE_MINUTE_IN_MILLIS = ONE_MINUTE_IN_SECONDS * 1000;
38var ONE_DAY_IN_MILLIS = 24 * 60 * 60 * 1000;
39// Audience to use for Firebase App Check Custom tokens
40var 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 */
46var 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 * @returns 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}());
123exports.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 * @returns A Firebase App Check error that can be returned to the user.
130 */
131function 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}
151exports.appCheckErrorFromCryptoSignerError = appCheckErrorFromCryptoSignerError;
152function 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}