-
Notifications
You must be signed in to change notification settings - Fork 4
/
ltitool.ts
381 lines (330 loc) · 11.3 KB
/
ltitool.ts
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
379
380
381
import * as jose from "jose";
import { LTI13ConfModel, LTIConfModel } from "./src/models/modelClasses";
import { dbConnect } from "./src/utils/database";
import * as dotenv from "dotenv";
import * as fs from "fs";
dotenv.config();
if (fs.existsSync(".env.local")) {
dotenv.config({ path: ".env.local", override: true });
}
const randomstring = require("randomstring");
const argv = require("yargs/yargs")(process.argv.slice(2)).argv;
// ************************************************************************************************
async function tool() {
await dbConnect();
const commandsLTI11 = ["delete", "list", "set", "show"];
const commandsLTI13 = ["create", "delete", "export", "import", "initialize", "list", "set", "show"];
if (argv.ltiver !== 11 && argv.ltiver !== 13) {
console.log("Incorrect LTI version, must be 11 or 13!");
process.exit(1);
}
if (argv._.length < 1 || (argv.ltiver === 11 && commandsLTI11.indexOf(argv._[0]) < 0)) {
console.log("Unknown command, must be: " + commandsLTI11.join(", "));
process.exit(1);
}
if (argv._.length < 1 || (argv.ltiver === 13 && commandsLTI13.indexOf(argv._[0]) < 0)) {
console.log("Unknown command, must be: " + commandsLTI13.join(", "));
process.exit(1);
}
// ************************************************************************************************
if (argv.ltiver === 11) {
if (argv._[0] === "set") {
if (!argv.key) {
console.log("Key is not defined!");
process.exit(1);
}
const secret = argv.secret ?? randomstring.generate();
const conf = await LTIConfModel.findOrCreate(
{ name: argv.key },
{ version: argv.ltiver, content: JSON.stringify({ secret }), description: argv.desc ?? "" }
);
if (!conf.created) {
conf.doc.content = JSON.stringify({ secret });
conf.doc.description = argv.desc ?? "";
await conf.doc.save();
}
console.log("LTI key is set.");
console.log("Key: " + argv.key);
if (argv.desc) {
console.log("Description: " + argv.desc);
}
console.log("Secret: " + secret);
}
// ************************************************************************************************
if (argv._[0] === "list") {
const keys = await LTIConfModel.find({ version: argv.ltiver });
if (keys.length > 0) {
console.log("The following LTI keys are defined:");
keys.forEach((x) => console.log("- " + x.name + (x.description ? `(${x.description})` : "")));
} else {
console.log("No keys defined.");
}
}
// ************************************************************************************************
if (argv._[0] === "show") {
if (!argv.key) {
console.log("Key is not defined!");
process.exit(1);
}
const key = await LTIConfModel.findOne({ name: argv.key, version: argv.ltiver });
if (key) {
console.log("Key: " + key.name);
if (key.description) {
console.log("Description: " + key.description);
}
console.log("Secret: " + JSON.parse(key.content)["secret"]);
} else {
console.log("Cannot find the key!");
process.exit(1);
}
}
// ************************************************************************************************
if (argv._[0] === "delete") {
if (!argv.key) {
console.log("Key is not defined!");
process.exit(1);
}
const key = await LTIConfModel.findOneAndDelete({ name: argv.key, version: argv.ltiver });
if (key) {
console.log("Key is deleted.");
} else {
console.log("Cannot find the key!");
process.exit(1);
}
}
process.exit(0);
// ************************************************************************************************
} else if (argv.ltiver === 13) {
if (argv._[0] === "initialize") {
if (!argv.name) {
console.log("Name is not defined!");
process.exit(1);
}
const { publicKey, privateKey } = await jose.generateKeyPair("RS256");
const keyId = randomstring.generate();
const conf = await LTI13ConfModel.findOrCreate(
{ name: argv.key },
{
myPublic: await jose.exportSPKI(publicKey),
mySecret: await jose.exportPKCS8(privateKey),
status: "pending",
iss: keyId,
keyId,
loginURL: keyId,
description: argv.desc ?? "",
name: argv.name,
}
);
if (conf.created) {
console.log("New key is created.");
console.log();
console.log(`Use this URL in LMS:`);
console.log(`${process.env.HOSTNAME_URL}/neuvontajono/api/lti/v13/${keyId}/initialize`);
process.exit(0);
} else {
console.log("Key with this name is already created!");
process.exit(1);
}
}
// ************************************************************************************************
if (argv._[0] === "delete") {
if (!argv.name) {
console.log("Key is not defined!");
process.exit(1);
}
const key = await LTI13ConfModel.findOneAndDelete({ name: argv.name });
if (key) {
console.log("Key is deleted.");
} else {
console.log("Cannot find the key!");
process.exit(1);
}
}
// ************************************************************************************************
if (argv._[0] === "list") {
const keys = await LTI13ConfModel.find();
if (keys.length > 0) {
console.log("The following LTI keys are defined:");
keys.forEach((x) => console.log("- " + x.name + (x.description ? `(${x.description})` : "")));
} else {
console.log("No keys defined.");
}
}
// ************************************************************************************************
if (argv._[0] === "show") {
if (!argv.name) {
console.log("Key is not defined!");
process.exit(1);
}
const key = await LTI13ConfModel.findOne({ name: argv.name });
if (key) {
console.log("Key: " + key.name);
if (key.description) {
console.log("Description: " + key.description);
}
if (key.status === "pending") {
console.log("Registration is not completed.");
console.log();
}
if (key.iss) {
console.log("ISS: " + key.iss);
}
if (key.loginURL) {
console.log("Login URL: " + key.loginURL);
}
if (key.clientId) {
console.log("Client id: " + key.clientId);
}
if (key.deploymentId) {
console.log("Deployment id: " + key.deploymentId);
}
console.log();
console.log("My public key:");
console.log(key.myPublic);
console.log();
if (key.otherPublic) {
console.log("Public key for the ISS:");
console.log(key.otherPublic);
}
if (key.jwks) {
console.log("JWKS for the ISS:");
console.log(key.jwks);
}
console.log();
console.log("My JWKS URL:");
console.log(`${process.env.HOSTNAME_URL}/neuvontajono/api/lti/v13/${key.keyId}/key`);
} else {
console.log("Cannot find the key!");
process.exit(1);
}
}
// ************************************************************************************************
if (argv._[0] === "export") {
if (!argv.name) {
console.log("Key is not defined!");
process.exit(1);
}
if (!argv.file) {
console.log("File is not defined!");
process.exit(1);
}
const key = await LTI13ConfModel.findOne({ name: argv.name });
if (key) {
fs.writeFileSync(argv.file, key.myPublic);
console.log("Public key is exported to file.");
} else {
console.log("Cannot find the key!");
process.exit(1);
}
}
// ************************************************************************************************
if (argv._[0] === "import") {
if (!argv.name) {
console.log("Key is not defined!");
process.exit(1);
}
if (!argv.file) {
console.log("File is not defined!");
process.exit(1);
}
const key = await LTI13ConfModel.findOne({ name: argv.name });
if (key) {
try {
const publicKey = fs.readFileSync(argv.file, { encoding: "utf-8" });
jose.importSPKI(publicKey, "RS256");
key.otherPublic = publicKey;
key.jwks = "";
key.status = "completed";
await key.save();
console.log("Public key is imported.");
} catch (e) {
console.log("Importing the public key failed!");
process.exit(1);
}
} else {
console.log("Cannot find the key!");
process.exit(1);
}
}
// ************************************************************************************************
if (argv._[0] === "create") {
if (!argv.name) {
console.log("Name is not defined!");
process.exit(1);
}
if (!argv.iss) {
console.log("ISS is not defined!");
process.exit(1);
}
if (!argv.login) {
console.log("Login URL is not defined!");
process.exit(1);
}
const { publicKey, privateKey } = await jose.generateKeyPair("RS256");
const keyId = randomstring.generate();
const conf = await LTI13ConfModel.findOrCreate(
{ name: argv.key },
{
myPublic: await jose.exportSPKI(publicKey),
mySecret: await jose.exportPKCS8(privateKey),
status: argv.jwks ? "completed" : "pending",
iss: argv.iss,
keyId,
loginURL: argv.login,
description: argv.desc ?? "",
jwks: argv.jwks ?? "",
name: argv.name,
}
);
if (conf.created) {
console.log("New key is created.");
if (!argv.jwks) {
console.log();
console.log("Remember to import the public key for this service.");
}
process.exit(0);
} else {
console.log("Key with this name is already created!");
process.exit(1);
}
}
// ************************************************************************************************
if (argv._[0] === "set") {
if (!argv.name) {
console.log("Key is not defined!");
process.exit(1);
}
const key = await LTI13ConfModel.findOne({ name: argv.name });
if (key) {
if (argv.jwks) {
key.jwks = argv.jwks;
key.status = "completed";
key.otherPublic = "";
}
if (argv.iss) {
key.iss = argv.iss;
}
if (argv.login) {
key.loginURL = argv.login;
}
if (argv.deploymentid) {
key.deploymentId = argv.deploymentid;
}
if (argv.clientid) {
key.clientId = argv.clientid;
}
if (argv.desc) {
key.description = argv.desc;
}
await key.save();
console.log("Key is updated.");
} else {
console.log("Cannot find the key!");
process.exit(1);
}
}
// ************************************************************************************************
process.exit(0);
}
}
tool();