Skip to content

Commit 301eb07

Browse files
author
Maël Nison
committed
Fixes npm auth
1 parent f536494 commit 301eb07

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

src/registries/npm-registry.js

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,15 @@ export default class NpmRegistry extends Registry {
7676
const registry = this.getRegistry(packageName || pathname);
7777
const requestUrl = url.resolve(registry, pathname);
7878
const alwaysAuth = this.getRegistryOrGlobalOption(registry, 'always-auth');
79-
const customHostSuffix = this.getRegistryOrGlobalOption(registry, 'custom-host-suffix');
8079

8180
const headers = Object.assign(
8281
{
8382
Accept: 'application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*',
8483
},
8584
opts.headers,
8685
);
87-
if (this.token || (alwaysAuth && isRequestToRegistry(requestUrl, registry, customHostSuffix))) {
86+
87+
if (alwaysAuth || (packageName || pathname)[0] === `@`) {
8888
const authorization = this.getAuth(packageName || pathname);
8989
if (authorization) {
9090
headers.authorization = authorization;
@@ -208,26 +208,35 @@ export default class NpmRegistry extends Registry {
208208
return this.token;
209209
}
210210

211-
const registry = this.getRegistry(packageName);
211+
const baseRegistry = this.getRegistry(packageName);
212+
const registries = [baseRegistry];
212213

213-
// Check for bearer token.
214-
const authToken = this.getRegistryOrGlobalOption(registry, '_authToken');
215-
if (authToken) {
216-
return `Bearer ${String(authToken)}`;
214+
if (baseRegistry === `https://registry.yarnpkg.com/`) {
215+
registries.push(`https://registry.npmjs.org/`);
217216
}
218217

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

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');
231240
}
232241

233242
return '';

0 commit comments

Comments
 (0)