Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions lib/commands/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,17 @@ class Token extends BaseCommand {
const parseable = this.npm.config.get('parseable')
log.info('token', 'getting list')
const tokens = await paginate('/-/npm/v1/tokens', this.npm.flatOptions)
this.generateTokenIds(tokens, 6)
if (json) {
output.buffer(tokens)
return
}
if (parseable) {
output.standard(['key', 'token', 'created', 'readonly', 'CIDR whitelist'].join('\t'))
output.standard(['name', 'key', 'token', 'created', 'readonly', 'CIDR whitelist'].join('\t'))
tokens.forEach(token => {
output.standard(
[
token.name,
token.key,
token.token,
token.created,
Expand All @@ -92,11 +94,11 @@ class Token extends BaseCommand {
})
return
}
this.generateTokenIds(tokens, 6)

const chalk = this.npm.chalk
for (const token of tokens) {
const created = String(token.created).slice(0, 10)
output.standard(`${chalk.blue('Token')} ${token.token}… with id ${chalk.cyan(token.id)} created ${created}`)
output.standard(`${chalk.blue('Token')} ${token.token}… with name ${chalk.magenta(token.name)} key ${chalk.cyan(token.key)} created ${created}`)
if (token.cidr_whitelist) {
output.standard(`with IP whitelist: ${chalk.green(token.cidr_whitelist.join(','))}`)
}
Expand Down Expand Up @@ -246,13 +248,13 @@ class Token extends BaseCommand {

generateTokenIds (tokens, minLength) {
for (const token of tokens) {
token.id = token.key
// token.id = token.key
for (let ii = minLength; ii < token.key.length; ++ii) {
const match = tokens.some(
ot => ot !== token && ot.key.slice(0, ii) === token.key.slice(0, ii)
)
if (!match) {
token.id = token.key.slice(0, ii)
token.key = token.key.slice(0, ii)
break
}
}
Expand Down
16 changes: 10 additions & 6 deletions test/lib/commands/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const tokens = [
{
key: 'abcd1234abcd1234',
token: 'efgh5678efgh5678',
name: 'abcd001',
cidr_whitelist: null,
readonly: false,
created: now,
Expand All @@ -21,6 +22,7 @@ const tokens = [
{
key: 'abcd1256',
token: 'hgfe8765',
name: 'abcd002',
cidr_whitelist: ['192.168.1.1/32'],
readonly: true,
created: now,
Expand Down Expand Up @@ -63,9 +65,9 @@ t.test('token list', async t => {
registry.getTokens(tokens)
await npm.exec('token', [])
t.strictSame(outputs, [
`Token efgh5678efgh5678… with id abcd123 created ${now.slice(0, 10)}`,
`Token efgh5678efgh5678… with name abcd001 key abcd123 created ${now.slice(0, 10)}`,
'',
`Token hgfe8765… with id abcd125 created ${now.slice(0, 10)}`,
`Token hgfe8765… with name abcd002 key abcd125 created ${now.slice(0, 10)}`,
'with IP whitelist: 192.168.1.1/32',
'',
])
Expand All @@ -86,7 +88,9 @@ t.test('token list json output', async t => {
registry.getTokens(tokens)
await npm.exec('token', ['list'])
const parsed = JSON.parse(joinedOutput())
t.match(parsed, tokens, 'prints the json parsed tokens')
t.match(parsed, tokens.map(token => {
return { ...token, key: token.key.slice(0, 6) }
}), 'prints the json parsed tokens')
})

t.test('token list parseable output', async t => {
Expand All @@ -104,9 +108,9 @@ t.test('token list parseable output', async t => {
registry.getTokens(tokens)
await npm.exec('token', [])
t.strictSame(outputs, [
'key\ttoken\tcreated\treadonly\tCIDR whitelist',
`abcd1234abcd1234\tefgh5678efgh5678\t${now}\tfalse\t`,
`abcd1256\thgfe8765\t${now}\ttrue\t192.168.1.1/32`,
'name\tkey\ttoken\tcreated\treadonly\tCIDR whitelist',
`abcd001\tabcd123\tefgh5678efgh5678\t${now}\tfalse\t`,
`abcd002\tabcd125\thgfe8765\t${now}\ttrue\t192.168.1.1/32`,
])
})

Expand Down