1 | /*! firebase-admin v10.0.0 */
|
2 | ;
|
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 });
|
20 | exports.AppCheck = void 0;
|
21 | var app_check_api_client_internal_1 = require("./app-check-api-client-internal");
|
22 | var token_generator_1 = require("./token-generator");
|
23 | var token_verifier_1 = require("./token-verifier");
|
24 | var crypto_signer_1 = require("../utils/crypto-signer");
|
25 | /**
|
26 | * The Firebase `AppCheck` service interface.
|
27 | */
|
28 | var AppCheck = /** @class */ (function () {
|
29 | /**
|
30 | * @param app - The app for this AppCheck service.
|
31 | * @constructor
|
32 | * @internal
|
33 | */
|
34 | function AppCheck(app) {
|
35 | this.app = app;
|
36 | this.client = new app_check_api_client_internal_1.AppCheckApiClient(app);
|
37 | try {
|
38 | this.tokenGenerator = new token_generator_1.AppCheckTokenGenerator(crypto_signer_1.cryptoSignerFromApp(app));
|
39 | }
|
40 | catch (err) {
|
41 | throw token_generator_1.appCheckErrorFromCryptoSignerError(err);
|
42 | }
|
43 | this.appCheckTokenVerifier = new token_verifier_1.AppCheckTokenVerifier(app);
|
44 | }
|
45 | /**
|
46 | * Creates a new {@link AppCheckToken} that can be sent
|
47 | * back to a client.
|
48 | *
|
49 | * @param appId - The app ID to use as the JWT app_id.
|
50 | * @param options - Optional options object when creating a new App Check Token.
|
51 | *
|
52 | * @returns A promise that fulfills with a `AppCheckToken`.
|
53 | */
|
54 | AppCheck.prototype.createToken = function (appId, options) {
|
55 | var _this = this;
|
56 | return this.tokenGenerator.createCustomToken(appId, options)
|
57 | .then(function (customToken) {
|
58 | return _this.client.exchangeToken(customToken, appId);
|
59 | });
|
60 | };
|
61 | /**
|
62 | * Verifies a Firebase App Check token (JWT). If the token is valid, the promise is
|
63 | * fulfilled with the token's decoded claims; otherwise, the promise is
|
64 | * rejected.
|
65 | *
|
66 | * @param appCheckToken - The App Check token to verify.
|
67 | *
|
68 | * @returns A promise fulfilled with the token's decoded claims
|
69 | * if the App Check token is valid; otherwise, a rejected promise.
|
70 | */
|
71 | AppCheck.prototype.verifyToken = function (appCheckToken) {
|
72 | return this.appCheckTokenVerifier.verifyToken(appCheckToken)
|
73 | .then(function (decodedToken) {
|
74 | return {
|
75 | appId: decodedToken.app_id,
|
76 | token: decodedToken,
|
77 | };
|
78 | });
|
79 | };
|
80 | return AppCheck;
|
81 | }());
|
82 | exports.AppCheck = AppCheck;
|