Skip to content

Commit a9fa722

Browse files
committed
fix: use basic auth for npm registry access
When username and password are configured in the .npmrc for the respective scope, use basic auth when getting package metadate from the npm registry.
1 parent af3e6c4 commit a9fa722

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

packages/@vue/cli/lib/util/ProjectPackageManager.js

+18-5
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ class PackageManager {
197197
return this._registries[cacheKey]
198198
}
199199

200-
async getAuthToken (scope) {
200+
async getAuthConfig (scope) {
201201
// get npmrc (https://docs.npmjs.com/configuring-npm/npmrc.html#files)
202202
const possibleRcPaths = [
203203
path.resolve(this.context, '.npmrc'),
@@ -225,8 +225,17 @@ class PackageManager {
225225
.replace(/https?:/, '') // remove leading protocol
226226
.replace(/([^/])$/, '$1/') // ensure ending with slash
227227
const authTokenKey = `${registryWithoutProtocol}:_authToken`
228+
const authUsernameKey = `${registryWithoutProtocol}:username`
229+
const authPasswordKey = `${registryWithoutProtocol}:_password`
228230

229-
return npmConfig[authTokenKey]
231+
const auth = {}
232+
if (authTokenKey in npmConfig) {
233+
auth.token = npmConfig[authTokenKey]
234+
}
235+
if (authPasswordKey in npmConfig) {
236+
auth.username = npmConfig[authUsernameKey]
237+
auth.password = Buffer.from(npmConfig[authPasswordKey], 'base64').toString()
238+
}
230239
}
231240

232241
async setRegistryEnvs () {
@@ -296,9 +305,13 @@ class PackageManager {
296305
headers.Accept = 'application/vnd.npm.install-v1+json;q=1.0, application/json;q=0.9, */*;q=0.8'
297306
}
298307

299-
const authToken = await this.getAuthToken(scope)
300-
if (authToken) {
301-
headers.Authorization = `Bearer ${authToken}`
308+
const authConfig = await this.getAuthToken(scope)
309+
if ('password' in authConfig) {
310+
const credentials = Buffer.from(`${authConfig.username}:${authConfig.password}`).toString('base64')
311+
headers.Authorization = `Basic ${credentials}`
312+
}
313+
if ('token' in authConfig) {
314+
headers.Authorization = `Bearer ${authConfig.token}`
302315
}
303316

304317
const url = `${registry.replace(/\/$/g, '')}/${packageName}`

0 commit comments

Comments
 (0)