-
Notifications
You must be signed in to change notification settings - Fork 116
/
management.js
378 lines (351 loc) · 9.65 KB
/
management.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// Copyright (c) 2024 by Juliusz Chroboczek.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
'use strict';
/**
* httpError returns an error that encapsulates the status of the response r.
*
* @param {Response} r
* @returns {Error}
*/
function httpError(r) {
let s = r.statusText;
if(s === '') {
switch(r.status) {
case 401: s = 'Unauthorised'; break;
case 403: s = 'Forbidden'; break;
case 404: s = 'Not Found'; break;
}
}
return new Error(`The server said: ${r.status} ${s}`);
}
/**
* listObjects fetches a list of strings from the given URL.
*
* @param {string} url
* @returns {Promise<Array<string>>}
*/
async function listObjects(url) {
let r = await fetch(url);
if(!r.ok)
throw httpError(r);
let data = await r.json();
if(!(data instanceof Array))
throw new Error("Server didn't return array");
return data;
}
/**
* createObject makes a PUT request to url with JSON data.
* It fails if the object already exists.
*
* @param {string} url
* @param {Object} [values]
*/
async function createObject(url, values) {
if(!values)
values = {};
let r = await fetch(url, {
method: 'PUT',
body: JSON.stringify(values),
headers: {
'If-None-Match': '*',
'Content-Type:': 'application/json',
}
});
if(!r.ok)
throw httpError(r);
}
/**
* getObject fetches the JSON object at a given URL.
* If an ETag is provided, it fails if the ETag didn't match.
*
* @param {string} url
* @param {string} [etag]
* @returns {Promise<Object>}
*/
async function getObject(url, etag) {
let options = {};
if(etag) {
options.headers = {
'If-Match': etag
}
}
let r = await fetch(url, options);
if(!r.ok)
throw httpError(r);
let newetag = r.headers.get("ETag");
if(!newetag)
throw new Error("The server didn't return an ETag");
if(etag && newetag !== etag)
throw new Error("The server returned a mismatched ETag");
let data = await r.json();
return {etag: newetag, data: data}
}
/**
* deleteObject makes a DELETE request to the given URL.
* If an ETag is provided, it fails if the ETag didn't match.
*
* @param {string} url
* @param {string} [etag]
*/
async function deleteObject(url, etag) {
/** @type {Object<string, string>} */
let headers = {};
if(etag)
headers['If-Match'] = etag;
let r = await fetch(url, {
method: 'DELETE',
headers: headers,
});
if(!r.ok)
throw httpError(r);
}
/**
* updateObject makes a read-modify-write cycle on the given URL. Any
* fields that are non-null in values are added or modified, any fields
* that are null are deleted, any fields that are absent are left unchanged.
*
* @param {string} url
* @param {Object} values
* @param {string} [etag]
*/
async function updateObject(url, values, etag) {
let old = await getObject(url, etag);
let data = old.data;
for(let k in values) {
if(values[k])
data[k] = values[k];
else
delete(data[k])
}
let r = await fetch(url, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'If-Match': old.etag,
}
})
if(!r.ok)
throw httpError(r);
}
/**
* listGroups returns the list of groups.
*
* @returns {Promise<Array<string>>}
*/
async function listGroups() {
return await listObjects('/galene-api/v0/.groups/');
}
/**
* getGroup returns the sanitised description of the given group.
*
* @param {string} group
* @param {string} [etag]
* @returns {Promise<Object>}
*/
async function getGroup(group, etag) {
return await getObject(`/galene-api/v0/.groups/${group}`, etag);
}
/**
* createGroup creates a group. It fails if the group already exists.
*
* @param {string} group
* @param {Object} [values]
*/
async function createGroup(group, values) {
return await createObject(`/galene-api/v0/.groups/${group}`, values);
}
/**
* deleteGroup deletes a group.
*
* @param {string} group
* @param {string} [etag]
*/
async function deleteGroup(group, etag) {
return await deleteObject(`/galene-api/v0/.groups/${group}`, etag);
}
/**
* updateGroup modifies a group definition.
* Any fields present in values are overriden, any fields absent in values
* are left unchanged.
*
* @param {string} group
* @param {Object} values
* @param {string} [etag]
*/
async function updateGroup(group, values, etag) {
return await updateObject(`/galene-api/v0/.groups/${group}`, values);
}
/**
* listUsers lists the users in a given group.
*
* @param {string} group
* @returns {Promise<Array<string>>}
*/
async function listUsers(group) {
return await listObjects(`/galene-api/v0/.groups/${group}/.users/`);
}
/**
* userURL returns the URL for a user entry
*
* @param {string} group
* @param {string} user
* @param {boolean} wildcard
*/
function userURL(group, user, wildcard) {
if(wildcard)
return `/galene-api/v0/.groups/${group}/.wildcard-user`;
else if(user === "")
return `/galene-api/v0/.groups/${group}/.empty-user`;
else
return `/galene-api/v0/.groups/${group}/.users/${user}`
}
/**
* getUser returns a given user entry.
*
* @param {string} group
* @param {string} user
* @param {boolean} wildcard
* @param {string} [etag]
* @returns {Promise<Object>}
*/
async function getUser(group, user, wildcard, etag) {
return await getObject(userURL(group, user, wildcard), etag);
}
/**
* createUser creates a new user entry. It fails if the user already
* exists.
*
* @param {string} group
* @param {string} user
* @param {boolean} wildcard
* @param {Object} values
*/
async function createUser(group, user, wildcard, values) {
return await createObject(userURL(group, user, wildcard), values);
}
/**
* deleteUser deletes a user.
*
* @param {string} group
* @param {string} user
* @param {boolean} wildcard
* @param {string} [etag]
*/
async function deleteUser(group, user, wildcard, etag) {
return await deleteObject(userURL(group, user, wildcard), etag);
}
/**
* updateUser modifies a given user entry.
*
* @param {string} group
* @param {string} user
* @param {Object} values
* @param {boolean} wildcard
* @param {string} [etag]
*/
async function updateUser(group, user, wildcard, values, etag) {
return await updateObject(userURL(group, user, wildcard), values, etag);
}
/**
* setPassword sets a user's password.
* If oldpassword is provided, then it is used for authentication instead
* of the browser's normal mechanism.
*
* @param {string} group
* @param {string} user
* @param {boolean} wildcard
* @param {string} password
* @param {string} [oldpassword]
*/
async function setPassword(group, user, wildcard, password, oldpassword) {
let options = {
method: 'POST',
headers: {
'Content-Type': 'text/plain'
},
body: password,
}
if(oldpassword) {
options.credentials = 'omit';
options.headers['Authorization'] =
`Basic ${btoa(user + ':' + oldpassword)}`
}
let r = await fetch(userURL(group, user, wildcard) + '/.password', options);
if(!r.ok)
throw httpError(r);
}
/**
* listTokens lists the tokens for a given group.
*
* @param {string} group
* @returns {Promise<Array<string>>}
*/
async function listTokens(group) {
return await listObjects(`/galene-api/v0/.groups/${group}/.tokens/`);
}
/**
* getToken returns a given token.
*
* @param {string} group
* @param {string} token
* @param {string} [etag]
* @returns {Promise<Object>}
*/
async function getToken(group, token, etag) {
return await getObject(`/galene-api/v0/.groups/${group}/.tokens/${token}`,
etag);
}
/**
* createToken creates a new token and returns its name
*
* @param {string} group
* @param {Object} template
* @returns {Promise<string>}
*/
async function createToken(group, template) {
let options = {
method: 'POST',
headers: {
'Content-Type': 'text/json'
},
body: template,
}
let r = await fetch(
`/galene-api/v0/.groups/${group}/.tokens/`,
options);
if(!r.ok)
throw httpError(r);
let t = r.headers.get('Location');
if(!t)
throw new Error("Server didn't return location header");
return t;
}
/**
* updateToken modifies a token.
*
* @param {string} group
* @param {Object} token
*/
async function updateToken(group, token, etag) {
if(!token.token)
throw new Error("Unnamed token");
return await updateObject(
`/galene-api/v0/.groups/${group}/.tokens/${token.token}`,
token, etag);
}