1"use strict"; 2var __importDefault = (this && this.__importDefault) || function (mod) { 3 return (mod && mod.__esModule) ? mod : { "default": mod }; 4}; 5Object.defineProperty(exports, "__esModule", { value: true }); 6exports.CIContextProvider = void 0; 7/* 8Copyright 2022 The Sigstore Authors. 9 10Licensed under the Apache License, Version 2.0 (the "License"); 11you may not use this file except in compliance with the License. 12You may obtain a copy of the License at 13 14 http://www.apache.org/licenses/LICENSE-2.0 15 16Unless required by applicable law or agreed to in writing, software 17distributed under the License is distributed on an "AS IS" BASIS, 18WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19See the License for the specific language governing permissions and 20limitations under the License. 21*/ 22const make_fetch_happen_1 = __importDefault(require("make-fetch-happen")); 23const util_1 = require("../util"); 24// Collection of all the CI-specific providers we have implemented 25const providers = [getGHAToken, getEnv]; 26/** 27 * CIContextProvider is a composite identity provider which will iterate 28 * over all of the CI-specific providers and return the token from the first 29 * one that resolves. 30 */ 31class CIContextProvider { 32 constructor(audience) { 33 this.audience = audience; 34 } 35 // Invoke all registered ProviderFuncs and return the value of whichever one 36 // resolves first. 37 async getToken() { 38 return util_1.promise 39 .promiseAny(providers.map((getToken) => getToken(this.audience))) 40 .catch(() => Promise.reject('CI: no tokens available')); 41 } 42} 43exports.CIContextProvider = CIContextProvider; 44/** 45 * getGHAToken can retrieve an OIDC token when running in a GitHub Actions 46 * workflow 47 */ 48async function getGHAToken(audience) { 49 // Check to see if we're running in GitHub Actions 50 if (!process.env.ACTIONS_ID_TOKEN_REQUEST_URL || 51 !process.env.ACTIONS_ID_TOKEN_REQUEST_TOKEN) { 52 return Promise.reject('no token available'); 53 } 54 // Construct URL to request token w/ appropriate audience 55 const url = new URL(process.env.ACTIONS_ID_TOKEN_REQUEST_URL); 56 url.searchParams.append('audience', audience); 57 const response = await (0, make_fetch_happen_1.default)(url.href, { 58 retry: 2, 59 headers: { 60 Accept: 'application/json', 61 Authorization: `Bearer ${process.env.ACTIONS_ID_TOKEN_REQUEST_TOKEN}`, 62 }, 63 }); 64 return response.json().then((data) => data.value); 65} 66/** 67 * getEnv can retrieve an OIDC token from an environment variable. 68 * This matches the behavior of https://github.com/sigstore/cosign/tree/main/pkg/providers/envvar 69 */ 70async function getEnv() { 71 if (!process.env.SIGSTORE_ID_TOKEN) { 72 return Promise.reject('no token available'); 73 } 74 return process.env.SIGSTORE_ID_TOKEN; 75} 76