-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Fixes npm auth #3774
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
Fixes npm auth #3774
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ import type RequestManager from '../util/request-manager.js'; | |
| import type {RegistryRequestOptions, CheckOutdatedReturn} from './base-registry.js'; | ||
| import type Config from '../config.js'; | ||
| import type {ConfigRegistries} from './index.js'; | ||
| import {YARN_REGISTRY} from '../constants.js'; | ||
| import * as fs from '../util/fs.js'; | ||
| import NpmResolver from '../resolvers/registries/npm-resolver.js'; | ||
| import envReplace from '../util/env-replace.js'; | ||
|
|
@@ -76,15 +77,19 @@ export default class NpmRegistry extends Registry { | |
| const registry = this.getRegistry(packageName || pathname); | ||
| const requestUrl = url.resolve(registry, pathname); | ||
| const alwaysAuth = this.getRegistryOrGlobalOption(registry, 'always-auth'); | ||
| const customHostSuffix = this.getRegistryOrGlobalOption(registry, 'custom-host-suffix'); | ||
|
|
||
| const headers = Object.assign( | ||
| { | ||
| Accept: 'application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*', | ||
| }, | ||
| opts.headers, | ||
| ); | ||
| if (this.token || (alwaysAuth && isRequestToRegistry(requestUrl, registry, customHostSuffix))) { | ||
|
|
||
| const packageIdent = packageName || pathname; | ||
| const isScoppedPackage = packageIdent.match(/^@|\/@/); | ||
|
|
||
| // this.token must be checked to account for publish requests on non-scopped packages | ||
| if (this.token || alwaysAuth || isScoppedPackage) { | ||
| const authorization = this.getAuth(packageName || pathname); | ||
| if (authorization) { | ||
| headers.authorization = authorization; | ||
|
|
@@ -208,26 +213,34 @@ export default class NpmRegistry extends Registry { | |
| return this.token; | ||
| } | ||
|
|
||
| const registry = this.getRegistry(packageName); | ||
| const baseRegistry = this.getRegistry(packageName); | ||
| const registries = [baseRegistry]; | ||
|
|
||
| // Check for bearer token. | ||
| const authToken = this.getRegistryOrGlobalOption(registry, '_authToken'); | ||
| if (authToken) { | ||
| return `Bearer ${String(authToken)}`; | ||
| // If sending a request to the Yarn registry, we must also send it the auth token for the npm registry | ||
| if (baseRegistry === YARN_REGISTRY) { | ||
| registries.push(DEFAULT_REGISTRY); | ||
| } | ||
|
|
||
| // Check for basic auth token. | ||
| const auth = this.getRegistryOrGlobalOption(registry, '_auth'); | ||
| if (auth) { | ||
| return `Basic ${String(auth)}`; | ||
| } | ||
| for (const registry of registries) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this trying to get an auth token for the first matching registry? If so, I find that a bit dangerous. Should we keep a mapping of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hence the check to make sure that the npm fallback is only added when the registry is the Yarn registry. The token won't be sent for any other hostname (wich is another issue). That being said, the whole "multi-registry" logic is flawed, since we only support a single registry implementation (and adding more of them wouldn't make much sense, since it would complexify the codebase for little gain). I'd like to rework it so that we only support a single registry implementation, the npm one, and then make possible to configure what needs to be generic (mostly the hostname). But that's a second step. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm +1 to this until we do the refactor. The code can use a short comment explaining this safe-guard. |
||
| // Check for bearer token. | ||
| const authToken = this.getRegistryOrGlobalOption(registry, '_authToken'); | ||
| if (authToken) { | ||
| return `Bearer ${String(authToken)}`; | ||
| } | ||
|
|
||
| // Check for basic username/password auth. | ||
| const username = this.getRegistryOrGlobalOption(registry, 'username'); | ||
| const password = this.getRegistryOrGlobalOption(registry, '_password'); | ||
| if (username && password) { | ||
| const pw = new Buffer(String(password), 'base64').toString(); | ||
| return 'Basic ' + new Buffer(String(username) + ':' + pw).toString('base64'); | ||
| // Check for basic auth token. | ||
| const auth = this.getRegistryOrGlobalOption(registry, '_auth'); | ||
| if (auth) { | ||
| return `Basic ${String(auth)}`; | ||
| } | ||
|
|
||
| // Check for basic username/password auth. | ||
| const username = this.getRegistryOrGlobalOption(registry, 'username'); | ||
| const password = this.getRegistryOrGlobalOption(registry, '_password'); | ||
| if (username && password) { | ||
| const pw = new Buffer(String(password), 'base64').toString(); | ||
| return 'Basic ' + new Buffer(String(username) + ':' + pw).toString('base64'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wow, no idea we did double base64 for the password part. |
||
| } | ||
| } | ||
|
|
||
| return ''; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does the second part cover? (sorry, not very familiar with scoped packages except for the
@user/packagepattern.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In some cases
packageIdentwill be@user/package, and in other cases it will behttps://example.org/@user/package.tgz