123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.CryptoSignerErrorCode = exports.CryptoSignerError = exports.cryptoSignerFromApp = exports.IAMSigner = exports.ServiceAccountSigner = void 0;
- const credential_internal_1 = require("../app/credential-internal");
- const api_request_1 = require("./api-request");
- const validator = require("../utils/validator");
- const ALGORITHM_RS256 = 'RS256';
- class ServiceAccountSigner {
-
- constructor(credential) {
- this.credential = credential;
- this.algorithm = ALGORITHM_RS256;
- if (!credential) {
- throw new CryptoSignerError({
- code: CryptoSignerErrorCode.INVALID_CREDENTIAL,
- message: 'INTERNAL ASSERT: Must provide a service account credential to initialize ServiceAccountSigner.',
- });
- }
- }
-
- sign(buffer) {
- const crypto = require('crypto');
- const sign = crypto.createSign('RSA-SHA256');
- sign.update(buffer);
- return Promise.resolve(sign.sign(this.credential.privateKey));
- }
-
- getAccountId() {
- return Promise.resolve(this.credential.clientEmail);
- }
- }
- exports.ServiceAccountSigner = ServiceAccountSigner;
- class IAMSigner {
- constructor(httpClient, serviceAccountId) {
- this.algorithm = ALGORITHM_RS256;
- if (!httpClient) {
- throw new CryptoSignerError({
- code: CryptoSignerErrorCode.INVALID_ARGUMENT,
- message: 'INTERNAL ASSERT: Must provide a HTTP client to initialize IAMSigner.',
- });
- }
- if (typeof serviceAccountId !== 'undefined' && !validator.isNonEmptyString(serviceAccountId)) {
- throw new CryptoSignerError({
- code: CryptoSignerErrorCode.INVALID_ARGUMENT,
- message: 'INTERNAL ASSERT: Service account ID must be undefined or a non-empty string.',
- });
- }
- this.httpClient = httpClient;
- this.serviceAccountId = serviceAccountId;
- }
-
- sign(buffer) {
- return this.getAccountId().then((serviceAccount) => {
- const request = {
- method: 'POST',
- url: `https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/${serviceAccount}:signBlob`,
- data: { payload: buffer.toString('base64') },
- };
- return this.httpClient.send(request);
- }).then((response) => {
-
- return Buffer.from(response.data.signedBlob, 'base64');
- }).catch((err) => {
- if (err instanceof api_request_1.HttpError) {
- throw new CryptoSignerError({
- code: CryptoSignerErrorCode.SERVER_ERROR,
- message: err.message,
- cause: err
- });
- }
- throw err;
- });
- }
-
- getAccountId() {
- if (validator.isNonEmptyString(this.serviceAccountId)) {
- return Promise.resolve(this.serviceAccountId);
- }
- const request = {
- method: 'GET',
- url: 'http://metadata/computeMetadata/v1/instance/service-accounts/default/email',
- headers: {
- 'Metadata-Flavor': 'Google',
- },
- };
- const client = new api_request_1.HttpClient();
- return client.send(request).then((response) => {
- if (!response.text) {
- throw new CryptoSignerError({
- code: CryptoSignerErrorCode.INTERNAL_ERROR,
- message: 'HTTP Response missing payload',
- });
- }
- this.serviceAccountId = response.text;
- return response.text;
- }).catch((err) => {
- throw new CryptoSignerError({
- code: CryptoSignerErrorCode.INVALID_CREDENTIAL,
- message: 'Failed to determine service account. Make sure to initialize ' +
- 'the SDK with a service account credential. Alternatively specify a service ' +
- `account with iam.serviceAccounts.signBlob permission. Original error: ${err}`,
- });
- });
- }
- }
- exports.IAMSigner = IAMSigner;
- function cryptoSignerFromApp(app) {
- const credential = app.options.credential;
- if (credential instanceof credential_internal_1.ServiceAccountCredential) {
- return new ServiceAccountSigner(credential);
- }
- return new IAMSigner(new api_request_1.AuthorizedHttpClient(app), app.options.serviceAccountId);
- }
- exports.cryptoSignerFromApp = cryptoSignerFromApp;
- class CryptoSignerError extends Error {
- constructor(errorInfo) {
- super(errorInfo.message);
- this.errorInfo = errorInfo;
-
-
-
-
- this.__proto__ = CryptoSignerError.prototype;
- }
-
- get code() {
- return this.errorInfo.code;
- }
-
- get message() {
- return this.errorInfo.message;
- }
-
- get cause() {
- return this.errorInfo.cause;
- }
- }
- exports.CryptoSignerError = CryptoSignerError;
- class CryptoSignerErrorCode {
- }
- exports.CryptoSignerErrorCode = CryptoSignerErrorCode;
- CryptoSignerErrorCode.INVALID_ARGUMENT = 'invalid-argument';
- CryptoSignerErrorCode.INTERNAL_ERROR = 'internal-error';
- CryptoSignerErrorCode.INVALID_CREDENTIAL = 'invalid-credential';
- CryptoSignerErrorCode.SERVER_ERROR = 'server-error';
|