Skip to content

Commit cfb3828

Browse files
bodograumannhaoqunjiang
authored andcommitted
fix: support basic auth for npm registry access (#6207)
When username and password are configured in the .npmrc for the respective scope, use basic auth when getting package metadata from the npm registry. Closes #6206
1 parent 5bc8c80 commit cfb3828

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

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

+19-5
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ class PackageManager {
199199
return this._registries[cacheKey]
200200
}
201201

202-
async getAuthToken (scope) {
202+
async getAuthConfig (scope) {
203203
// get npmrc (https://docs.npmjs.com/configuring-npm/npmrc.html#files)
204204
const possibleRcPaths = [
205205
path.resolve(this.context, '.npmrc'),
@@ -227,8 +227,18 @@ class PackageManager {
227227
.replace(/https?:/, '') // remove leading protocol
228228
.replace(/([^/])$/, '$1/') // ensure ending with slash
229229
const authTokenKey = `${registryWithoutProtocol}:_authToken`
230+
const authUsernameKey = `${registryWithoutProtocol}:username`
231+
const authPasswordKey = `${registryWithoutProtocol}:_password`
230232

231-
return npmConfig[authTokenKey]
233+
const auth = {}
234+
if (authTokenKey in npmConfig) {
235+
auth.token = npmConfig[authTokenKey]
236+
}
237+
if (authPasswordKey in npmConfig) {
238+
auth.username = npmConfig[authUsernameKey]
239+
auth.password = Buffer.from(npmConfig[authPasswordKey], 'base64').toString()
240+
}
241+
return auth
232242
}
233243

234244
async setRegistryEnvs () {
@@ -296,9 +306,13 @@ class PackageManager {
296306
headers.Accept = 'application/vnd.npm.install-v1+json;q=1.0, application/json;q=0.9, */*;q=0.8'
297307
}
298308

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

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

0 commit comments

Comments
 (0)