Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/smanonr..../lib/node_mod.../npm/lib
File: token.js
'use strict'
[0] Fix | Delete
[1] Fix | Delete
const profile = require('libnpm/profile')
[2] Fix | Delete
const npm = require('./npm.js')
[3] Fix | Delete
const figgyPudding = require('figgy-pudding')
[4] Fix | Delete
const npmConfig = require('./config/figgy-config.js')
[5] Fix | Delete
const output = require('./utils/output.js')
[6] Fix | Delete
const otplease = require('./utils/otplease.js')
[7] Fix | Delete
const Table = require('cli-table3')
[8] Fix | Delete
const Bluebird = require('bluebird')
[9] Fix | Delete
const isCidrV4 = require('is-cidr').v4
[10] Fix | Delete
const isCidrV6 = require('is-cidr').v6
[11] Fix | Delete
const readUserInfo = require('./utils/read-user-info.js')
[12] Fix | Delete
const ansistyles = require('ansistyles')
[13] Fix | Delete
const log = require('npmlog')
[14] Fix | Delete
const pulseTillDone = require('./utils/pulse-till-done.js')
[15] Fix | Delete
[16] Fix | Delete
module.exports = token
[17] Fix | Delete
[18] Fix | Delete
token._validateCIDRList = validateCIDRList
[19] Fix | Delete
[20] Fix | Delete
token.usage =
[21] Fix | Delete
'npm token list\n' +
[22] Fix | Delete
'npm token revoke <tokenKey>\n' +
[23] Fix | Delete
'npm token create [--read-only] [--cidr=list]\n'
[24] Fix | Delete
[25] Fix | Delete
token.subcommands = ['list', 'revoke', 'create']
[26] Fix | Delete
[27] Fix | Delete
token.completion = function (opts, cb) {
[28] Fix | Delete
var argv = opts.conf.argv.remain
[29] Fix | Delete
[30] Fix | Delete
switch (argv[2]) {
[31] Fix | Delete
case 'list':
[32] Fix | Delete
case 'revoke':
[33] Fix | Delete
case 'create':
[34] Fix | Delete
return cb(null, [])
[35] Fix | Delete
default:
[36] Fix | Delete
return cb(new Error(argv[2] + ' not recognized'))
[37] Fix | Delete
}
[38] Fix | Delete
}
[39] Fix | Delete
[40] Fix | Delete
function withCb (prom, cb) {
[41] Fix | Delete
prom.then((value) => cb(null, value), cb)
[42] Fix | Delete
}
[43] Fix | Delete
[44] Fix | Delete
function token (args, cb) {
[45] Fix | Delete
log.gauge.show('token')
[46] Fix | Delete
if (args.length === 0) return withCb(list([]), cb)
[47] Fix | Delete
switch (args[0]) {
[48] Fix | Delete
case 'list':
[49] Fix | Delete
case 'ls':
[50] Fix | Delete
withCb(list(), cb)
[51] Fix | Delete
break
[52] Fix | Delete
case 'delete':
[53] Fix | Delete
case 'revoke':
[54] Fix | Delete
case 'remove':
[55] Fix | Delete
case 'rm':
[56] Fix | Delete
withCb(rm(args.slice(1)), cb)
[57] Fix | Delete
break
[58] Fix | Delete
case 'create':
[59] Fix | Delete
withCb(create(args.slice(1)), cb)
[60] Fix | Delete
break
[61] Fix | Delete
default:
[62] Fix | Delete
cb(new Error('Unknown profile command: ' + args[0]))
[63] Fix | Delete
}
[64] Fix | Delete
}
[65] Fix | Delete
[66] Fix | Delete
function generateTokenIds (tokens, minLength) {
[67] Fix | Delete
const byId = {}
[68] Fix | Delete
tokens.forEach((token) => {
[69] Fix | Delete
token.id = token.key
[70] Fix | Delete
for (let ii = minLength; ii < token.key.length; ++ii) {
[71] Fix | Delete
if (!tokens.some((ot) => ot !== token && ot.key.slice(0, ii) === token.key.slice(0, ii))) {
[72] Fix | Delete
token.id = token.key.slice(0, ii)
[73] Fix | Delete
break
[74] Fix | Delete
}
[75] Fix | Delete
}
[76] Fix | Delete
byId[token.id] = token
[77] Fix | Delete
})
[78] Fix | Delete
return byId
[79] Fix | Delete
}
[80] Fix | Delete
[81] Fix | Delete
const TokenConfig = figgyPudding({
[82] Fix | Delete
auth: {},
[83] Fix | Delete
registry: {},
[84] Fix | Delete
otp: {},
[85] Fix | Delete
cidr: {},
[86] Fix | Delete
'read-only': {},
[87] Fix | Delete
json: {},
[88] Fix | Delete
parseable: {}
[89] Fix | Delete
})
[90] Fix | Delete
[91] Fix | Delete
function config () {
[92] Fix | Delete
let conf = TokenConfig(npmConfig())
[93] Fix | Delete
const creds = npm.config.getCredentialsByURI(conf.registry)
[94] Fix | Delete
if (creds.token) {
[95] Fix | Delete
conf = conf.concat({
[96] Fix | Delete
auth: { token: creds.token }
[97] Fix | Delete
})
[98] Fix | Delete
} else if (creds.username) {
[99] Fix | Delete
conf = conf.concat({
[100] Fix | Delete
auth: {
[101] Fix | Delete
basic: {
[102] Fix | Delete
username: creds.username,
[103] Fix | Delete
password: creds.password
[104] Fix | Delete
}
[105] Fix | Delete
}
[106] Fix | Delete
})
[107] Fix | Delete
} else if (creds.auth) {
[108] Fix | Delete
const auth = Buffer.from(creds.auth, 'base64').toString().split(':', 2)
[109] Fix | Delete
conf = conf.concat({
[110] Fix | Delete
auth: {
[111] Fix | Delete
basic: {
[112] Fix | Delete
username: auth[0],
[113] Fix | Delete
password: auth[1]
[114] Fix | Delete
}
[115] Fix | Delete
}
[116] Fix | Delete
})
[117] Fix | Delete
} else {
[118] Fix | Delete
conf = conf.concat({ auth: {} })
[119] Fix | Delete
}
[120] Fix | Delete
if (conf.otp) conf.auth.otp = conf.otp
[121] Fix | Delete
return conf
[122] Fix | Delete
}
[123] Fix | Delete
[124] Fix | Delete
function list (args) {
[125] Fix | Delete
const conf = config()
[126] Fix | Delete
log.info('token', 'getting list')
[127] Fix | Delete
return pulseTillDone.withPromise(profile.listTokens(conf)).then((tokens) => {
[128] Fix | Delete
if (conf.json) {
[129] Fix | Delete
output(JSON.stringify(tokens, null, 2))
[130] Fix | Delete
return
[131] Fix | Delete
} else if (conf.parseable) {
[132] Fix | Delete
output(['key', 'token', 'created', 'readonly', 'CIDR whitelist'].join('\t'))
[133] Fix | Delete
tokens.forEach((token) => {
[134] Fix | Delete
output([
[135] Fix | Delete
token.key,
[136] Fix | Delete
token.token,
[137] Fix | Delete
token.created,
[138] Fix | Delete
token.readonly ? 'true' : 'false',
[139] Fix | Delete
token.cidr_whitelist ? token.cidr_whitelist.join(',') : ''
[140] Fix | Delete
].join('\t'))
[141] Fix | Delete
})
[142] Fix | Delete
return
[143] Fix | Delete
}
[144] Fix | Delete
generateTokenIds(tokens, 6)
[145] Fix | Delete
const idWidth = tokens.reduce((acc, token) => Math.max(acc, token.id.length), 0)
[146] Fix | Delete
const table = new Table({
[147] Fix | Delete
head: ['id', 'token', 'created', 'readonly', 'CIDR whitelist'],
[148] Fix | Delete
colWidths: [Math.max(idWidth, 2) + 2, 9, 12, 10]
[149] Fix | Delete
})
[150] Fix | Delete
tokens.forEach((token) => {
[151] Fix | Delete
table.push([
[152] Fix | Delete
token.id,
[153] Fix | Delete
token.token + '…',
[154] Fix | Delete
String(token.created).slice(0, 10),
[155] Fix | Delete
token.readonly ? 'yes' : 'no',
[156] Fix | Delete
token.cidr_whitelist ? token.cidr_whitelist.join(', ') : ''
[157] Fix | Delete
])
[158] Fix | Delete
})
[159] Fix | Delete
output(table.toString())
[160] Fix | Delete
})
[161] Fix | Delete
}
[162] Fix | Delete
[163] Fix | Delete
function rm (args) {
[164] Fix | Delete
if (args.length === 0) {
[165] Fix | Delete
throw new Error('npm token revoke <tokenKey>')
[166] Fix | Delete
}
[167] Fix | Delete
const conf = config()
[168] Fix | Delete
const toRemove = []
[169] Fix | Delete
const progress = log.newItem('removing tokens', toRemove.length)
[170] Fix | Delete
progress.info('token', 'getting existing list')
[171] Fix | Delete
return pulseTillDone.withPromise(profile.listTokens(conf).then((tokens) => {
[172] Fix | Delete
args.forEach((id) => {
[173] Fix | Delete
const matches = tokens.filter((token) => token.key.indexOf(id) === 0)
[174] Fix | Delete
if (matches.length === 1) {
[175] Fix | Delete
toRemove.push(matches[0].key)
[176] Fix | Delete
} else if (matches.length > 1) {
[177] Fix | Delete
throw new Error(`Token ID "${id}" was ambiguous, a new token may have been created since you last ran \`npm-profile token list\`.`)
[178] Fix | Delete
} else {
[179] Fix | Delete
const tokenMatches = tokens.filter((token) => id.indexOf(token.token) === 0)
[180] Fix | Delete
if (tokenMatches === 0) {
[181] Fix | Delete
throw new Error(`Unknown token id or value "${id}".`)
[182] Fix | Delete
}
[183] Fix | Delete
toRemove.push(id)
[184] Fix | Delete
}
[185] Fix | Delete
})
[186] Fix | Delete
return Bluebird.map(toRemove, (key) => {
[187] Fix | Delete
return otplease(conf, conf => {
[188] Fix | Delete
return profile.removeToken(key, conf)
[189] Fix | Delete
})
[190] Fix | Delete
})
[191] Fix | Delete
})).then(() => {
[192] Fix | Delete
if (conf.json) {
[193] Fix | Delete
output(JSON.stringify(toRemove))
[194] Fix | Delete
} else if (conf.parseable) {
[195] Fix | Delete
output(toRemove.join('\t'))
[196] Fix | Delete
} else {
[197] Fix | Delete
output('Removed ' + toRemove.length + ' token' + (toRemove.length !== 1 ? 's' : ''))
[198] Fix | Delete
}
[199] Fix | Delete
})
[200] Fix | Delete
}
[201] Fix | Delete
[202] Fix | Delete
function create (args) {
[203] Fix | Delete
const conf = config()
[204] Fix | Delete
const cidr = conf.cidr
[205] Fix | Delete
const readonly = conf['read-only']
[206] Fix | Delete
[207] Fix | Delete
const validCIDR = validateCIDRList(cidr)
[208] Fix | Delete
return readUserInfo.password().then((password) => {
[209] Fix | Delete
log.info('token', 'creating')
[210] Fix | Delete
return pulseTillDone.withPromise(otplease(conf, conf => {
[211] Fix | Delete
return profile.createToken(password, readonly, validCIDR, conf)
[212] Fix | Delete
}))
[213] Fix | Delete
}).then((result) => {
[214] Fix | Delete
delete result.key
[215] Fix | Delete
delete result.updated
[216] Fix | Delete
if (conf.json) {
[217] Fix | Delete
output(JSON.stringify(result))
[218] Fix | Delete
} else if (conf.parseable) {
[219] Fix | Delete
Object.keys(result).forEach((k) => output(k + '\t' + result[k]))
[220] Fix | Delete
} else {
[221] Fix | Delete
const table = new Table()
[222] Fix | Delete
Object.keys(result).forEach((k) => table.push({[ansistyles.bright(k)]: String(result[k])}))
[223] Fix | Delete
output(table.toString())
[224] Fix | Delete
}
[225] Fix | Delete
})
[226] Fix | Delete
}
[227] Fix | Delete
[228] Fix | Delete
function validateCIDR (cidr) {
[229] Fix | Delete
if (isCidrV6(cidr)) {
[230] Fix | Delete
throw new Error('CIDR whitelist can only contain IPv4 addresses, ' + cidr + ' is IPv6')
[231] Fix | Delete
}
[232] Fix | Delete
if (!isCidrV4(cidr)) {
[233] Fix | Delete
throw new Error('CIDR whitelist contains invalid CIDR entry: ' + cidr)
[234] Fix | Delete
}
[235] Fix | Delete
}
[236] Fix | Delete
[237] Fix | Delete
function validateCIDRList (cidrs) {
[238] Fix | Delete
const maybeList = cidrs ? (Array.isArray(cidrs) ? cidrs : [cidrs]) : []
[239] Fix | Delete
const list = maybeList.length === 1 ? maybeList[0].split(/,\s*/) : maybeList
[240] Fix | Delete
list.forEach(validateCIDR)
[241] Fix | Delete
return list
[242] Fix | Delete
}
[243] Fix | Delete
[244] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function