github.com/jfrog/frogbot@v1.1.1-0.20231221090046-821a26f50338/action/node_modules/@actions/core/lib/oidc-utils.js (about)

     1  "use strict";
     2  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
     3      function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
     4      return new (P || (P = Promise))(function (resolve, reject) {
     5          function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
     6          function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
     7          function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
     8          step((generator = generator.apply(thisArg, _arguments || [])).next());
     9      });
    10  };
    11  Object.defineProperty(exports, "__esModule", { value: true });
    12  exports.OidcClient = void 0;
    13  const http_client_1 = require("@actions/http-client");
    14  const auth_1 = require("@actions/http-client/lib/auth");
    15  const core_1 = require("./core");
    16  class OidcClient {
    17      static createHttpClient(allowRetry = true, maxRetry = 10) {
    18          const requestOptions = {
    19              allowRetries: allowRetry,
    20              maxRetries: maxRetry
    21          };
    22          return new http_client_1.HttpClient('actions/oidc-client', [new auth_1.BearerCredentialHandler(OidcClient.getRequestToken())], requestOptions);
    23      }
    24      static getRequestToken() {
    25          const token = process.env['ACTIONS_ID_TOKEN_REQUEST_TOKEN'];
    26          if (!token) {
    27              throw new Error('Unable to get ACTIONS_ID_TOKEN_REQUEST_TOKEN env variable');
    28          }
    29          return token;
    30      }
    31      static getIDTokenUrl() {
    32          const runtimeUrl = process.env['ACTIONS_ID_TOKEN_REQUEST_URL'];
    33          if (!runtimeUrl) {
    34              throw new Error('Unable to get ACTIONS_ID_TOKEN_REQUEST_URL env variable');
    35          }
    36          return runtimeUrl;
    37      }
    38      static getCall(id_token_url) {
    39          var _a;
    40          return __awaiter(this, void 0, void 0, function* () {
    41              const httpclient = OidcClient.createHttpClient();
    42              const res = yield httpclient
    43                  .getJson(id_token_url)
    44                  .catch(error => {
    45                  throw new Error(`Failed to get ID Token. \n 
    46          Error Code : ${error.statusCode}\n 
    47          Error Message: ${error.result.message}`);
    48              });
    49              const id_token = (_a = res.result) === null || _a === void 0 ? void 0 : _a.value;
    50              if (!id_token) {
    51                  throw new Error('Response json body do not have ID Token field');
    52              }
    53              return id_token;
    54          });
    55      }
    56      static getIDToken(audience) {
    57          return __awaiter(this, void 0, void 0, function* () {
    58              try {
    59                  // New ID Token is requested from action service
    60                  let id_token_url = OidcClient.getIDTokenUrl();
    61                  if (audience) {
    62                      const encodedAudience = encodeURIComponent(audience);
    63                      id_token_url = `${id_token_url}&audience=${encodedAudience}`;
    64                  }
    65                  core_1.debug(`ID token url is ${id_token_url}`);
    66                  const id_token = yield OidcClient.getCall(id_token_url);
    67                  core_1.setSecret(id_token);
    68                  return id_token;
    69              }
    70              catch (error) {
    71                  throw new Error(`Error message: ${error.message}`);
    72              }
    73          });
    74      }
    75  }
    76  exports.OidcClient = OidcClient;
    77  //# sourceMappingURL=oidc-utils.js.map