Skip to content

Commit 3c263ef

Browse files
committed
fix: change to the oidc flow for more granualr control over log levels
1 parent 9021253 commit 3c263ef

1 file changed

Lines changed: 30 additions & 24 deletions

File tree

lib/utils/oidc.js

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ async function oidc ({ packageName, registry, opts, config }) {
3030
/** @see https://github.com/watson/ci-info/blob/v4.2.0/vendors.json#L161C13-L161C22 */
3131
ciInfo.GITLAB
3232
)) {
33+
log.silly('oidc', 'Not running OIDC, not in a supported CI environment')
3334
return undefined
3435
}
3536

@@ -67,14 +68,11 @@ async function oidc ({ packageName, registry, opts, config }) {
6768
process.env.ACTIONS_ID_TOKEN_REQUEST_URL &&
6869
process.env.ACTIONS_ID_TOKEN_REQUEST_TOKEN
6970
) {
70-
log.silly('oidc', '"GITHUB_ACTIONS" detected with "ACTIONS_ID_" envs, fetching id_token')
71-
7271
/**
7372
* The specification for an audience is `npm:registry.npmjs.org`,
7473
* where "registry.npmjs.org" can be any supported registry.
7574
*/
7675
const audience = `npm:${new URL(registry).hostname}`
77-
log.silly('oidc', `Using audience: ${audience}`)
7876
const url = new URL(process.env.ACTIONS_ID_TOKEN_REQUEST_URL)
7977
url.searchParams.append('audience', audience)
8078
const startTime = Date.now()
@@ -96,17 +94,19 @@ async function oidc ({ packageName, registry, opts, config }) {
9694
const json = await response.json()
9795

9896
if (!response.ok) {
99-
throw new Error(`Failed to fetch id_token from GitHub: received an invalid response`)
97+
log.silly('oidc', `Failed to fetch id_token from GitHub: received an invalid response`)
98+
return undefined
10099
}
101100

102101
if (!json.value) {
103-
throw new Error(`Failed to fetch id_token from GitHub: missing value`)
102+
log.silly('oidc', `Failed to fetch id_token from GitHub: missing value`)
103+
return undefined
104104
}
105105

106-
log.silly('oidc', 'GITHUB_ACTIONS valid fetch response for id_token')
107106
idToken = json.value
108107
} else {
109-
throw new Error('GITHUB_ACTIONS detected. If you intend to publish using OIDC, please set workflow permissions for `id-token: write`')
108+
log.silly('oidc', 'GITHUB_ACTIONS detected. If you intend to publish using OIDC, please set workflow permissions for `id-token: write`')
109+
return undefined
110110
}
111111
}
112112
}
@@ -130,22 +130,31 @@ async function oidc ({ packageName, registry, opts, config }) {
130130
}
131131

132132
const escapedPackageName = npa(packageName).escapedName
133-
const response = await npmFetch.json(new URL(`/-/npm/v1/oidc/token/exchange/package/${escapedPackageName}`, registry), {
134-
...{
135-
...opts,
136-
[authTokenKey]: idToken, // Use the idToken as the auth token for the request
137-
},
138-
method: 'POST',
139-
headers: {
140-
...opts.headers,
141-
'Content-Type': 'application/json',
142-
// this will not work because the existing auth token will replace it.
143-
// authorization: `Bearer ${idToken}`,
144-
},
145-
})
133+
let response
134+
try {
135+
response = await npmFetch.json(new URL(`/-/npm/v1/oidc/token/exchange/package/${escapedPackageName}`, registry), {
136+
...{
137+
...opts,
138+
[authTokenKey]: idToken, // Use the idToken as the auth token for the request
139+
},
140+
method: 'POST',
141+
headers: {
142+
...opts.headers,
143+
'Content-Type': 'application/json',
144+
// this will not work because the existing auth token will replace it.
145+
// authorization: `Bearer ${idToken}`,
146+
},
147+
})
148+
} catch (error) {
149+
if (error?.body?.message) {
150+
log.verbose('oidc', `Registry body response error message "${error.body.message}"`)
151+
}
152+
return undefined
153+
}
146154

147155
if (!response?.token) {
148-
throw new Error('OIDC token exchange failure: missing token in response body')
156+
log.silly('oidc', 'OIDC token exchange failure: missing token in response body')
157+
return undefined
149158
}
150159
/*
151160
* The "opts" object is a clone of npm.flatOptions and is passed through the `publish` command,
@@ -158,9 +167,6 @@ async function oidc ({ packageName, registry, opts, config }) {
158167
log.silly('oidc', `OIDC token successfully retrieved`)
159168
} catch (error) {
160169
log.verbose('oidc', error.message)
161-
if (error?.body?.message) {
162-
log.verbose('oidc', `Registry body response error message "${error.body.message}"`)
163-
}
164170
}
165171
return undefined
166172
}

0 commit comments

Comments
 (0)