@@ -2,6 +2,7 @@ const { log } = require('proc-log')
2
2
const npmFetch = require ( 'npm-registry-fetch' )
3
3
const ciInfo = require ( 'ci-info' )
4
4
const fetch = require ( 'make-fetch-happen' )
5
+ const npa = require ( 'npm-package-arg' )
5
6
6
7
/**
7
8
* Handles OpenID Connect (OIDC) token retrieval and exchange for CI environments.
@@ -103,21 +104,28 @@ async function oidc ({ packageName, registry, opts, config }) {
103
104
return undefined
104
105
}
105
106
106
- const response = await npmFetch . json ( new URL ( '/-/npm/v1/oidc/token/exchange' , registry ) , {
107
- ...opts ,
107
+ const parsedRegistry = new URL ( registry )
108
+ const regKey = `//${ parsedRegistry . host } ${ parsedRegistry . pathname } `
109
+ const authTokenKey = `${ regKey } :_authToken`
110
+
111
+ const escapedPackageName = npa ( packageName ) . escapedName
112
+ const response = await npmFetch . json ( new URL ( `/-/npm/v1/oidc/token/exchange/package/${ escapedPackageName } ` , registry ) , {
113
+ ...{
114
+ ...opts ,
115
+ [ authTokenKey ] : idToken , // Use the idToken as the auth token for the request
116
+ } ,
108
117
method : 'POST' ,
109
- body : {
110
- package_name : packageName ,
111
- id_token : idToken ,
118
+ headers : {
119
+ ...opts . headers ,
120
+ 'Content-Type' : 'application/json' ,
121
+ // this will not work because the existing auth token will replace it.
122
+ // authorization: `Bearer ${idToken}`,
112
123
} ,
113
124
} )
114
125
115
126
if ( ! response ?. token ) {
116
127
throw new Error ( 'OIDC token exchange failure: missing token in response body' )
117
128
}
118
- const parsedRegistry = new URL ( registry )
119
- const regKey = `//${ parsedRegistry . host } ${ parsedRegistry . pathname } `
120
- const authTokenKey = `${ regKey } :_authToken`
121
129
/*
122
130
* The "opts" object is a clone of npm.flatOptions and is passed through the `publish` command,
123
131
* eventually reaching `otplease`. To ensure the token is accessible during the publishing process,
0 commit comments