Skip to content

Commit 9c53114

Browse files
Maël Nisonbestander
authored andcommitted
Fixes npm auth
1 parent 44fe0dd commit 9c53114

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

src/registries/npm-registry.js

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ export default class NpmRegistry extends Registry {
5858
const registry = this.getRegistry(packageName || pathname);
5959
const requestUrl = url.resolve(registry, pathname);
6060
const alwaysAuth = this.getRegistryOrGlobalOption(registry, 'always-auth');
61-
const customHostSuffix = this.getRegistryOrGlobalOption(registry, 'custom-host-suffix');
6261

6362
const headers = Object.assign(
6463
{
6564
Accept: 'application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*',
6665
},
6766
opts.headers,
6867
);
69-
if (this.token || (alwaysAuth && isRequestToRegistry(requestUrl, registry, customHostSuffix))) {
68+
69+
if (alwaysAuth || (packageName || pathname)[0] === `@`) {
7070
const authorization = this.getAuth(packageName || pathname);
7171
if (authorization) {
7272
headers.authorization = authorization;
@@ -180,26 +180,33 @@ export default class NpmRegistry extends Registry {
180180
return this.token;
181181
}
182182

183-
const registry = this.getRegistry(packageName);
183+
const baseRegistry = this.getRegistry(packageName);
184+
const registries = [baseRegistry];
184185

185-
// Check for bearer token.
186-
const authToken = this.getRegistryOrGlobalOption(registry, '_authToken');
187-
if (authToken) {
188-
return `Bearer ${String(authToken)}`;
186+
if (baseRegistry === `https://registry.yarnpkg.com/`) {
187+
registries.push(`https://registry.npmjs.org/`);
189188
}
190189

191-
// Check for basic auth token.
192-
const auth = this.getRegistryOrGlobalOption(registry, '_auth');
193-
if (auth) {
194-
return `Basic ${String(auth)}`;
195-
}
190+
for (const registry of registries) {
191+
// Check for bearer token.
192+
const authToken = this.getRegistryOrGlobalOption(registry, '_authToken');
193+
if (authToken) {
194+
return `Bearer ${String(authToken)}`;
195+
}
196196
197-
// Check for basic username/password auth.
198-
const username = this.getRegistryOrGlobalOption(registry, 'username');
199-
const password = this.getRegistryOrGlobalOption(registry, '_password');
200-
if (username && password) {
201-
const pw = new Buffer(String(password), 'base64').toString();
202-
return 'Basic ' + new Buffer(String(username) + ':' + pw).toString('base64');
197+
// Check for basic auth token.
198+
const auth = this.getRegistryOrGlobalOption(registry, '_auth');
199+
if (auth) {
200+
return `Basic ${String(auth)}`;
201+
}
202+
203+
// Check for basic username/password auth.
204+
const username = this.getRegistryOrGlobalOption(registry, 'username');
205+
const password = this.getRegistryOrGlobalOption(registry, '_password');
206+
if (username && password) {
207+
const pw = new Buffer(String(password), 'base64').toString();
208+
return 'Basic ' + new Buffer(String(username) + ':' + pw).toString('base64');
209+
}
203210
}
204211

205212
return '';

0 commit comments

Comments
 (0)