Skip to content

Commit 37c856b

Browse files
committed
wip
1 parent a0b368f commit 37c856b

File tree

3 files changed

+407
-372
lines changed

3 files changed

+407
-372
lines changed

lib/commands/token.js

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

7+
async function paginate(href, opts, items = []) {
8+
while (href) {
9+
const result = await fetch.json(href, opts)
10+
items = items.concat(result.objects)
11+
href = result.urls.next
12+
}
13+
return items
14+
}
15+
716
class Token extends BaseCommand {
817
static description = 'Manage your authentication tokens'
918
static name = 'token'
@@ -63,7 +72,7 @@ class Token extends BaseCommand {
6372
const json = this.npm.config.get('json')
6473
const parseable = this.npm.config.get('parseable')
6574
log.info('token', 'getting list')
66-
const tokens = await listTokens(this.npm.flatOptions)
75+
const tokens = await paginate('/-/npm/v1/tokens', this.npm.flatOptions)
6776
if (json) {
6877
output.buffer(tokens)
6978
return
@@ -104,10 +113,9 @@ class Token extends BaseCommand {
104113
const json = this.npm.config.get('json')
105114
const parseable = this.npm.config.get('parseable')
106115
const toRemove = []
107-
const opts = { ...this.npm.flatOptions }
108116
log.info('token', `removing ${toRemove.length} tokens`)
109-
const tokens = await listTokens(opts)
110-
args.forEach(id => {
117+
const tokens = await paginate('/-/npm/v1/tokens', this.npm.flatOptions)
118+
for (const id of args) {
111119
const matches = tokens.filter(token => token.key.indexOf(id) === 0)
112120
if (matches.length === 1) {
113121
toRemove.push(matches[0].key)
@@ -123,12 +131,16 @@ class Token extends BaseCommand {
123131

124132
toRemove.push(id)
125133
}
126-
})
127-
await Promise.all(
128-
toRemove.map(key => {
129-
return otplease(this.npm, opts, c => removeToken(key, c))
130-
})
131-
)
134+
}
135+
for (const tokenKey of toRemove) {
136+
await otplease(this.npm, this.npm.flatOptions, opts =>
137+
fetch(`/-/npm/v1/tokens/token/${tokenKey}`, {
138+
...opts,
139+
method: 'DELETE',
140+
ignoreBody: true,
141+
})
142+
)
143+
}
132144
if (json) {
133145
output.buffer(toRemove)
134146
} else if (parseable) {
@@ -204,10 +216,12 @@ class Token extends BaseCommand {
204216
}
205217

206218
log.info('token', 'creating')
207-
const result = await otplease(
208-
this.npm,
209-
{ ...this.npm.flatOptions },
210-
c => createGatToken(tokenData, c)
219+
const result = await otplease(this.npm, this.npm.flatOptions, opts =>
220+
fetch.json('/-/npm/v1/tokens', {
221+
...opts,
222+
method: 'POST',
223+
body: tokenData,
224+
})
211225
)
212226
delete result.key
213227
delete result.updated

mock-registry/lib/index.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ class MockRegistry {
442442
}
443443

444444
getTokens (tokens) {
445-
return this.nock.get('/-/npm/v1/tokens')
445+
return this.nock.get(this.fullPath('/-/npm/v1/tokens'))
446446
.reply(200, {
447447
objects: tokens,
448448
urls: {},
@@ -451,19 +451,26 @@ class MockRegistry {
451451
})
452452
}
453453

454-
createToken ({ password, readonly = false, cidr = [] }) {
455-
return this.nock.post('/-/npm/v1/tokens', {
456-
password,
457-
readonly,
458-
cidr_whitelist: cidr,
459-
}).reply(200, {
460-
key: 'n3wk3y',
461-
token: 'n3wt0k3n',
462-
created: new Date(),
463-
updated: new Date(),
464-
readonly,
465-
cidr_whitelist: cidr,
466-
})
454+
// The server has rules for what resultData correlates with what tokenData but we don't need to be 100% in sync with that, we just need to be able to pass all of the possible tokenData attributes, and be able to accept all of the possible resultData attributes
455+
createToken (tokenData, resultData = {}) {
456+
return this.nock.post(this.fullPath('/-/npm/v1/tokens'), tokenData)
457+
.reply(201, {
458+
id: `0xdeadbeef`,
459+
key: 'n3wk3y',
460+
token: 'n3wt0k3n',
461+
created: new Date(),
462+
updated: new Date(),
463+
access: 'read-only',
464+
name: tokenData.name,
465+
password: tokenData.password,
466+
...resultData
467+
})
468+
}
469+
470+
revokeToken (token) {
471+
return this.nock.delete(
472+
this.fullPath(`/-/npm/v1/tokens/token/${token}`)
473+
).reply(200)
467474
}
468475

469476
async package ({ manifest, times = 1, query, tarballs }) {

0 commit comments

Comments
 (0)