From c0e5944055c5fae326aa7d2fcfd5b135365ecaec Mon Sep 17 00:00:00 2001 From: Rebecca Turner Date: Thu, 12 Jul 2018 18:27:12 -0700 Subject: [PATCH] npm-registry-client@WIP --- .../npm-registry-client/lib/access.js | 14 ++++++++ .../npm-registry-client/lib/request.js | 35 +++++++++++++------ node_modules/npm-registry-client/package.json | 33 ++++++++--------- package-lock.json | 5 ++- package.json | 2 +- 5 files changed, 55 insertions(+), 34 deletions(-) diff --git a/node_modules/npm-registry-client/lib/access.js b/node_modules/npm-registry-client/lib/access.js index d8d65a9442a25..caa80b12191c1 100644 --- a/node_modules/npm-registry-client/lib/access.js +++ b/node_modules/npm-registry-client/lib/access.js @@ -16,6 +16,12 @@ subcommands.public = function (uri, params, cb) { subcommands.restricted = function (uri, params, cb) { return setAccess.call(this, 'restricted', uri, params, cb) } +subcommands['2fa-required'] = function (uri, params, cb) { + return setRequires2fa.call(this, true, uri, params, cb) +} +subcommands['2fa-not-required'] = function (uri, params, cb) { + return setRequires2fa.call(this, false, uri, params, cb) +} function setAccess (access, uri, params, cb) { return this.request(apiUri(uri, 'package', params.package, 'access'), { @@ -25,6 +31,14 @@ function setAccess (access, uri, params, cb) { }, cb) } +function setRequires2fa (requires2fa, uri, params, cb) { + return this.request(apiUri(uri, 'package', params.package, 'access'), { + method: 'POST', + auth: params.auth, + body: JSON.stringify({ publish_requires_tfa: requires2fa }) + }, cb) +} + subcommands.grant = function (uri, params, cb) { var reqUri = apiUri(uri, 'team', params.scope, params.team, 'package') return this.request(reqUri, { diff --git a/node_modules/npm-registry-client/lib/request.js b/node_modules/npm-registry-client/lib/request.js index 471546c1d29b8..5987bfa6fb0e4 100644 --- a/node_modules/npm-registry-client/lib/request.js +++ b/node_modules/npm-registry-client/lib/request.js @@ -289,13 +289,26 @@ function requestDone (method, where, cb) { } if (!parsed.error) { - er = makeError( - 'Registry returned ' + response.statusCode + - ' for ' + method + - ' on ' + where, - name, - response.statusCode - ) + if (response.statusCode === 401 && response.headers['www-authenticate']) { + const auth = response.headers['www-authenticate'].split(/,\s*/).map(s => s.toLowerCase()) + if (auth.indexOf('ipaddress') !== -1) { + er = makeError('Login is not allowed from your IP address', name, response.statusCode, 'EAUTHIP') + } else if (auth.indexOf('otp') !== -1) { + er = makeError('OTP required for this operation', name, response.statusCode, 'EOTP') + } else { + er = makeError('Unable to authenticate, need: ' + response.headers['www-authenticate'], name, response.statusCode, 'EAUTHUNKNOWN') + } + } else { + const msg = parsed.message ? ': ' + parsed.message : '' + er = makeError( + 'Registry returned ' + response.statusCode + + ' for ' + method + + ' on ' + where + + msg, + name, + response.statusCode + ) + } } else if (name && parsed.error === 'not_found') { er = makeError('404 Not Found: ' + name, name, response.statusCode) } else if (name && parsed.error === 'User not found') { @@ -312,12 +325,12 @@ function requestDone (method, where, cb) { }.bind(this) } -function makeError (message, name, code) { +function makeError (message, name, statusCode, code) { var er = new Error(message) if (name) er.pkgid = name - if (code) { - er.statusCode = code - er.code = 'E' + code + if (statusCode) { + er.statusCode = statusCode + er.code = code || 'E' + statusCode } return er } diff --git a/node_modules/npm-registry-client/package.json b/node_modules/npm-registry-client/package.json index 6058da582b930..de860a8dac4a8 100644 --- a/node_modules/npm-registry-client/package.json +++ b/node_modules/npm-registry-client/package.json @@ -1,33 +1,26 @@ { - "_args": [ - [ - "npm-registry-client@8.5.1", - "/Users/rebecca/code/npm" - ] - ], - "_from": "npm-registry-client@8.5.1", + "_from": "github:npm/npm-registry-client#per-package-2fa", "_id": "npm-registry-client@8.5.1", "_inBundle": false, - "_integrity": "sha512-7rjGF2eA7hKDidGyEWmHTiKfXkbrcQAsGL/Rh4Rt3x3YNRNHhwaTzVJfW3aNvvlhg4G62VCluif0sLCb/i51Hg==", + "_integrity": "", "_location": "/npm-registry-client", "_phantomChildren": { "safe-buffer": "5.1.2" }, "_requested": { - "type": "version", - "registry": true, - "raw": "npm-registry-client@8.5.1", - "name": "npm-registry-client", - "escapedName": "npm-registry-client", - "rawSpec": "8.5.1", - "saveSpec": null, - "fetchSpec": "8.5.1" + "type": "git", + "raw": "github:npm/npm-registry-client#per-package-2fa", + "rawSpec": "github:npm/npm-registry-client#per-package-2fa", + "saveSpec": "github:npm/npm-registry-client#per-package-2fa", + "fetchSpec": null, + "gitCommittish": "per-package-2fa" }, "_requiredBy": [ + "#USER", "/" ], - "_resolved": "https://registry.npmjs.org/npm-registry-client/-/npm-registry-client-8.5.1.tgz", - "_spec": "8.5.1", + "_resolved": "github:npm/npm-registry-client#8b472d221e53bb6c43f436eb36d30f3afb70fc1a", + "_spec": "github:npm/npm-registry-client#per-package-2fa", "_where": "/Users/rebecca/code/npm", "author": { "name": "Isaac Z. Schlueter", @@ -37,6 +30,7 @@ "bugs": { "url": "https://github.com/npm/npm-registry-client/issues" }, + "bundleDependencies": false, "dependencies": { "concat-stream": "^1.5.2", "graceful-fs": "^4.1.6", @@ -51,6 +45,7 @@ "slide": "^1.1.3", "ssri": "^5.2.4" }, + "deprecated": false, "description": "Client for the npm registry", "devDependencies": { "negotiator": "^0.6.1", @@ -60,7 +55,7 @@ "rimraf": "^2.5.4", "standard": "^11.0.0", "standard-version": "^4.3.0", - "tap": "^11.1.2" + "tap": "^12.0.1" }, "files": [ "lib", diff --git a/package-lock.json b/package-lock.json index 2e9b08188d273..9739351ffdd26 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3189,9 +3189,8 @@ } }, "npm-registry-client": { - "version": "8.5.1", - "resolved": "https://registry.npmjs.org/npm-registry-client/-/npm-registry-client-8.5.1.tgz", - "integrity": "sha512-7rjGF2eA7hKDidGyEWmHTiKfXkbrcQAsGL/Rh4Rt3x3YNRNHhwaTzVJfW3aNvvlhg4G62VCluif0sLCb/i51Hg==", + "version": "github:npm/npm-registry-client#8b472d221e53bb6c43f436eb36d30f3afb70fc1a", + "from": "github:npm/npm-registry-client#per-package-2fa", "requires": { "concat-stream": "^1.5.2", "graceful-fs": "^4.1.6", diff --git a/package.json b/package.json index c985242aad832..8004d5c550be1 100644 --- a/package.json +++ b/package.json @@ -97,7 +97,7 @@ "npm-packlist": "~1.1.10", "npm-pick-manifest": "^2.1.0", "npm-profile": "^3.0.2", - "npm-registry-client": "^8.5.1", + "npm-registry-client": "github:npm/npm-registry-client#per-package-2fa", "npm-registry-fetch": "^1.1.0", "npm-user-validate": "~1.0.0", "npmlog": "~4.1.2",