Skip to content

Commit b673257

Browse files
committed
feat(trusted-publishing): handle failure to retrieve id-token in the context of github actions
for #958
1 parent f063ab4 commit b673257

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/trusted-publishing/token-exchange.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@ const GITHUB_ACTIONS_PROVIDER_NAME = "GitHub Actions";
66
const GITLAB_PIPELINES_PROVIDER_NAME = "GitLab CI/CD";
77

88
async function exchangeGithubActionsToken(packageName) {
9-
const idToken = await getIDToken("npm:registry.npmjs.org");
9+
let idToken;
10+
11+
try {
12+
idToken = await getIDToken("npm:registry.npmjs.org");
13+
} catch (e) {
14+
return undefined;
15+
}
16+
1017
const response = await fetch(
1118
`${OFFICIAL_REGISTRY}-/npm/v1/oidc/token/exchange/package/${encodeURIComponent(packageName)}`,
1219
{

test/trusted-publishing/token-exchange.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ test.serial("that an access token is returned when token exchange succeeds", asy
3636
t.is(await exchangeToken(pkg), token);
3737
});
3838

39+
test.serial("that `undefined` is returned when ID token retrieval fails", async (t) => {
40+
td.when(getIDToken("npm:registry.npmjs.org")).thenThrow(new Error("Unable to get ACTIONS_ID_TOKEN_REQUEST_URL env variable"));
41+
42+
t.is(await exchangeToken(pkg), undefined);
43+
});
44+
3945
test.serial("that `undefined` is returned when token exchange fails", async (t) => {
4046
td.when(getIDToken("npm:registry.npmjs.org")).thenResolve(idToken);
4147
td.when(

0 commit comments

Comments
 (0)