Skip to content

Commit 5ac71d9

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

File tree

5 files changed

+548
-67
lines changed

5 files changed

+548
-67
lines changed

lib/commands/token.js

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
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> --access=<read-only|read-write> [--expires=<YYYY-MM-DD>] [--packages=<pkg1,pkg2>] [--scopes=<scope1,scope2>] [--orgs=<org1,org2>] [--cidr=<ip-range>] [--bypass-2fa]']
11+
static params = ['name',
12+
'expires',
13+
'access',
14+
'packages',
15+
'scopes',
16+
'orgs',
17+
'cidr',
18+
'bypass-2fa',
19+
'registry',
20+
'otp',
21+
'read-only',
22+
]
1223

1324
static async completion (opts) {
1425
const argv = opts.conf.argv.remain
@@ -127,15 +138,66 @@ class Token extends BaseCommand {
127138
const json = this.npm.config.get('json')
128139
const parseable = this.npm.config.get('parseable')
129140
const cidr = this.npm.config.get('cidr')
130-
const readonly = this.npm.config.get('read-only')
141+
const name = this.npm.config.get('name')
142+
const expires = this.npm.config.get('expires')
143+
const access = this.npm.config.get('access')
144+
const packages = this.npm.config.get('packages')
145+
const scopes = this.npm.config.get('scopes')
146+
const orgs = this.npm.config.get('orgs')
147+
const bypassTwoFactor = this.npm.config.get('bypass-2fa')
148+
149+
// Validate required parameters
150+
if (!name) {
151+
throw this.usageError('--name is required for token creation')
152+
}
153+
if (!access) {
154+
throw this.usageError('--access is required (use "read-only" or "read-write")')
155+
}
156+
if (!['read-only', 'read-write'].includes(access)) {
157+
throw this.usageError('--access must be either "read-only" or "read-write"')
158+
}
131159

132160
const validCIDR = await this.validateCIDRList(cidr)
161+
162+
// Prompt for password (required by backend for token creation)
133163
const password = await readUserInfo.password()
164+
165+
// Build GAT token data structure matching backend expectations
166+
const tokenData = {
167+
token_type: 'granular',
168+
name: name,
169+
access: access,
170+
password: password,
171+
}
172+
173+
// Add packages, scopes, and orgs as separate arrays (not nested objects)
174+
if (packages?.length > 0) {
175+
tokenData.packages = packages
176+
}
177+
if (scopes?.length > 0) {
178+
tokenData.scopes = scopes
179+
}
180+
if (orgs?.length > 0) {
181+
tokenData.orgs = orgs
182+
}
183+
if (expires) {
184+
tokenData.expires = 10 // Hardcoding for now. Backend expects # of days as an integer
185+
}
186+
187+
// Add optional fields
188+
if (validCIDR?.length > 0) {
189+
tokenData.cidr_whitelist = validCIDR
190+
}
191+
if (bypassTwoFactor) {
192+
tokenData.bypass_2fa = true
193+
}
194+
134195
log.info('token', 'creating')
196+
log.silly('token', 'request body:', JSON.stringify(tokenData, null, 2))
135197
const result = await otplease(
136198
this.npm,
137199
{ ...this.npm.flatOptions },
138-
c => createToken(password, readonly, validCIDR, c)
200+
c => createGatToken(tokenData, c)
139201
)
140202
delete result.key
141203
delete result.updated
@@ -145,12 +207,15 @@ class Token extends BaseCommand {
145207
Object.keys(result).forEach(k => output.standard(k + '\t' + result[k]))
146208
} else {
147209
const chalk = this.npm.chalk
148-
// Identical to list
149-
const level = result.readonly ? 'read only' : 'publish'
210+
// Display based on access level
211+
const level = result.access === 'read-only' || result.readonly ? 'read only' : 'publish'
150212
output.standard(`Created ${chalk.blue(level)} token ${result.token}`)
151213
if (result.cidr_whitelist?.length) {
152214
output.standard(`with IP whitelist: ${chalk.green(result.cidr_whitelist.join(','))}`)
153215
}
216+
if (result.expires) {
217+
output.standard(`expires: ${result.expires}`)
218+
}
154219
}
155220
}
156221

@@ -180,7 +245,7 @@ class Token extends BaseCommand {
180245
for (const cidr of list) {
181246
if (isCidrV6(cidr)) {
182247
throw this.invalidCIDRError(
183-
`CIDR whitelist can only contain IPv4 addresses${cidr} is IPv6`
248+
`CIDR whitelist can only contain IPv4 addresses, ${cidr} is IPv6`
184249
)
185250
}
186251

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

Lines changed: 12 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,7 @@ 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,
144150
"script-shell": null,
145151
"searchexclude": "",
146152
"searchlimit": 20,
@@ -187,6 +193,7 @@ auth-type = "web"
187193
before = null
188194
bin-links = true
189195
browser = null
196+
bypass-2fa = false
190197
ca = null
191198
; cache = "{CACHE}" ; overridden by cli
192199
cache-max = null
@@ -214,6 +221,7 @@ editor = "{EDITOR}"
214221
engine-strict = false
215222
expect-result-count = null
216223
expect-results = null
224+
expires = null
217225
fetch-retries = 2
218226
fetch-retry-factor = 10
219227
fetch-retry-maxtimeout = 60000
@@ -266,6 +274,7 @@ logs-max = 10
266274
; long = false ; overridden by cli
267275
maxsockets = 15
268276
message = "%s"
277+
name = null
269278
node-gyp = "{CWD}/node_modules/node-gyp/bin/node-gyp.js"
270279
node-options = null
271280
noproxy = [""]
@@ -275,12 +284,14 @@ omit = []
275284
omit-lockfile-registry-resolved = false
276285
only = null
277286
optional = null
287+
orgs = null
278288
os = null
279289
otp = null
280290
pack-destination = "."
281291
package = []
282292
package-lock = true
283293
package-lock-only = false
294+
packages = []
284295
parseable = false
285296
prefer-dedupe = false
286297
prefer-offline = false
@@ -307,6 +318,7 @@ save-prod = false
307318
sbom-format = null
308319
sbom-type = "library"
309320
scope = ""
321+
scopes = null
310322
script-shell = null
311323
searchexclude = ""
312324
searchlimit = 20

0 commit comments

Comments
 (0)