Debian Codegraph @

node_modules/@sigstore/sign/dist/identity/ci.js

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