token-verifier.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*! firebase-admin v12.1.1 */
  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. const validator = require("../utils/validator");
  21. const util = require("../utils/index");
  22. const app_check_api_client_internal_1 = require("./app-check-api-client-internal");
  23. const jwt_1 = require("../utils/jwt");
  24. const APP_CHECK_ISSUER = 'https://firebaseappcheck.googleapis.com/';
  25. const JWKS_URL = 'https://firebaseappcheck.googleapis.com/v1/jwks';
  26. /**
  27. * Class for verifying Firebase App Check tokens.
  28. *
  29. * @internal
  30. */
  31. class AppCheckTokenVerifier {
  32. constructor(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. * @returns A promise fulfilled with the decoded claims of the Firebase App Check token.
  41. */
  42. verifyToken(token) {
  43. if (!validator.isString(token)) {
  44. throw new app_check_api_client_internal_1.FirebaseAppCheckError('invalid-argument', 'App check token must be a non-null string.');
  45. }
  46. return this.ensureProjectId()
  47. .then((projectId) => {
  48. return this.decodeAndVerify(token, projectId);
  49. })
  50. .then((decoded) => {
  51. const decodedAppCheckToken = decoded.payload;
  52. decodedAppCheckToken.app_id = decodedAppCheckToken.sub;
  53. return decodedAppCheckToken;
  54. });
  55. }
  56. ensureProjectId() {
  57. return util.findProjectId(this.app)
  58. .then((projectId) => {
  59. if (!validator.isNonEmptyString(projectId)) {
  60. 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 ' +
  61. 'GOOGLE_CLOUD_PROJECT environment variable to verify an App Check token.');
  62. }
  63. return projectId;
  64. });
  65. }
  66. decodeAndVerify(token, projectId) {
  67. return this.safeDecode(token)
  68. .then((decodedToken) => {
  69. this.verifyContent(decodedToken, projectId);
  70. return this.verifySignature(token)
  71. .then(() => decodedToken);
  72. });
  73. }
  74. safeDecode(jwtToken) {
  75. return (0, jwt_1.decodeJwt)(jwtToken)
  76. .catch(() => {
  77. const errorMessage = 'Decoding App Check token failed. Make sure you passed ' +
  78. 'the entire string JWT which represents the Firebase App Check token.';
  79. throw new app_check_api_client_internal_1.FirebaseAppCheckError('invalid-argument', errorMessage);
  80. });
  81. }
  82. /**
  83. * Verifies the content of a Firebase App Check JWT.
  84. *
  85. * @param fullDecodedToken - The decoded JWT.
  86. * @param projectId - The Firebase Project Id.
  87. */
  88. verifyContent(fullDecodedToken, projectId) {
  89. const header = fullDecodedToken.header;
  90. const payload = fullDecodedToken.payload;
  91. const projectIdMatchMessage = ' Make sure the App Check token comes from the same ' +
  92. 'Firebase project as the service account used to authenticate this SDK.';
  93. const scopedProjectId = `projects/${projectId}`;
  94. let errorMessage;
  95. if (header.alg !== jwt_1.ALGORITHM_RS256) {
  96. errorMessage = 'The provided App Check token has incorrect algorithm. Expected "' +
  97. jwt_1.ALGORITHM_RS256 + '" but got ' + '"' + header.alg + '".';
  98. }
  99. else if (!validator.isNonEmptyArray(payload.aud) || !payload.aud.includes(scopedProjectId)) {
  100. errorMessage = 'The provided App Check token has incorrect "aud" (audience) claim. Expected "' +
  101. scopedProjectId + '" but got "' + payload.aud + '".' + projectIdMatchMessage;
  102. }
  103. else if (typeof payload.iss !== 'string' || !payload.iss.startsWith(APP_CHECK_ISSUER)) {
  104. errorMessage = 'The provided App Check token has incorrect "iss" (issuer) claim.';
  105. }
  106. else if (typeof payload.sub !== 'string') {
  107. errorMessage = 'The provided App Check token has no "sub" (subject) claim.';
  108. }
  109. else if (payload.sub === '') {
  110. errorMessage = 'The provided App Check token has an empty string "sub" (subject) claim.';
  111. }
  112. if (errorMessage) {
  113. throw new app_check_api_client_internal_1.FirebaseAppCheckError('invalid-argument', errorMessage);
  114. }
  115. }
  116. verifySignature(jwtToken) {
  117. return this.signatureVerifier.verify(jwtToken)
  118. .catch((error) => {
  119. throw this.mapJwtErrorToAppCheckError(error);
  120. });
  121. }
  122. /**
  123. * Maps JwtError to FirebaseAppCheckError
  124. *
  125. * @param error - JwtError to be mapped.
  126. * @returns FirebaseAppCheckError instance.
  127. */
  128. mapJwtErrorToAppCheckError(error) {
  129. if (error.code === jwt_1.JwtErrorCode.TOKEN_EXPIRED) {
  130. const errorMessage = 'The provided App Check token has expired. Get a fresh App Check token' +
  131. ' from your client app and try again.';
  132. return new app_check_api_client_internal_1.FirebaseAppCheckError('app-check-token-expired', errorMessage);
  133. }
  134. else if (error.code === jwt_1.JwtErrorCode.INVALID_SIGNATURE) {
  135. const errorMessage = 'The provided App Check token has invalid signature.';
  136. return new app_check_api_client_internal_1.FirebaseAppCheckError('invalid-argument', errorMessage);
  137. }
  138. else if (error.code === jwt_1.JwtErrorCode.NO_MATCHING_KID) {
  139. const errorMessage = 'The provided App Check token has "kid" claim which does not ' +
  140. 'correspond to a known public key. Most likely the provided App Check token ' +
  141. 'is expired, so get a fresh token from your client app and try again.';
  142. return new app_check_api_client_internal_1.FirebaseAppCheckError('invalid-argument', errorMessage);
  143. }
  144. return new app_check_api_client_internal_1.FirebaseAppCheckError('invalid-argument', error.message);
  145. }
  146. }
  147. exports.AppCheckTokenVerifier = AppCheckTokenVerifier;