app-check-api-client-internal.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*! firebase-admin v12.1.1 */
  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 });
  20. exports.FirebaseAppCheckError = exports.APP_CHECK_ERROR_CODE_MAPPING = exports.AppCheckApiClient = void 0;
  21. const api_request_1 = require("../utils/api-request");
  22. const error_1 = require("../utils/error");
  23. const utils = require("../utils/index");
  24. const validator = require("../utils/validator");
  25. // App Check backend constants
  26. const FIREBASE_APP_CHECK_V1_API_URL_FORMAT = 'https://firebaseappcheck.googleapis.com/v1/projects/{projectId}/apps/{appId}:exchangeCustomToken';
  27. const ONE_TIME_USE_TOKEN_VERIFICATION_URL_FORMAT = 'https://firebaseappcheck.googleapis.com/v1beta/projects/{projectId}:verifyAppCheckToken';
  28. const FIREBASE_APP_CHECK_CONFIG_HEADERS = {
  29. 'X-Firebase-Client': `fire-admin-node/${utils.getSdkVersion()}`
  30. };
  31. /**
  32. * Class that facilitates sending requests to the Firebase App Check backend API.
  33. *
  34. * @internal
  35. */
  36. class AppCheckApiClient {
  37. constructor(app) {
  38. this.app = app;
  39. if (!validator.isNonNullObject(app) || !('options' in app)) {
  40. throw new FirebaseAppCheckError('invalid-argument', 'First argument passed to admin.appCheck() must be a valid Firebase app instance.');
  41. }
  42. this.httpClient = new api_request_1.AuthorizedHttpClient(app);
  43. }
  44. /**
  45. * Exchange a signed custom token to App Check token
  46. *
  47. * @param customToken - The custom token to be exchanged.
  48. * @param appId - The mobile App ID.
  49. * @returns A promise that fulfills with a `AppCheckToken`.
  50. */
  51. exchangeToken(customToken, appId) {
  52. if (!validator.isNonEmptyString(appId)) {
  53. throw new FirebaseAppCheckError('invalid-argument', '`appId` must be a non-empty string.');
  54. }
  55. if (!validator.isNonEmptyString(customToken)) {
  56. throw new FirebaseAppCheckError('invalid-argument', '`customToken` must be a non-empty string.');
  57. }
  58. return this.getUrl(appId)
  59. .then((url) => {
  60. const request = {
  61. method: 'POST',
  62. url,
  63. headers: FIREBASE_APP_CHECK_CONFIG_HEADERS,
  64. data: { customToken }
  65. };
  66. return this.httpClient.send(request);
  67. })
  68. .then((resp) => {
  69. return this.toAppCheckToken(resp);
  70. })
  71. .catch((err) => {
  72. throw this.toFirebaseError(err);
  73. });
  74. }
  75. verifyReplayProtection(token) {
  76. if (!validator.isNonEmptyString(token)) {
  77. throw new FirebaseAppCheckError('invalid-argument', '`token` must be a non-empty string.');
  78. }
  79. return this.getVerifyTokenUrl()
  80. .then((url) => {
  81. const request = {
  82. method: 'POST',
  83. url,
  84. headers: FIREBASE_APP_CHECK_CONFIG_HEADERS,
  85. data: { app_check_token: token }
  86. };
  87. return this.httpClient.send(request);
  88. })
  89. .then((resp) => {
  90. if (typeof resp.data.alreadyConsumed !== 'undefined'
  91. && !validator.isBoolean(resp.data?.alreadyConsumed)) {
  92. throw new FirebaseAppCheckError('invalid-argument', '`alreadyConsumed` must be a boolean value.');
  93. }
  94. return resp.data.alreadyConsumed || false;
  95. })
  96. .catch((err) => {
  97. throw this.toFirebaseError(err);
  98. });
  99. }
  100. getUrl(appId) {
  101. return this.getProjectId()
  102. .then((projectId) => {
  103. const urlParams = {
  104. projectId,
  105. appId,
  106. };
  107. const baseUrl = utils.formatString(FIREBASE_APP_CHECK_V1_API_URL_FORMAT, urlParams);
  108. return utils.formatString(baseUrl);
  109. });
  110. }
  111. getVerifyTokenUrl() {
  112. return this.getProjectId()
  113. .then((projectId) => {
  114. const urlParams = {
  115. projectId
  116. };
  117. const baseUrl = utils.formatString(ONE_TIME_USE_TOKEN_VERIFICATION_URL_FORMAT, urlParams);
  118. return utils.formatString(baseUrl);
  119. });
  120. }
  121. getProjectId() {
  122. if (this.projectId) {
  123. return Promise.resolve(this.projectId);
  124. }
  125. return utils.findProjectId(this.app)
  126. .then((projectId) => {
  127. if (!validator.isNonEmptyString(projectId)) {
  128. throw new FirebaseAppCheckError('unknown-error', 'Failed to determine project ID. Initialize the '
  129. + 'SDK with service account credentials or set project ID as an app option. '
  130. + 'Alternatively, set the GOOGLE_CLOUD_PROJECT environment variable.');
  131. }
  132. this.projectId = projectId;
  133. return projectId;
  134. });
  135. }
  136. toFirebaseError(err) {
  137. if (err instanceof error_1.PrefixedFirebaseError) {
  138. return err;
  139. }
  140. const response = err.response;
  141. if (!response.isJson()) {
  142. return new FirebaseAppCheckError('unknown-error', `Unexpected response with status: ${response.status} and body: ${response.text}`);
  143. }
  144. const error = response.data.error || {};
  145. let code = 'unknown-error';
  146. if (error.status && error.status in exports.APP_CHECK_ERROR_CODE_MAPPING) {
  147. code = exports.APP_CHECK_ERROR_CODE_MAPPING[error.status];
  148. }
  149. const message = error.message || `Unknown server error: ${response.text}`;
  150. return new FirebaseAppCheckError(code, message);
  151. }
  152. /**
  153. * Creates an AppCheckToken from the API response.
  154. *
  155. * @param resp - API response object.
  156. * @returns An AppCheckToken instance.
  157. */
  158. toAppCheckToken(resp) {
  159. const token = resp.data.token;
  160. // `ttl` is a string with the suffix "s" preceded by the number of seconds,
  161. // with nanoseconds expressed as fractional seconds.
  162. const ttlMillis = this.stringToMilliseconds(resp.data.ttl);
  163. return {
  164. token,
  165. ttlMillis
  166. };
  167. }
  168. /**
  169. * Converts a duration string with the suffix `s` to milliseconds.
  170. *
  171. * @param duration - The duration as a string with the suffix "s" preceded by the
  172. * number of seconds, with fractional seconds. For example, 3 seconds with 0 nanoseconds
  173. * is expressed as "3s", while 3 seconds and 1 nanosecond is expressed as "3.000000001s",
  174. * and 3 seconds and 1 microsecond is expressed as "3.000001s".
  175. *
  176. * @returns The duration in milliseconds.
  177. */
  178. stringToMilliseconds(duration) {
  179. if (!validator.isNonEmptyString(duration) || !duration.endsWith('s')) {
  180. throw new FirebaseAppCheckError('invalid-argument', '`ttl` must be a valid duration string with the suffix `s`.');
  181. }
  182. const seconds = duration.slice(0, -1);
  183. return Math.floor(Number(seconds) * 1000);
  184. }
  185. }
  186. exports.AppCheckApiClient = AppCheckApiClient;
  187. exports.APP_CHECK_ERROR_CODE_MAPPING = {
  188. ABORTED: 'aborted',
  189. INVALID_ARGUMENT: 'invalid-argument',
  190. INVALID_CREDENTIAL: 'invalid-credential',
  191. INTERNAL: 'internal-error',
  192. PERMISSION_DENIED: 'permission-denied',
  193. UNAUTHENTICATED: 'unauthenticated',
  194. NOT_FOUND: 'not-found',
  195. UNKNOWN: 'unknown-error',
  196. };
  197. /**
  198. * Firebase App Check error code structure. This extends PrefixedFirebaseError.
  199. *
  200. * @param code - The error code.
  201. * @param message - The error message.
  202. * @constructor
  203. */
  204. class FirebaseAppCheckError extends error_1.PrefixedFirebaseError {
  205. constructor(code, message) {
  206. super('app-check', code, message);
  207. /* tslint:disable:max-line-length */
  208. // Set the prototype explicitly. See the following link for more details:
  209. // https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work
  210. /* tslint:enable:max-line-length */
  211. this.__proto__ = FirebaseAppCheckError.prototype;
  212. }
  213. }
  214. exports.FirebaseAppCheckError = FirebaseAppCheckError;