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 auth token when retrieving package metadata #5586

Merged
merged 3 commits into from Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 38 additions & 0 deletions packages/@vue/cli/lib/util/ProjectPackageManager.js
@@ -1,6 +1,7 @@
const fs = require('fs-extra')
const path = require('path')

const ini = require('ini')
const minimist = require('minimist')
const LRU = require('lru-cache')

Expand Down Expand Up @@ -154,6 +155,38 @@ class PackageManager {
return this._registry
}

async getAuthToken () {
// get npmrc (https://docs.npmjs.com/configuring-npm/npmrc.html#files)
const possibleRcPaths = [
path.resolve(this.context, '.npmrc'),
path.resolve(require('os').homedir(), '.npmrc')
]
if (process.env.PREFIX) {
possibleRcPaths.push(path.resolve(process.env.PREFIX, '/etc/npmrc'))
}
// there's also a '/path/to/npm/npmrc', skipped for simplicity of implementation

let npmConfig = {}
for (const loc of possibleRcPaths) {
if (fs.existsSync(loc)) {
try {
// the closer config file (the one with lower index) takes higher precedence
npmConfig = Object.assign({}, ini.parse(fs.readFileSync(loc, 'utf-8')), npmConfig)
} catch (e) {
// in case of file permission issues, etc.
}
}
}

const registry = await this.getRegistry()
const registryWithoutProtocol = registry
.replace(/https?:/, '') // remove leading protocol
.replace(/([^/])$/, '$1/') // ensure ending with slash
const authTokenKey = `${registryWithoutProtocol}:_authToken`

return npmConfig[authTokenKey]
}

async setRegistryEnvs () {
const registry = await this.getRegistry()

Expand Down Expand Up @@ -204,6 +237,7 @@ class PackageManager {
// https://github.com/npm/registry/blob/master/docs/responses/package-metadata.md
async getMetadata (packageName, { full = false } = {}) {
const registry = await this.getRegistry()
const authToken = await this.getAuthToken()

const metadataKey = `${this.bin}-${registry}-${packageName}`
let metadata = metadataCache.get(metadataKey)
Expand All @@ -217,6 +251,10 @@ class PackageManager {
headers.Accept = 'application/vnd.npm.install-v1+json'
}

if (authToken) {
headers.Authorization = `Bearer ${authToken}`
}

const url = `${registry.replace(/\/$/g, '')}/${packageName}`
try {
metadata = (await request.get(url, { headers })).body
Expand Down
1 change: 1 addition & 0 deletions packages/@vue/cli/package.json
Expand Up @@ -39,6 +39,7 @@
"fs-extra": "^7.0.1",
"globby": "^9.2.0",
"import-global": "^0.1.0",
"ini": "^1.3.5",
"inquirer": "^7.1.0",
"isbinaryfile": "^4.0.6",
"javascript-stringify": "^1.6.0",
Expand Down