Skip to content

Commit

Permalink
Fixes npm auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Maël Nison committed Jun 30, 2017
1 parent f536494 commit 1f0a89d
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions src/registries/npm-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ 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))) {

if (alwaysAuth || (packageName || pathname)[0] === `@`) {
const authorization = this.getAuth(packageName || pathname);
if (authorization) {
headers.authorization = authorization;
Expand Down Expand Up @@ -208,26 +208,33 @@ 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 (baseRegistry === `https://registry.yarnpkg.com/`) {
registries.push(`https://registry.npmjs.org/`);
}

// Check for basic auth token.
const auth = this.getRegistryOrGlobalOption(registry, '_auth');
if (auth) {
return `Basic ${String(auth)}`;
}
for (const registry of registries) {
// 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');
}
}

return '';
Expand Down

0 comments on commit 1f0a89d

Please sign in to comment.