Skip to content

Commit 12e237f

Browse files
committed
fix: Uses the npm-profile package to create tokens with GAT support
1 parent 06510a8 commit 12e237f

File tree

5 files changed

+670
-59
lines changed

5 files changed

+670
-59
lines changed

lib/commands/token.js

Lines changed: 84 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
11
const { log, output } = require('proc-log')
2-
const { listTokens, createToken, removeToken } = require('npm-profile')
2+
const { listTokens, createGatToken, removeToken } = require('npm-profile')
33
const { otplease } = require('../utils/auth.js')
44
const readUserInfo = require('../utils/read-user-info.js')
55
const BaseCommand = require('../base-cmd.js')
66

77
class Token extends BaseCommand {
88
static description = 'Manage your authentication tokens'
99
static name = 'token'
10-
static usage = ['list', 'revoke <id|token>', 'create [--read-only] [--cidr=list]']
11-
static params = ['read-only', 'cidr', 'registry', 'otp']
10+
static usage = ['list', 'revoke <id|token>', 'create --name=<name> [--token-description=<desc>] [--packages=<pkg1,pkg2>] [--packages-all] [--scopes=<scope1,scope2>] [--orgs=<org1,org2>] [--packages-and-scopes-permission=<read-only|read-write|no-access>] [--orgs-permission=<read-only|read-write|no-access>] [--expires=<days>] [--cidr=<ip-range>] [--bypass-2fa] [--password=<pass>]']
11+
static params = ['name',
12+
'token-description',
13+
'expires',
14+
'packages',
15+
'packages-all',
16+
'scopes',
17+
'orgs',
18+
'packages-and-scopes-permission',
19+
'orgs-permission',
20+
'cidr',
21+
'bypass-2fa',
22+
'password',
23+
'registry',
24+
'otp',
25+
'read-only',
26+
]
1227

1328
static async completion (opts) {
1429
const argv = opts.conf.argv.remain
@@ -127,15 +142,72 @@ class Token extends BaseCommand {
127142
const json = this.npm.config.get('json')
128143
const parseable = this.npm.config.get('parseable')
129144
const cidr = this.npm.config.get('cidr')
130-
const readonly = this.npm.config.get('read-only')
145+
const name = this.npm.config.get('name')
146+
const tokenDescription = this.npm.config.get('token-description')
147+
const expires = this.npm.config.get('expires')
148+
const packages = this.npm.config.get('packages')
149+
const packagesAll = this.npm.config.get('packages-all')
150+
const scopes = this.npm.config.get('scopes')
151+
const orgs = this.npm.config.get('orgs')
152+
const packagesAndScopesPermission = this.npm.config.get('packages-and-scopes-permission')
153+
const orgsPermission = this.npm.config.get('orgs-permission')
154+
const bypassTwoFactor = this.npm.config.get('bypass-2fa')
155+
let password = this.npm.config.get('password')
131156

132157
const validCIDR = await this.validateCIDRList(cidr)
133-
const password = await readUserInfo.password()
158+
159+
/* istanbul ignore if - skip testing read input */
160+
if (!password) {
161+
password = await readUserInfo.password()
162+
}
163+
164+
const tokenData = {
165+
name: name,
166+
password: password,
167+
}
168+
169+
if (tokenDescription) {
170+
tokenData.description = tokenDescription
171+
}
172+
173+
if (packages?.length > 0) {
174+
tokenData.packages = packages
175+
}
176+
if (packagesAll) {
177+
tokenData.packages_all = true
178+
}
179+
if (scopes?.length > 0) {
180+
tokenData.scopes = scopes
181+
}
182+
if (orgs?.length > 0) {
183+
tokenData.orgs = orgs
184+
}
185+
186+
if (packagesAndScopesPermission) {
187+
tokenData.packages_and_scopes_permission = packagesAndScopesPermission
188+
}
189+
if (orgsPermission) {
190+
tokenData.orgs_permission = orgsPermission
191+
}
192+
193+
// Add expiration in days
194+
if (expires) {
195+
tokenData.expires = parseInt(expires, 10)
196+
}
197+
198+
// Add optional fields
199+
if (validCIDR?.length > 0) {
200+
tokenData.cidr_whitelist = validCIDR
201+
}
202+
if (bypassTwoFactor) {
203+
tokenData.bypass_2fa = true
204+
}
205+
134206
log.info('token', 'creating')
135207
const result = await otplease(
136208
this.npm,
137209
{ ...this.npm.flatOptions },
138-
c => createToken(password, readonly, validCIDR, c)
210+
c => createGatToken(tokenData, c)
139211
)
140212
delete result.key
141213
delete result.updated
@@ -145,12 +217,15 @@ class Token extends BaseCommand {
145217
Object.keys(result).forEach(k => output.standard(k + '\t' + result[k]))
146218
} else {
147219
const chalk = this.npm.chalk
148-
// Identical to list
149-
const level = result.readonly ? 'read only' : 'publish'
220+
// Display based on access level
221+
const level = result.access === 'read-only' || result.readonly ? 'read only' : 'publish'
150222
output.standard(`Created ${chalk.blue(level)} token ${result.token}`)
151223
if (result.cidr_whitelist?.length) {
152224
output.standard(`with IP whitelist: ${chalk.green(result.cidr_whitelist.join(','))}`)
153225
}
226+
if (result.expires) {
227+
output.standard(`expires: ${result.expires}`)
228+
}
154229
}
155230
}
156231

@@ -180,7 +255,7 @@ class Token extends BaseCommand {
180255
for (const cidr of list) {
181256
if (isCidrV6(cidr)) {
182257
throw this.invalidCIDRError(
183-
`CIDR whitelist can only contain IPv4 addresses${cidr} is IPv6`
258+
`CIDR whitelist can only contain IPv4 addresses, ${cidr} is IPv6`
184259
)
185260
}
186261

tap-snapshots/test/lib/commands/config.js.test.cjs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ exports[`test/lib/commands/config.js TAP config list --json > output matches sna
2323
"before": null,
2424
"bin-links": true,
2525
"browser": null,
26+
"bypass-2fa": false,
2627
"ca": null,
2728
"cache-max": null,
2829
"cache-min": 0,
@@ -48,6 +49,7 @@ exports[`test/lib/commands/config.js TAP config list --json > output matches sna
4849
"engine-strict": false,
4950
"expect-result-count": null,
5051
"expect-results": null,
52+
"expires": null,
5153
"fetch-retries": 2,
5254
"fetch-retry-factor": 10,
5355
"fetch-retry-maxtimeout": 60000,
@@ -97,6 +99,7 @@ exports[`test/lib/commands/config.js TAP config list --json > output matches sna
9799
"logs-dir": null,
98100
"logs-max": 10,
99101
"long": false,
102+
"name": null,
100103
"maxsockets": 15,
101104
"message": "%s",
102105
"node-gyp": "{CWD}/node_modules/node-gyp/bin/node-gyp.js",
@@ -108,13 +111,15 @@ exports[`test/lib/commands/config.js TAP config list --json > output matches sna
108111
"omit": [],
109112
"omit-lockfile-registry-resolved": false,
110113
"only": null,
114+
"orgs": null,
111115
"optional": null,
112116
"os": null,
113117
"otp": null,
114118
"package": [],
115119
"package-lock": true,
116120
"package-lock-only": false,
117121
"pack-destination": ".",
122+
"packages": [],
118123
"parseable": false,
119124
"prefer-dedupe": false,
120125
"prefer-offline": false,
@@ -141,6 +146,11 @@ exports[`test/lib/commands/config.js TAP config list --json > output matches sna
141146
"sbom-format": null,
142147
"sbom-type": "library",
143148
"scope": "",
149+
"scopes": null,
150+
"packages-all": false,
151+
"packages-and-scopes-permission": null,
152+
"orgs-permission": null,
153+
"token-description": null,
144154
"script-shell": null,
145155
"searchexclude": "",
146156
"searchlimit": 20,
@@ -187,6 +197,7 @@ auth-type = "web"
187197
before = null
188198
bin-links = true
189199
browser = null
200+
bypass-2fa = false
190201
ca = null
191202
; cache = "{CACHE}" ; overridden by cli
192203
cache-max = null
@@ -214,6 +225,7 @@ editor = "{EDITOR}"
214225
engine-strict = false
215226
expect-result-count = null
216227
expect-results = null
228+
expires = null
217229
fetch-retries = 2
218230
fetch-retry-factor = 10
219231
fetch-retry-maxtimeout = 60000
@@ -266,6 +278,7 @@ logs-max = 10
266278
; long = false ; overridden by cli
267279
maxsockets = 15
268280
message = "%s"
281+
name = null
269282
node-gyp = "{CWD}/node_modules/node-gyp/bin/node-gyp.js"
270283
node-options = null
271284
noproxy = [""]
@@ -275,13 +288,19 @@ omit = []
275288
omit-lockfile-registry-resolved = false
276289
only = null
277290
optional = null
291+
orgs = null
292+
orgs-permission = null
278293
os = null
279294
otp = null
280295
pack-destination = "."
281296
package = []
282297
package-lock = true
283298
package-lock-only = false
299+
packages = []
300+
packages-all = false
301+
packages-and-scopes-permission = null
284302
parseable = false
303+
password = (protected)
285304
prefer-dedupe = false
286305
prefer-offline = false
287306
prefer-online = false
@@ -307,6 +326,7 @@ save-prod = false
307326
sbom-format = null
308327
sbom-type = "library"
309328
scope = ""
329+
scopes = null
310330
script-shell = null
311331
searchexclude = ""
312332
searchlimit = 20
@@ -321,6 +341,7 @@ strict-ssl = true
321341
tag = "latest"
322342
tag-version-prefix = "v"
323343
timing = false
344+
token-description = null
324345
umask = 0
325346
unicode = false
326347
update-notifier = true

0 commit comments

Comments
 (0)