Skip to content

Commit 6da6555

Browse files
authored
fix: output all the required parameters for npm token list (#9588)
### What Added missing token metadata to `npm token list` outputs: * Added `id` field to the `--json` output. * Added `name` field to the default and `--parseable` outputs of `npm token list`. ### Why Currently, token information is fragmented across different output formats: * `npm token revoke` requires a token ID or token value, but the `--json` output does not expose the token ID. * The default and `--parseable` outputs expose the ID but not the token name. * This makes it difficult to automate token lifecycle management (such as identifying and revoking expired tokens) because users need information from multiple output formats. ### How * Updated the token listing implementation to include the token `id` in the JSON response. * Updated the default and parseable formatters to include the token `name`. * Ensured all output formats expose the metadata required for scripting, automation, and token revocation workflows while maintaining backward compatibility where possible. Fixes the issue: #9443
1 parent fdcfcee commit 6da6555

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

lib/commands/token.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,22 @@ class Token extends BaseCommand {
7373
const parseable = this.npm.config.get('parseable')
7474
log.info('token', 'getting list')
7575
const tokens = await paginate('/-/npm/v1/tokens', this.npm.flatOptions)
76+
77+
this.generateTokenIds(tokens, 6)
78+
7679
if (json) {
7780
output.buffer(tokens)
7881
return
7982
}
8083
if (parseable) {
81-
output.standard(['key', 'token', 'created', 'readonly', 'CIDR whitelist'].join('\t'))
84+
output.standard(['key', 'token', 'id', 'name', 'created', 'readonly', 'CIDR whitelist'].join('\t'))
8285
tokens.forEach(token => {
8386
output.standard(
8487
[
8588
token.key,
8689
token.token,
90+
token.id,
91+
token.name,
8792
token.created,
8893
token.readonly ? 'true' : 'false',
8994
token.cidr_whitelist ? token.cidr_whitelist.join(',') : '',
@@ -92,11 +97,10 @@ class Token extends BaseCommand {
9297
})
9398
return
9499
}
95-
this.generateTokenIds(tokens, 6)
96100
const chalk = this.npm.chalk
97101
for (const token of tokens) {
98102
const created = String(token.created).slice(0, 10)
99-
output.standard(`${chalk.blue('Token')} ${token.token}… with id ${chalk.cyan(token.id)} created ${created}`)
103+
output.standard(`${chalk.blue('Token')} ${token.token}… with id ${chalk.cyan(token.id)} name ${chalk.magenta(token.name)} created ${created}`)
100104
if (token.cidr_whitelist) {
101105
output.standard(`with IP whitelist: ${chalk.green(token.cidr_whitelist.join(','))}`)
102106
}

test/lib/commands/token.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const tokens = [
1313
{
1414
key: 'abcd1234abcd1234',
1515
token: 'efgh5678efgh5678',
16+
name: 'abcd001',
1617
cidr_whitelist: null,
1718
readonly: false,
1819
created: now,
@@ -21,6 +22,7 @@ const tokens = [
2122
{
2223
key: 'abcd1256',
2324
token: 'hgfe8765',
25+
name: 'abcd002',
2426
cidr_whitelist: ['192.168.1.1/32'],
2527
readonly: true,
2628
created: now,
@@ -63,9 +65,9 @@ t.test('token list', async t => {
6365
registry.getTokens(tokens)
6466
await npm.exec('token', [])
6567
t.strictSame(outputs, [
66-
`Token efgh5678efgh5678… with id abcd123 created ${now.slice(0, 10)}`,
68+
`Token efgh5678efgh5678… with id abcd123 name abcd001 created ${now.slice(0, 10)}`,
6769
'',
68-
`Token hgfe8765… with id abcd125 created ${now.slice(0, 10)}`,
70+
`Token hgfe8765… with id abcd125 name abcd002 created ${now.slice(0, 10)}`,
6971
'with IP whitelist: 192.168.1.1/32',
7072
'',
7173
])
@@ -104,9 +106,9 @@ t.test('token list parseable output', async t => {
104106
registry.getTokens(tokens)
105107
await npm.exec('token', [])
106108
t.strictSame(outputs, [
107-
'key\ttoken\tcreated\treadonly\tCIDR whitelist',
108-
`abcd1234abcd1234\tefgh5678efgh5678\t${now}\tfalse\t`,
109-
`abcd1256\thgfe8765\t${now}\ttrue\t192.168.1.1/32`,
109+
'key\ttoken\tid\tname\tcreated\treadonly\tCIDR whitelist',
110+
`abcd1234abcd1234\tefgh5678efgh5678\tabcd123\tabcd001\t${now}\tfalse\t`,
111+
`abcd1256\thgfe8765\tabcd125\tabcd002\t${now}\ttrue\t192.168.1.1/32`,
110112
])
111113
})
112114

0 commit comments

Comments
 (0)