Skip to content

Commit 5ff6922

Browse files
arcanisBYK
authored andcommitted
Fix: Fix various npm auth issues (#3774)
**Summary** Fixes #3765. There was some confusion around when and how to send the auth tokens to NPM and Yarn registries. This patch is a first attempt to get these fixed. **Test plan** See #3842.
1 parent 15f53dd commit 5ff6922

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

src/registries/npm-registry.js

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type RequestManager from '../util/request-manager.js';
55
import type {RegistryRequestOptions, CheckOutdatedReturn} from './base-registry.js';
66
import type Config from '../config.js';
77
import type {ConfigRegistries} from './index.js';
8+
import {YARN_REGISTRY} from '../constants.js';
89
import * as fs from '../util/fs.js';
910
import NpmResolver from '../resolvers/registries/npm-resolver.js';
1011
import envReplace from '../util/env-replace.js';
@@ -76,15 +77,19 @@ export default class NpmRegistry extends Registry {
7677
const registry = this.getRegistry(packageName || pathname);
7778
const requestUrl = url.resolve(registry, pathname);
7879
const alwaysAuth = this.getRegistryOrGlobalOption(registry, 'always-auth');
79-
const customHostSuffix = this.getRegistryOrGlobalOption(registry, 'custom-host-suffix');
8080

8181
const headers = Object.assign(
8282
{
8383
Accept: 'application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*',
8484
},
8585
opts.headers,
8686
);
87-
if (this.token || (alwaysAuth && isRequestToRegistry(requestUrl, registry, customHostSuffix))) {
87+
88+
const packageIdent = packageName || pathname;
89+
const isScoppedPackage = packageIdent.match(/^@|\/@/);
90+
91+
// this.token must be checked to account for publish requests on non-scopped packages
92+
if (this.token || alwaysAuth || isScoppedPackage) {
8893
const authorization = this.getAuth(packageName || pathname);
8994
if (authorization) {
9095
headers.authorization = authorization;
@@ -208,26 +213,34 @@ export default class NpmRegistry extends Registry {
208213
return this.token;
209214
}
210215

211-
const registry = this.getRegistry(packageName);
216+
const baseRegistry = this.getRegistry(packageName);
217+
const registries = [baseRegistry];
212218

213-
// Check for bearer token.
214-
const authToken = this.getRegistryOrGlobalOption(registry, '_authToken');
215-
if (authToken) {
216-
return `Bearer ${String(authToken)}`;
219+
// If sending a request to the Yarn registry, we must also send it the auth token for the npm registry
220+
if (baseRegistry === YARN_REGISTRY) {
221+
registries.push(DEFAULT_REGISTRY);
217222
}
218223

219-
// Check for basic auth token.
220-
const auth = this.getRegistryOrGlobalOption(registry, '_auth');
221-
if (auth) {
222-
return `Basic ${String(auth)}`;
223-
}
224+
for (const registry of registries) {
225+
// Check for bearer token.
226+
const authToken = this.getRegistryOrGlobalOption(registry, '_authToken');
227+
if (authToken) {
228+
return `Bearer ${String(authToken)}`;
229+
}
224230
225-
// Check for basic username/password auth.
226-
const username = this.getRegistryOrGlobalOption(registry, 'username');
227-
const password = this.getRegistryOrGlobalOption(registry, '_password');
228-
if (username && password) {
229-
const pw = new Buffer(String(password), 'base64').toString();
230-
return 'Basic ' + new Buffer(String(username) + ':' + pw).toString('base64');
231+
// Check for basic auth token.
232+
const auth = this.getRegistryOrGlobalOption(registry, '_auth');
233+
if (auth) {
234+
return `Basic ${String(auth)}`;
235+
}
236+
237+
// Check for basic username/password auth.
238+
const username = this.getRegistryOrGlobalOption(registry, 'username');
239+
const password = this.getRegistryOrGlobalOption(registry, '_password');
240+
if (username && password) {
241+
const pw = new Buffer(String(password), 'base64').toString();
242+
return 'Basic ' + new Buffer(String(username) + ':' + pw).toString('base64');
243+
}
231244
}
232245

233246
return '';

0 commit comments

Comments
 (0)