diff options
Diffstat (limited to 'deps/npm/lib/token.js')
-rw-r--r-- | deps/npm/lib/token.js | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/deps/npm/lib/token.js b/deps/npm/lib/token.js index 2a3b65e6ad..2918f6a876 100644 --- a/deps/npm/lib/token.js +++ b/deps/npm/lib/token.js @@ -4,8 +4,8 @@ const npm = require('./npm.js') const output = require('./utils/output.js') const Table = require('cli-table2') const Bluebird = require('bluebird') -const isCidrV4 = require('is-cidr').isCidrV4 -const isCidrV6 = require('is-cidr').isCidrV6 +const isCidrV4 = require('is-cidr').v4 +const isCidrV6 = require('is-cidr').v6 const readUserInfo = require('./utils/read-user-info.js') const ansistyles = require('ansistyles') const log = require('npmlog') @@ -13,6 +13,8 @@ const pulseTillDone = require('./utils/pulse-till-done.js') module.exports = token +token._validateCIDRList = validateCIDRList + token.usage = 'npm token list\n' + 'npm token revoke <tokenKey>\n' + @@ -81,7 +83,17 @@ function config () { registry: npm.config.get('registry'), otp: npm.config.get('otp') } - conf.auth = npm.config.getCredentialsByURI(conf.registry) + const creds = npm.config.getCredentialsByURI(conf.registry) + if (creds.token) { + conf.auth = {token: creds.token} + } else if (creds.username) { + conf.auth = {basic: {username: creds.username, password: creds.password}} + } else if (creds.auth) { + const auth = Buffer.from(creds.auth, 'base64').toString().split(':', 2) + conf.auth = {basic: {username: auth[0], password: auth[1]}} + } else { + conf.auth = {} + } if (conf.otp) conf.auth.otp = conf.otp return conf } @@ -149,8 +161,14 @@ function rm (args) { } }) return Bluebird.map(toRemove, (key) => { - progress.info('token', 'removing', key) - profile.removeToken(key, conf).then(() => profile.completeWork(1)) + return profile.removeToken(key, conf).catch((ex) => { + if (ex.code !== 'EOTP') throw ex + log.info('token', 'failed because revoking this token requires OTP') + return readUserInfo.otp('Authenticator provided OTP:').then((otp) => { + conf.auth.otp = otp + return profile.removeToken(key, conf) + }) + }) }) })).then(() => { if (conf.json) { @@ -205,7 +223,8 @@ function validateCIDR (cidr) { } function validateCIDRList (cidrs) { - const list = Array.isArray(cidrs) ? cidrs : cidrs ? cidrs.split(/,\s*/) : [] + const maybeList = cidrs ? (Array.isArray(cidrs) ? cidrs : [cidrs]) : [] + const list = maybeList.length === 1 ? maybeList[0].split(/,\s*/) : maybeList list.forEach(validateCIDR) return list } |