WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 0e54172

Browse files
committed
fix: updates params for token create to accept csv
1 parent 54929ce commit 0e54172

File tree

2 files changed

+132
-5
lines changed

2 files changed

+132
-5
lines changed

lib/commands/token.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,10 @@ class Token extends BaseCommand {
157157
const name = this.npm.config.get('name')
158158
const tokenDescription = this.npm.config.get('token-description')
159159
const expires = this.npm.config.get('expires')
160-
const packages = this.npm.config.get('packages')
160+
const packages = this.normalizeList(this.npm.config.get('packages'))
161161
const packagesAll = this.npm.config.get('packages-all')
162-
const scopes = this.npm.config.get('scopes')
163-
const orgs = this.npm.config.get('orgs')
162+
const scopes = this.normalizeList(this.npm.config.get('scopes'))
163+
const orgs = this.normalizeList(this.npm.config.get('orgs'))
164164
const packagesAndScopesPermission = this.npm.config.get('packages-and-scopes-permission')
165165
const orgsPermission = this.npm.config.get('orgs-permission')
166166
const bypassTwoFactor = this.npm.config.get('bypass-2fa')
@@ -244,6 +244,11 @@ class Token extends BaseCommand {
244244
}
245245
}
246246

247+
normalizeList (items) {
248+
const maybeList = [].concat(items).filter(Boolean)
249+
return maybeList.length === 1 ? maybeList[0].split(/,\s*/) : maybeList
250+
}
251+
247252
invalidCIDRError (msg) {
248253
return Object.assign(new Error(msg), { code: 'EINVALIDCIDR' })
249254
}
@@ -265,8 +270,7 @@ class Token extends BaseCommand {
265270

266271
async validateCIDRList (cidrs) {
267272
const { v4: isCidrV4, v6: isCidrV6 } = await import('is-cidr')
268-
const maybeList = [].concat(cidrs).filter(Boolean)
269-
const list = maybeList.length === 1 ? maybeList[0].split(/,\s*/) : maybeList
273+
const list = this.normalizeList(cidrs)
270274
for (const cidr of list) {
271275
if (isCidrV6(cidr)) {
272276
throw this.invalidCIDRError(

test/lib/commands/token.js

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,3 +473,126 @@ t.test('token create invalid cidr', async t => {
473473
message: 'CIDR whitelist contains invalid CIDR entry: apple/cider',
474474
})
475475
})
476+
477+
t.test('token create with comma-separated packages', async t => {
478+
const { npm, outputs } = await loadMockNpm(t, {
479+
config: {
480+
...auth,
481+
name: 'csv-packages-token',
482+
password: 'test-password',
483+
packages: '@npmcli/pkg1,@npmcli/pkg2,@npmcli/pkg3',
484+
'packages-and-scopes-permission': 'read-write',
485+
},
486+
})
487+
488+
const registry = new MockRegistry({
489+
tap: t,
490+
registry: npm.config.get('registry'),
491+
authorization: authToken,
492+
})
493+
494+
registry.createToken({
495+
name: 'csv-packages-token',
496+
password: 'test-password',
497+
packages: ['@npmcli/pkg1', '@npmcli/pkg2', '@npmcli/pkg3'],
498+
packages_and_scopes_permission: 'read-write',
499+
}, {
500+
access: 'publish',
501+
})
502+
503+
await npm.exec('token', ['create'])
504+
t.match(outputs, ['Created publish token n3wt0k3n'])
505+
})
506+
507+
t.test('token create with comma-separated scopes', async t => {
508+
const { npm, outputs } = await loadMockNpm(t, {
509+
config: {
510+
...auth,
511+
name: 'csv-scopes-token',
512+
password: 'test-password',
513+
scopes: '@npmcli,@scope2,@scope3',
514+
'packages-and-scopes-permission': 'read-only',
515+
},
516+
})
517+
518+
const registry = new MockRegistry({
519+
tap: t,
520+
registry: npm.config.get('registry'),
521+
authorization: authToken,
522+
})
523+
524+
registry.createToken({
525+
name: 'csv-scopes-token',
526+
password: 'test-password',
527+
scopes: ['@npmcli', '@scope2', '@scope3'],
528+
packages_and_scopes_permission: 'read-only',
529+
}, {
530+
access: 'read-only',
531+
})
532+
533+
await npm.exec('token', ['create'])
534+
t.match(outputs, ['Created read only token n3wt0k3n'])
535+
})
536+
537+
t.test('token create with comma-separated orgs', async t => {
538+
const { npm, outputs } = await loadMockNpm(t, {
539+
config: {
540+
...auth,
541+
name: 'csv-orgs-token',
542+
password: 'test-password',
543+
orgs: '@npmcli,@org2,@org3',
544+
'orgs-permission': 'read-write',
545+
},
546+
})
547+
548+
const registry = new MockRegistry({
549+
tap: t,
550+
registry: npm.config.get('registry'),
551+
authorization: authToken,
552+
})
553+
554+
registry.createToken({
555+
name: 'csv-orgs-token',
556+
password: 'test-password',
557+
orgs: ['@npmcli', '@org2', '@org3'],
558+
orgs_permission: 'read-write',
559+
}, {
560+
access: 'publish',
561+
})
562+
563+
await npm.exec('token', ['create'])
564+
t.match(outputs, ['Created publish token n3wt0k3n'])
565+
})
566+
567+
t.test('token create with comma-separated cidr', async t => {
568+
const { npm, outputs } = await loadMockNpm(t, {
569+
config: {
570+
...auth,
571+
name: 'csv-cidr-token',
572+
password: 'test-password',
573+
cidr: '10.0.0.0/8,192.168.1.0/24,172.16.0.0/12',
574+
},
575+
})
576+
577+
const registry = new MockRegistry({
578+
tap: t,
579+
registry: npm.config.get('registry'),
580+
authorization: authToken,
581+
})
582+
583+
const expires = new Date()
584+
registry.createToken({
585+
name: 'csv-cidr-token',
586+
password: 'test-password',
587+
cidr_whitelist: ['10.0.0.0/8', '192.168.1.0/24', '172.16.0.0/12'],
588+
}, {
589+
cidr_whitelist: ['10.0.0.0/8', '192.168.1.0/24', '172.16.0.0/12'],
590+
expires,
591+
})
592+
593+
await npm.exec('token', ['create'])
594+
t.match(outputs, [
595+
'Created read only token n3wt0k3n',
596+
'with IP whitelist: 10.0.0.0/8,192.168.1.0/24,172.16.0.0/12',
597+
])
598+
})

0 commit comments

Comments
 (0)