crypto-signer.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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.CryptoSignerErrorCode = exports.CryptoSignerError = exports.cryptoSignerFromApp = exports.IAMSigner = exports.ServiceAccountSigner = void 0;
  21. const credential_internal_1 = require("../app/credential-internal");
  22. const api_request_1 = require("./api-request");
  23. const validator = require("../utils/validator");
  24. const ALGORITHM_RS256 = 'RS256';
  25. /**
  26. * A CryptoSigner implementation that uses an explicitly specified service account private key to
  27. * sign data. Performs all operations locally, and does not make any RPC calls.
  28. */
  29. class ServiceAccountSigner {
  30. /**
  31. * Creates a new CryptoSigner instance from the given service account credential.
  32. *
  33. * @param credential - A service account credential.
  34. */
  35. constructor(credential) {
  36. this.credential = credential;
  37. this.algorithm = ALGORITHM_RS256;
  38. if (!credential) {
  39. throw new CryptoSignerError({
  40. code: CryptoSignerErrorCode.INVALID_CREDENTIAL,
  41. message: 'INTERNAL ASSERT: Must provide a service account credential to initialize ServiceAccountSigner.',
  42. });
  43. }
  44. }
  45. /**
  46. * @inheritDoc
  47. */
  48. sign(buffer) {
  49. const crypto = require('crypto'); // eslint-disable-line @typescript-eslint/no-var-requires
  50. const sign = crypto.createSign('RSA-SHA256');
  51. sign.update(buffer);
  52. return Promise.resolve(sign.sign(this.credential.privateKey));
  53. }
  54. /**
  55. * @inheritDoc
  56. */
  57. getAccountId() {
  58. return Promise.resolve(this.credential.clientEmail);
  59. }
  60. }
  61. exports.ServiceAccountSigner = ServiceAccountSigner;
  62. /**
  63. * A CryptoSigner implementation that uses the remote IAM service to sign data. If initialized without
  64. * a service account ID, attempts to discover a service account ID by consulting the local Metadata
  65. * service. This will succeed in managed environments like Google Cloud Functions and App Engine.
  66. *
  67. * @see https://cloud.google.com/iam/reference/rest/v1/projects.serviceAccounts/signBlob
  68. * @see https://cloud.google.com/compute/docs/storing-retrieving-metadata
  69. */
  70. class IAMSigner {
  71. constructor(httpClient, serviceAccountId) {
  72. this.algorithm = ALGORITHM_RS256;
  73. if (!httpClient) {
  74. throw new CryptoSignerError({
  75. code: CryptoSignerErrorCode.INVALID_ARGUMENT,
  76. message: 'INTERNAL ASSERT: Must provide a HTTP client to initialize IAMSigner.',
  77. });
  78. }
  79. if (typeof serviceAccountId !== 'undefined' && !validator.isNonEmptyString(serviceAccountId)) {
  80. throw new CryptoSignerError({
  81. code: CryptoSignerErrorCode.INVALID_ARGUMENT,
  82. message: 'INTERNAL ASSERT: Service account ID must be undefined or a non-empty string.',
  83. });
  84. }
  85. this.httpClient = httpClient;
  86. this.serviceAccountId = serviceAccountId;
  87. }
  88. /**
  89. * @inheritDoc
  90. */
  91. sign(buffer) {
  92. return this.getAccountId().then((serviceAccount) => {
  93. const request = {
  94. method: 'POST',
  95. url: `https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/${serviceAccount}:signBlob`,
  96. data: { payload: buffer.toString('base64') },
  97. };
  98. return this.httpClient.send(request);
  99. }).then((response) => {
  100. // Response from IAM is base64 encoded. Decode it into a buffer and return.
  101. return Buffer.from(response.data.signedBlob, 'base64');
  102. }).catch((err) => {
  103. if (err instanceof api_request_1.HttpError) {
  104. throw new CryptoSignerError({
  105. code: CryptoSignerErrorCode.SERVER_ERROR,
  106. message: err.message,
  107. cause: err
  108. });
  109. }
  110. throw err;
  111. });
  112. }
  113. /**
  114. * @inheritDoc
  115. */
  116. getAccountId() {
  117. if (validator.isNonEmptyString(this.serviceAccountId)) {
  118. return Promise.resolve(this.serviceAccountId);
  119. }
  120. const request = {
  121. method: 'GET',
  122. url: 'http://metadata/computeMetadata/v1/instance/service-accounts/default/email',
  123. headers: {
  124. 'Metadata-Flavor': 'Google',
  125. },
  126. };
  127. const client = new api_request_1.HttpClient();
  128. return client.send(request).then((response) => {
  129. if (!response.text) {
  130. throw new CryptoSignerError({
  131. code: CryptoSignerErrorCode.INTERNAL_ERROR,
  132. message: 'HTTP Response missing payload',
  133. });
  134. }
  135. this.serviceAccountId = response.text;
  136. return response.text;
  137. }).catch((err) => {
  138. throw new CryptoSignerError({
  139. code: CryptoSignerErrorCode.INVALID_CREDENTIAL,
  140. message: 'Failed to determine service account. Make sure to initialize ' +
  141. 'the SDK with a service account credential. Alternatively specify a service ' +
  142. `account with iam.serviceAccounts.signBlob permission. Original error: ${err}`,
  143. });
  144. });
  145. }
  146. }
  147. exports.IAMSigner = IAMSigner;
  148. /**
  149. * Creates a new CryptoSigner instance for the given app. If the app has been initialized with a
  150. * service account credential, creates a ServiceAccountSigner.
  151. *
  152. * @param app - A FirebaseApp instance.
  153. * @returns A CryptoSigner instance.
  154. */
  155. function cryptoSignerFromApp(app) {
  156. const credential = app.options.credential;
  157. if (credential instanceof credential_internal_1.ServiceAccountCredential) {
  158. return new ServiceAccountSigner(credential);
  159. }
  160. return new IAMSigner(new api_request_1.AuthorizedHttpClient(app), app.options.serviceAccountId);
  161. }
  162. exports.cryptoSignerFromApp = cryptoSignerFromApp;
  163. /**
  164. * CryptoSigner error code structure.
  165. *
  166. * @param errorInfo - The error information (code and message).
  167. * @constructor
  168. */
  169. class CryptoSignerError extends Error {
  170. constructor(errorInfo) {
  171. super(errorInfo.message);
  172. this.errorInfo = errorInfo;
  173. /* tslint:disable:max-line-length */
  174. // Set the prototype explicitly. See the following link for more details:
  175. // https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work
  176. /* tslint:enable:max-line-length */
  177. this.__proto__ = CryptoSignerError.prototype;
  178. }
  179. /** @returns The error code. */
  180. get code() {
  181. return this.errorInfo.code;
  182. }
  183. /** @returns The error message. */
  184. get message() {
  185. return this.errorInfo.message;
  186. }
  187. /** @returns The error data. */
  188. get cause() {
  189. return this.errorInfo.cause;
  190. }
  191. }
  192. exports.CryptoSignerError = CryptoSignerError;
  193. /**
  194. * Crypto Signer error codes and their default messages.
  195. */
  196. class CryptoSignerErrorCode {
  197. }
  198. exports.CryptoSignerErrorCode = CryptoSignerErrorCode;
  199. CryptoSignerErrorCode.INVALID_ARGUMENT = 'invalid-argument';
  200. CryptoSignerErrorCode.INTERNAL_ERROR = 'internal-error';
  201. CryptoSignerErrorCode.INVALID_CREDENTIAL = 'invalid-credential';
  202. CryptoSignerErrorCode.SERVER_ERROR = 'server-error';