-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.js
278 lines (242 loc) · 6.71 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
const { URL } = require('node:url')
const timers = require('node:timers/promises')
const fetch = require('npm-registry-fetch')
const { HttpErrorBase } = require('npm-registry-fetch/lib/errors')
const { log } = require('proc-log')
// try loginWeb, catch the "not supported" message and fall back to couch
const login = async (opener, prompter, opts = {}) => {
try {
return await loginWeb(opener, opts)
} catch (er) {
if (er instanceof WebLoginNotSupported) {
log.verbose('web login', 'not supported, trying couch')
const { username, password } = await prompter(opts.creds)
return loginCouch(username, password, opts)
}
throw er
}
}
const adduser = async (opener, prompter, opts = {}) => {
try {
return await adduserWeb(opener, opts)
} catch (er) {
if (er instanceof WebLoginNotSupported) {
log.verbose('web adduser', 'not supported, trying couch')
const { username, email, password } = await prompter(opts.creds)
return adduserCouch(username, email, password, opts)
}
throw er
}
}
const adduserWeb = (opener, opts = {}) => {
log.verbose('web adduser', 'before first POST')
return webAuth(opener, opts, { create: true })
}
const loginWeb = (opener, opts = {}) => {
log.verbose('web login', 'before first POST')
return webAuth(opener, opts, {})
}
const isValidUrl = u => {
try {
return /^https?:$/.test(new URL(u).protocol)
} catch {
return false
}
}
const webAuth = async (opener, opts, body) => {
try {
const res = await fetch('/-/v1/login', {
...opts,
method: 'POST',
body,
})
const content = await res.json()
log.verbose('web auth', 'got response', content)
const { doneUrl, loginUrl } = content
if (!isValidUrl(doneUrl) || !isValidUrl(loginUrl)) {
throw new WebLoginInvalidResponse('POST', res, content)
}
return await webAuthOpener(opener, loginUrl, doneUrl, opts)
} catch (er) {
if ((er.statusCode >= 400 && er.statusCode <= 499) || er.statusCode === 500) {
throw new WebLoginNotSupported('POST', {
status: er.statusCode,
headers: er.headers,
}, er.body)
}
throw er
}
}
const webAuthOpener = async (opener, loginUrl, doneUrl, opts) => {
const abortController = new AbortController()
const { signal } = abortController
try {
log.verbose('web auth', 'opening url pair')
const [, authResult] = await Promise.all([
opener(loginUrl, { signal }).catch((err) => {
if (err.name === 'AbortError') {
abortController.abort()
return
}
throw err
}),
webAuthCheckLogin(doneUrl, { ...opts, cache: false }, { signal }).then((r) => {
log.verbose('web auth', 'done-check finished')
abortController.abort()
return r
}),
])
return authResult
} catch (er) {
abortController.abort()
throw er
}
}
const webAuthCheckLogin = async (doneUrl, opts, { signal } = {}) => {
signal?.throwIfAborted()
const res = await fetch(doneUrl, opts)
const content = await res.json()
if (res.status === 200) {
if (!content.token) {
throw new WebLoginInvalidResponse('GET', res, content)
}
return content
}
if (res.status === 202) {
const retry = +res.headers.get('retry-after') * 1000
if (retry > 0) {
await timers.setTimeout(retry, null, { signal })
}
return webAuthCheckLogin(doneUrl, opts, { signal })
}
throw new WebLoginInvalidResponse('GET', res, content)
}
const couchEndpoint = (username) => `/-/user/org.couchdb.user:${encodeURIComponent(username)}`
const putCouch = async (path, username, body, opts) => {
const result = await fetch.json(`${couchEndpoint(username)}${path}`, {
...opts,
method: 'PUT',
body,
})
result.username = username
return result
}
const adduserCouch = async (username, email, password, opts = {}) => {
const body = {
_id: `org.couchdb.user:${username}`,
name: username,
password: password,
email: email,
type: 'user',
roles: [],
date: new Date().toISOString(),
}
log.verbose('adduser', 'before first PUT', {
...body,
password: 'XXXXX',
})
return putCouch('', username, body, opts)
}
const loginCouch = async (username, password, opts = {}) => {
const body = {
_id: `org.couchdb.user:${username}`,
name: username,
password: password,
type: 'user',
roles: [],
date: new Date().toISOString(),
}
log.verbose('login', 'before first PUT', {
...body,
password: 'XXXXX',
})
try {
return await putCouch('', username, body, opts)
} catch (err) {
if (err.code === 'E400') {
err.message = `There is no user with the username "${username}".`
throw err
}
if (err.code !== 'E409') {
throw err
}
}
const result = await fetch.json(couchEndpoint(username), {
...opts,
query: { write: true },
})
for (const k of Object.keys(result)) {
if (!body[k] || k === 'roles') {
body[k] = result[k]
}
}
return putCouch(`/-rev/${body._rev}`, username, body, {
...opts,
forceAuth: {
username,
password: Buffer.from(password, 'utf8').toString('base64'),
otp: opts.otp,
},
})
}
const get = (opts = {}) => fetch.json('/-/npm/v1/user', opts)
const set = (profile, opts = {}) => fetch.json('/-/npm/v1/user', {
...opts,
method: 'POST',
// profile keys can't be empty strings, but they CAN be null
body: Object.fromEntries(Object.entries(profile).map(([k, v]) => [k, v === '' ? null : v])),
})
const paginate = async (href, opts, items = []) => {
const result = await fetch.json(href, opts)
items = items.concat(result.objects)
if (result.urls.next) {
return paginate(result.urls.next, opts, items)
}
return items
}
const listTokens = (opts = {}) => paginate('/-/npm/v1/tokens', opts)
const removeToken = async (tokenKey, opts = {}) => {
await fetch(`/-/npm/v1/tokens/token/${tokenKey}`, {
...opts,
method: 'DELETE',
ignoreBody: true,
})
return null
}
const createToken = (password, readonly, cidrs, opts = {}) => fetch.json('/-/npm/v1/tokens', {
...opts,
method: 'POST',
body: {
password: password,
readonly: readonly,
cidr_whitelist: cidrs,
},
})
class WebLoginInvalidResponse extends HttpErrorBase {
constructor (method, res, body) {
super(method, res, body)
this.message = 'Invalid response from web login endpoint'
}
}
class WebLoginNotSupported extends HttpErrorBase {
constructor (method, res, body) {
super(method, res, body)
this.message = 'Web login not supported'
this.code = 'ENYI'
}
}
module.exports = {
adduserCouch,
loginCouch,
adduserWeb,
loginWeb,
login,
adduser,
get,
set,
listTokens,
removeToken,
createToken,
webAuthCheckLogin,
webAuthOpener,
}