-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathcreateUserSession.ts
More file actions
66 lines (52 loc) · 2.45 KB
/
Copy pathcreateUserSession.ts
File metadata and controls
66 lines (52 loc) · 2.45 KB
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
'use strict';
/*
* A tool for generating a test user session which can be used for debugging configs
* that require sessions.
*/
// As of v14, Node.js does not exit when there is an unhandled Promise rejection. Convert an
// unhandled rejection into an uncaught exception, which does cause Node.js to exit.
import fs from "node:fs";
import path from "node:path";
import querystring from "node:querystring";
import process from "node:process";
process.on('unhandledRejection', (err) => { throw err; });
import settings from 'ep_etherpad-lite/node/utils/Settings';
(async () => {
const baseURL = `http://${settings.ip}:${settings.port}`;
const apiGet = async (p: string): Promise<any> => {
const r = await fetch(baseURL + p);
if (!r.ok) throw new Error(`HTTP ${r.status} ${r.statusText}`);
return r.json();
};
const apiPost = async (p: string): Promise<any> => {
const r = await fetch(baseURL + p, {method: 'POST'});
if (!r.ok) throw new Error(`HTTP ${r.status} ${r.statusText}`);
return r.json();
};
const filePath = path.join(__dirname, '../APIKEY.txt');
const apikey = fs.readFileSync(filePath, {encoding: 'utf-8'});
let res;
res = await apiGet('/api/');
const apiVersion = res.currentVersion;
if (!apiVersion) throw new Error('No version set in API');
console.log('apiVersion', apiVersion);
const uri = (cmd: string, args: querystring.ParsedUrlQueryInput ) => `/api/${apiVersion}/${cmd}?${querystring.stringify(args)}`;
res = await apiPost(uri('createGroup', {apikey}));
if (res.code === 1) throw new Error(`Error creating group: ${res}`);
const groupID = res.data.groupID;
console.log('groupID', groupID);
res = await apiPost(uri('createGroupPad', {apikey, groupID}));
if (res.code === 1) throw new Error(`Error creating group pad: ${res}`);
console.log('Test Pad ID ====> ', res.data.padID);
res = await apiPost(uri('createAuthor', {apikey}));
if (res.code === 1) throw new Error(`Error creating author: ${res}`);
const authorID = res.data.authorID;
console.log('authorID', authorID);
const validUntil = Math.floor(new Date().getTime() / 1000) + 60000;
console.log('validUntil', validUntil);
res = await apiPost(uri('createSession', {apikey, groupID, authorID, validUntil}));
if (res.code === 1) throw new Error(`Error creating session: ${JSON.stringify(res)}`);
console.log('Session made: ====> create a cookie named sessionID and set the value to',
res.data.sessionID);
process.exit(0)
})();