Skip to content

Commit 5ac7dab

Browse files
committed
feat!: support for creating granular access tokens
BREAKING CHANGE: The createToken function has been updated to support granular access tokens. The previous method of creating classic tokens is deprecated. Please use the new createToken function with appropriate parameters for granular tokens.
1 parent ef7203a commit 5ac7dab

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed

lib/index.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,14 +236,10 @@ const removeToken = async (tokenKey, opts = {}) => {
236236
return null
237237
}
238238

239-
const createToken = (password, readonly, cidrs, opts = {}) => fetch.json('/-/npm/v1/tokens', {
239+
const createToken = (tokenData, opts = {}) => fetch.json('/-/npm/v1/tokens', {
240240
...opts,
241241
method: 'POST',
242-
body: {
243-
password: password,
244-
readonly: readonly,
245-
cidr_whitelist: cidrs,
246-
},
242+
body: tokenData,
247243
})
248244

249245
class WebLoginInvalidResponse extends HttpErrorBase {

test/index.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -271,21 +271,24 @@ test('removeToken', t => {
271271
})
272272

273273
test('createToken', t => {
274-
const base = {
275-
password: 'secretPassw0rd!',
276-
readonly: true,
277-
cidr_whitelist: ['8.8.8.8/32', '127.0.0.1', '192.168.1.1'],
274+
const tokenData = {
275+
type: 'granular',
276+
name: 'CI Publish Token',
277+
access: 'read-write',
278+
expires: '2025-12-01T00:00:00Z',
279+
packages: ['@my-org/my-package'],
280+
scopes: ['@my-org'],
281+
cidr_whitelist: ['8.8.8.8/32'],
278282
}
279-
const obj = Object.assign({
280-
token: 'deadbeef',
281-
key: 'sha512-badc0ffee',
282-
created: (new Date()).toString(),
283-
})
284-
delete obj.password
285-
tnock(t, registry).post('/-/npm/v1/tokens', base).reply(200, obj)
286-
return profile.createToken(
287-
base.password,
288-
base.readonly,
289-
base.cidr_whitelist
290-
).then(ret => t.same(ret, obj, 'got the right return value'))
283+
const response = {
284+
id: '789abc',
285+
token: 'npm_gat_abcdef',
286+
name: 'CI Publish Token',
287+
type: 'granular',
288+
access: 'read-write',
289+
expires: '2025-12-01T00:00:00Z',
290+
created: '2025-04-15T08:20:00Z',
291+
}
292+
tnock(t, registry).post('/-/npm/v1/tokens', tokenData).reply(201, response)
293+
return profile.createToken(tokenData).then(ret => t.same(ret, response, 'got the right return value'))
291294
})

0 commit comments

Comments
 (0)