@@ -30,6 +30,7 @@ async function oidc ({ packageName, registry, opts, config }) {
30
30
/** @see https://github.com/watson/ci-info/blob/v4.2.0/vendors.json#L161C13-L161C22 */
31
31
ciInfo . GITLAB
32
32
) ) {
33
+ log . silly ( 'oidc' , 'Not running OIDC, not in a supported CI environment' )
33
34
return undefined
34
35
}
35
36
@@ -67,14 +68,11 @@ async function oidc ({ packageName, registry, opts, config }) {
67
68
process . env . ACTIONS_ID_TOKEN_REQUEST_URL &&
68
69
process . env . ACTIONS_ID_TOKEN_REQUEST_TOKEN
69
70
) {
70
- log . silly ( 'oidc' , '"GITHUB_ACTIONS" detected with "ACTIONS_ID_" envs, fetching id_token' )
71
-
72
71
/**
73
72
* The specification for an audience is `npm:registry.npmjs.org`,
74
73
* where "registry.npmjs.org" can be any supported registry.
75
74
*/
76
75
const audience = `npm:${ new URL ( registry ) . hostname } `
77
- log . silly ( 'oidc' , `Using audience: ${ audience } ` )
78
76
const url = new URL ( process . env . ACTIONS_ID_TOKEN_REQUEST_URL )
79
77
url . searchParams . append ( 'audience' , audience )
80
78
const startTime = Date . now ( )
@@ -96,17 +94,19 @@ async function oidc ({ packageName, registry, opts, config }) {
96
94
const json = await response . json ( )
97
95
98
96
if ( ! response . ok ) {
99
- throw new Error ( `Failed to fetch id_token from GitHub: received an invalid response` )
97
+ log . verbose ( 'oidc' , `Failed to fetch id_token from GitHub: received an invalid response` )
98
+ return undefined
100
99
}
101
100
102
101
if ( ! json . value ) {
103
- throw new Error ( `Failed to fetch id_token from GitHub: missing value` )
102
+ log . verbose ( 'oidc' , `Failed to fetch id_token from GitHub: missing value` )
103
+ return undefined
104
104
}
105
105
106
- log . silly ( 'oidc' , 'GITHUB_ACTIONS valid fetch response for id_token' )
107
106
idToken = json . value
108
107
} 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
110
110
}
111
111
}
112
112
}
@@ -130,22 +130,31 @@ async function oidc ({ packageName, registry, opts, config }) {
130
130
}
131
131
132
132
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
+ }
146
154
147
155
if ( ! response ?. token ) {
148
- throw new Error ( 'OIDC token exchange failure: missing token in response body' )
156
+ log . verbose ( 'oidc' , 'OIDC token exchange failure: missing token in response body' )
157
+ return undefined
149
158
}
150
159
/*
151
160
* The "opts" object is a clone of npm.flatOptions and is passed through the `publish` command,
@@ -157,10 +166,8 @@ async function oidc ({ packageName, registry, opts, config }) {
157
166
config . set ( authTokenKey , response . token , 'user' )
158
167
log . silly ( 'oidc' , `OIDC token successfully retrieved` )
159
168
} catch ( error ) {
160
- log . verbose ( 'oidc' , error . message )
161
- if ( error ?. body ?. message ) {
162
- log . verbose ( 'oidc' , `Registry body response error message "${ error . body . message } "` )
163
- }
169
+ /* istanbul ignore next */
170
+ log . verbose ( 'oidc' , 'Failure checking OIDC config' , error )
164
171
}
165
172
return undefined
166
173
}
0 commit comments