firebase-admin
Version:
Firebase admin SDK for Node.js
83 lines (82 loc) • 3.07 kB
JavaScript
/*! firebase-admin v10.0.0 */
;
/*!
* @license
* Copyright 2021 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.AppCheck = void 0;
var app_check_api_client_internal_1 = require("./app-check-api-client-internal");
var token_generator_1 = require("./token-generator");
var token_verifier_1 = require("./token-verifier");
var crypto_signer_1 = require("../utils/crypto-signer");
/**
* The Firebase `AppCheck` service interface.
*/
var AppCheck = /** @class */ (function () {
/**
* @param app - The app for this AppCheck service.
* @constructor
* @internal
*/
function AppCheck(app) {
this.app = app;
this.client = new app_check_api_client_internal_1.AppCheckApiClient(app);
try {
this.tokenGenerator = new token_generator_1.AppCheckTokenGenerator(crypto_signer_1.cryptoSignerFromApp(app));
}
catch (err) {
throw token_generator_1.appCheckErrorFromCryptoSignerError(err);
}
this.appCheckTokenVerifier = new token_verifier_1.AppCheckTokenVerifier(app);
}
/**
* Creates a new {@link AppCheckToken} that can be sent
* back to a client.
*
* @param appId - The app ID to use as the JWT app_id.
* @param options - Optional options object when creating a new App Check Token.
*
* @returns A promise that fulfills with a `AppCheckToken`.
*/
AppCheck.prototype.createToken = function (appId, options) {
var _this = this;
return this.tokenGenerator.createCustomToken(appId, options)
.then(function (customToken) {
return _this.client.exchangeToken(customToken, appId);
});
};
/**
* Verifies a Firebase App Check token (JWT). If the token is valid, the promise is
* fulfilled with the token's decoded claims; otherwise, the promise is
* rejected.
*
* @param appCheckToken - The App Check token to verify.
*
* @returns A promise fulfilled with the token's decoded claims
* if the App Check token is valid; otherwise, a rejected promise.
*/
AppCheck.prototype.verifyToken = function (appCheckToken) {
return this.appCheckTokenVerifier.verifyToken(appCheckToken)
.then(function (decodedToken) {
return {
appId: decodedToken.app_id,
token: decodedToken,
};
});
};
return AppCheck;
}());
exports.AppCheck = AppCheck;