Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support basic auth for npm registry access #6207

Merged
merged 1 commit into from
Jan 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions packages/@vue/cli/lib/util/ProjectPackageManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class PackageManager {
return this._registries[cacheKey]
}

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

return npmConfig[authTokenKey]
const auth = {}
if (authTokenKey in npmConfig) {
auth.token = npmConfig[authTokenKey]
}
if (authPasswordKey in npmConfig) {
auth.username = npmConfig[authUsernameKey]
auth.password = Buffer.from(npmConfig[authPasswordKey], 'base64').toString()
}
return auth
}

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

const authToken = await this.getAuthToken(scope)
if (authToken) {
headers.Authorization = `Bearer ${authToken}`
const authConfig = await this.getAuthConfig(scope)
if ('password' in authConfig) {
const credentials = Buffer.from(`${authConfig.username}:${authConfig.password}`).toString('base64')
headers.Authorization = `Basic ${credentials}`
}
if ('token' in authConfig) {
headers.Authorization = `Bearer ${authConfig.token}`
}

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