-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcreateCredentials.js
More file actions
120 lines (103 loc) · 3.46 KB
/
createCredentials.js
File metadata and controls
120 lines (103 loc) · 3.46 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
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
// This script is derived from a script created for CSS:
// https://github.com/CommunitySolidServer/CommunitySolidServer/blob/main/test/deploy/createAccountCredentials.ts
if (process.argv.length !== 3) {
throw new Error('Exactly 1 parameter is needed: the server URL.');
}
const baseUrl = process.argv[2];
const alice = {
email: 'alice@example.com',
password: 'alice-secret',
podName: 'alice',
};
const bob = {
email: 'bob@example.com',
password: 'bob-secret',
podName: 'bob',
};
/**
* Registers a user with the server and provides them with a pod.
* @param user - The user settings necessary to register a user.
*/
async function register(user) {
// Get controls
let res = await fetch(new URL('.account/', baseUrl));
let { controls } = await res.json();
// Create account
res = await fetch(controls.account.create, { method: 'POST' });
if (res.status !== 200) {
throw new Error(`Account creation failed: ${await res.text()}`);
}
const authorization = `CSS-Account-Token ${(await res.json()).authorization}`;
// Get account controls
res = await fetch(controls.main.index, {
headers: { authorization },
});
({ controls } = await res.json());
// Add login method
res = await fetch(controls.password.create, {
method: 'POST',
headers: { authorization, 'content-type': 'application/json' },
body: JSON.stringify({
email: user.email,
password: user.password,
}),
});
if (res.status !== 200) {
throw new Error(`Login creation failed: ${await res.text()}`);
}
// Create pod
res = await fetch(controls.account.pod, {
method: 'POST',
headers: { authorization, 'content-type': 'application/json' },
body: JSON.stringify({ name: user.podName }),
});
if (res.status !== 200) {
throw new Error(`Pod creation failed: ${await res.text()}`);
}
const { webId } = await res.json();
return { webId, authorization };
}
/**
* Requests a client credentials API token.
* @param webId - WebID to create credentials for.
* @param authorization - Authorization header for the account that tries to create credentials.
* @returns The id/secret for the client credentials request.
*/
async function createCredentials(webId, authorization) {
let res = await fetch(new URL('.account/', baseUrl), {
headers: { authorization },
});
const { controls } = await res.json();
res = await fetch(controls.account.clientCredentials, {
method: 'POST',
headers: { authorization, 'content-type': 'application/json' },
body: JSON.stringify({ name: 'token', webId }),
});
if (res.status !== 200) {
throw new Error(`Token generation failed: ${await res.text()}`);
}
return res.json();
}
/**
* Generates all the necessary data and outputs the necessary lines
* that need to be added to the CTH environment file
* so it can use client credentials.
* @param user - User for which data needs to be generated.
*/
async function outputCredentials(user) {
const { webId, authorization } = await register(user);
const { id, secret } = await createCredentials(webId, authorization);
const name = user.podName.toUpperCase();
console.log(`USERS_${name}_CLIENTID=${id}`);
console.log(`USERS_${name}_CLIENTSECRET=${secret}`);
}
/**
* Ends the process and writes out an error in case something goes wrong.
*/
function endProcess(error) {
console.error(error);
process.exit(1);
}
// Create tokens for Alice and Bob
outputCredentials(alice).catch(endProcess);
outputCredentials(bob).catch(endProcess);