From 1f0a89da9f45ddf977319ba1bc29895ad42f8d1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Fri, 30 Jun 2017 11:57:28 +0100 Subject: [PATCH 1/4] Fixes npm auth --- src/registries/npm-registry.js | 43 ++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/src/registries/npm-registry.js b/src/registries/npm-registry.js index cdaaa3372d..9efbd1acf7 100644 --- a/src/registries/npm-registry.js +++ b/src/registries/npm-registry.js @@ -76,7 +76,6 @@ 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( { @@ -84,7 +83,8 @@ export default class NpmRegistry extends Registry { }, 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; @@ -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 ''; From a241fb7099661b87798a622b44f806208b4f8ad1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Sat, 1 Jul 2017 00:00:20 +0100 Subject: [PATCH 2/4] Fixes yarn install with private packages --- src/registries/npm-registry.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/registries/npm-registry.js b/src/registries/npm-registry.js index 9efbd1acf7..d5c7045e33 100644 --- a/src/registries/npm-registry.js +++ b/src/registries/npm-registry.js @@ -84,7 +84,10 @@ export default class NpmRegistry extends Registry { opts.headers, ); - if (alwaysAuth || (packageName || pathname)[0] === `@`) { + const packageIdent = packageName || pathname; + const isScoppedPackage = packageIdent.match(/^@|\/@/); + + if (alwaysAuth || isScoppedPackage) { const authorization = this.getAuth(packageName || pathname); if (authorization) { headers.authorization = authorization; From d4dcfa7384fab9fb85a674197da0411b2fd3cc66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Thu, 6 Jul 2017 11:19:21 +0100 Subject: [PATCH 3/4] Fixes #3831 --- src/registries/npm-registry.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/registries/npm-registry.js b/src/registries/npm-registry.js index d5c7045e33..4f3d17c019 100644 --- a/src/registries/npm-registry.js +++ b/src/registries/npm-registry.js @@ -87,7 +87,8 @@ export default class NpmRegistry extends Registry { const packageIdent = packageName || pathname; const isScoppedPackage = packageIdent.match(/^@|\/@/); - if (alwaysAuth || isScoppedPackage) { + // 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; From 52e3c8b4c22e2be72c28c5337dd6e11863bb4612 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Thu, 6 Jul 2017 11:22:40 +0100 Subject: [PATCH 4/4] Uses constants --- src/registries/npm-registry.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/registries/npm-registry.js b/src/registries/npm-registry.js index 4f3d17c019..02d0added4 100644 --- a/src/registries/npm-registry.js +++ b/src/registries/npm-registry.js @@ -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'; @@ -215,8 +216,9 @@ export default class NpmRegistry extends Registry { const baseRegistry = this.getRegistry(packageName); const registries = [baseRegistry]; - if (baseRegistry === `https://registry.yarnpkg.com/`) { - registries.push(`https://registry.npmjs.org/`); + // 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); } for (const registry of registries) {