forked from nordeck/jitsi-keycloak-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter.ts
480 lines (412 loc) · 15 KB
/
adapter.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
import * as fs from 'node:fs';
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import { STATUS_CODE } from "https://deno.land/[email protected]/http/status.ts";
import { create, getNumericDate } from "https://deno.land/x/[email protected]/mod.ts";
import { Algorithm } from "https://deno.land/x/[email protected]/algorithm.ts";
import {
DEBUG,
HOSTNAME,
JWT_ALG,
JWT_APP_ID,
JWT_APP_SECRET,
JWT_EXP_SECOND,
JWT_HASH,
KEYCLOAK_CLIENT_ID,
KEYCLOAK_MODE,
KEYCLOAK_ORIGIN,
KEYCLOAK_ORIGIN_INTERNAL,
KEYCLOAK_REALM,
PORT,
PERMISSIONS_FILE
} from "./config.ts";
import { createContext } from "./context.ts";
// -----------------------------------------------------------------------------
// HTTP response for OK
// -----------------------------------------------------------------------------
function ok(body: string): Response {
return new Response(body, {
status: STATUS_CODE.OK,
});
}
// -----------------------------------------------------------------------------
// HTTP response for NotFound
// -----------------------------------------------------------------------------
function notFound(): Response {
return new Response(null, {
status: STATUS_CODE.NotFound,
});
}
// -----------------------------------------------------------------------------
// HTTP response for MethodNotAllowed
// -----------------------------------------------------------------------------
function methodNotAllowed(): Response {
return new Response(null, {
status: STATUS_CODE.MethodNotAllowed,
});
}
// -----------------------------------------------------------------------------
// HTTP response for Unauthorized
// -----------------------------------------------------------------------------
function unauthorized(): Response {
return new Response(null, {
status: STATUS_CODE.Unauthorized,
});
}
// -----------------------------------------------------------------------------
// Generate JWT (Jitsi token)
// -----------------------------------------------------------------------------
async function generateJWT(
userInfo: Record<string, unknown>,
room: string
): Promise<string | undefined> {
try {
const encoder = new TextEncoder();
const keyData = encoder.encode(JWT_APP_SECRET);
const cryptoKey = await crypto.subtle.importKey(
"raw",
keyData,
{
name: "HMAC",
hash: JWT_HASH,
},
true,
["sign", "verify"],
);
const alg = JWT_ALG as Algorithm;
const header = { alg: alg, typ: "JWT" };
const payload = {
aud: JWT_APP_ID,
iss: JWT_APP_ID,
sub: "*",
room: room,
iat: getNumericDate(0),
nbf: getNumericDate(0),
exp: getNumericDate(JWT_EXP_SECOND),
context: createContext(userInfo),
};
return await create(header, payload, cryptoKey);
} catch {
return undefined;
}
}
// -----------------------------------------------------------------------------
// Get the access token from Keycloak by using the short-term auth code
//
// redirect_uri should be the same with URI which is set while getting the
// short-term authorization code but actually there is no redirection at this
// stage. This is for security check only.
// -----------------------------------------------------------------------------
async function getToken(
host: string,
code: string,
path: string,
search: string,
hash: string,
): Promise<string | undefined> {
const url = `${KEYCLOAK_ORIGIN_INTERNAL}/realms/${KEYCLOAK_REALM}` +
`/protocol/openid-connect/token`;
const bundle = `path=${encodeURIComponent(path)}` +
`&search=${encodeURIComponent(search)}` +
`&hash=${encodeURIComponent(hash)}`;
const redirectURI = `https://${host}/static/oidc-adapter.html` +
`?${bundle}`;
const data = new URLSearchParams();
data.append("client_id", KEYCLOAK_CLIENT_ID);
data.append("grant_type", "authorization_code");
data.append("redirect_uri", redirectURI);
data.append("code", code);
if (DEBUG) console.log(`getToken url: ${url}`);
if (DEBUG) console.log(`getToken redirectURI: ${redirectURI}`);
if (DEBUG) console.log(`getToken data:`);
if (DEBUG) console.log(data);
try {
const res = await fetch(url, {
headers: {
"Accept": "application/json",
},
method: "POST",
body: data,
});
const json = await res.json();
const token = json.access_token;
if (DEBUG) console.log(`getToken json:`);
if (DEBUG) console.log(json);
if (!token) throw ("cannot get Keycloak token");
return token;
} catch {
return undefined;
}
}
// -----------------------------------------------------------------------------
// Get the user info from Keycloak by using the access token
// -----------------------------------------------------------------------------
async function getUserInfo(
token: string,
): Promise<Record<string, unknown> | undefined> {
try {
const url = `${KEYCLOAK_ORIGIN_INTERNAL}/realms/${KEYCLOAK_REALM}` +
`/protocol/openid-connect/userinfo`;
const res = await fetch(url, {
headers: {
"Accept": "application/json",
"Authorization": `Bearer ${token}`,
},
method: "GET",
});
const userInfo = await res.json();
if (DEBUG) console.log(`getUserInfo userInfo:`);
if (DEBUG) console.log(userInfo);
// sub is the mandotary field for successful request
if (!userInfo.sub) throw ("no user info");
return await userInfo;
} catch {
return undefined;
}
}
// -----------------------------------------------------------------------------
// Send the Jitsi token if auth is OK
// -----------------------------------------------------------------------------
async function tokenize(req: Request): Promise<Response> {
const host = req.headers.get("host");
const url = new URL(req.url);
const qs = new URLSearchParams(url.search);
const code = qs.get("code");
const path = qs.get("path") || "";
const search = qs.get("search") || "";
const hash = qs.get("hash") || "";
if (DEBUG) console.log(`tokenize code: ${code}`);
// host is needed for redirection. If no host, this is not a valid request.
if (!host) return unauthorized();
// only the currently logged in user has a short-term auth code
if (!code) return unauthorized();
// get the access token from Keycloak if the short-term auth code is valid
const token = await getToken(host, code, path, search, hash);
if (!token) {
if (DEBUG) console.log(`Could not get Keycloak's access token`);
return unauthorized();
}
// get the user info from Keycloak by using the access token
const userInfo = await getUserInfo(token);
if (!userInfo) return unauthorized();
// Enhance userinfo
userInfo["lobby_bypass"] = true;
userInfo["security_bypass"] = true;
userInfo["affiliation"] = "owner";
let tokenRoom = "*"
// Loading permissions
if (PERMISSIONS_FILE) {
if (fs.existsSync(PERMISSIONS_FILE)) {
let permissions: any = [];
const rawData = fs.readFileSync(PERMISSIONS_FILE, 'utf-8');
permissions = JSON.parse(rawData);
// now you can use the 'permissions' object
console.log(`Loaded ${permissions.length} permissions from ${PERMISSIONS_FILE}`)
let roomName = path.slice(1);
let room = permissions.find(r => r.room === roomName);
if (room) {
// Check for lobby settings
const lobby = room.useLobby
const userName = userInfo["email"]
console.log(`Found config for room ${roomName}. Checking for user ${userName}`)
tokenRoom = roomName;
// check if the user is in the moderator list
if (room.moderators.includes(userName)) {
console.log(`${userName} is a moderator of ${roomName}`);
// make use owner
userInfo["affiliation"] = "owner";
}
else {
console.log(`${userName} is not a moderator of ${roomName}`);
// reduce permissions
userInfo["affiliation"] = "member";
if (lobby) {
userInfo["lobby_bypass"] = false;
}
}
} else {
console.log(`room ${roomName} not found in permissions.`);
}
} else {
console.error(`File not found: ${PERMISSIONS_FILE} - No permissions loaded.`);
}
}
// generate JWT
const jwt = await generateJWT(userInfo, tokenRoom);
if (DEBUG) console.log(`tokenize token: ${jwt}`);
return new Response(JSON.stringify(jwt), {
status: STATUS_CODE.OK,
});
}
// -----------------------------------------------------------------------------
// Redirect to Keycloak auth service to get a short-term authorization code.
//
// If successful, Keycloak will redirect the request to oidc-adapter.html
// (redirect_uri) with a short-term authorization code.
// -----------------------------------------------------------------------------
function oidcRedirectForCode(req: Request, prompt: string): Response {
const host = req.headers.get("host");
const url = new URL(req.url);
const qs = new URLSearchParams(url.search);
const path = qs.get("path");
const search = qs.get("search") || "";
const hash = qs.get("hash") || "";
if (!host) throw ("missing host");
if (!path) throw ("missing path");
const bundle = `path=${encodeURIComponent(path)}` +
`&search=${encodeURIComponent(search)}` +
`&hash=${encodeURIComponent(hash)}`;
const target = `${KEYCLOAK_ORIGIN}/realms/${KEYCLOAK_REALM}` +
`/protocol/openid-connect/auth?client_id=${KEYCLOAK_CLIENT_ID}` +
`&response_mode=${KEYCLOAK_MODE}&response_type=code&scope=openid` +
`&prompt=${prompt}&redirect_uri=https://${host}/static/oidc-adapter.html` +
`?${encodeURIComponent(bundle)}`;
if (DEBUG) console.log(`oidcRedirectForCode prompt: ${prompt}`);
if (DEBUG) console.log(`oidcRedirectForCode host: ${host}`);
if (DEBUG) console.log(`oidcRedirectForCode path: ${path}`);
if (DEBUG) console.log(`oidcRedirectForCode search: ${search}`);
if (DEBUG) console.log(`oidcRedirectForCode hash: ${hash}`);
if (DEBUG) console.log(`oidcRedirectForCode bundle: ${bundle}`);
if (DEBUG) console.log(`oidcRedirectForCode target: ${target}`);
return Response.redirect(target, STATUS_CODE.Found);
}
// -----------------------------------------------------------------------------
// Redirect to Keycloak auth service to get a short-term authorization code.
// Don't ask for a credential if auth fails
// -----------------------------------------------------------------------------
function redirect(req: Request): Response {
return oidcRedirectForCode(req, "none");
}
// -----------------------------------------------------------------------------
// Redirect to Keycloak auth service to get a short-term authorization code.
// Ask for a credential if auth fails
// -----------------------------------------------------------------------------
function auth(req: Request): Response {
return oidcRedirectForCode(req, "login");
}
function generateGUID() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}
return `${s4()}${s4()}-${s4()}-${s4()}-${s4()}-${s4()}${s4()}${s4()}`;
}
async function yolo_auth(req: Request): Response {
// Don't go to keycloak if the room name is prefixed yolo_ and just create a usable JWT
const host = req.headers.get("host");
const url = new URL(req.url);
const qs = new URLSearchParams(url.search);
const path = qs.get("path");
const search = qs.get("search") || "";
const hash = qs.get("hash") || "";
console.log("Got a yolo requerst for " + path)
// check if it's really a yolo
if (!path.startsWith("yolo_")) {
console.log("Not yolo. aborting.")
return new Response("no-yolo", {
status: STATUS_CODE.FORBIDDEN,
});
}
// Generate JWT
const userInfo = {
"sub": generateGUID(),
"preferred_username": "Fellow Jitster",
"email": "[email protected]",
"lobby_bypass": true,
"security_bypass": true,
"affiliation": "owner"
}
let roomName = path;
const jwt = await generateJWT(userInfo, roomName);
if (DEBUG) console.log(`tokenize token: ${jwt}`);
const redirectUrl = '/' + path + '?oidc=authenticated&jwt=' + jwt;
// Create a Response object with a 302 redirect status
return new Response(null, {
status: 302,
headers: {
'Location': redirectUrl
}
});
}
// bypass auth function for monitoring
async function monitoring_auth(req: Request): Promise<Response> {
const url = new URL(req.url);
const qs = new URLSearchParams(url.search);
const path = qs.get("path");
console.log("Got a monitoring request for " + path);
// check it's amonitoring request
if (!path?.startsWith("jitsimonitoring_aml0c2lt")) {
console.log("Not a monitoring request. Aborting.")
return new Response("not-monitoring", {
status: STATUS_CODE.FORBIDDEN,
});
}
// Generate JWT with monitoring userInfo
const userInfo = {
"sub": "monitoring-" + generateGUID(),
"preferred_username": "Jitsi Monitor",
"email": "[email protected]",
"lobby_bypass": true,
"security_bypass": true,
"affiliation": "owner"
}
const jwt = await generateJWT(userInfo, path);
if (DEBUG) console.log(`monitoring token: ${jwt}`);
const redirectUrl = '/' + path + '?oidc=authenticated&jwt=' + jwt;
return new Response(null, {
status: 302,
headers: {
'Location': redirectUrl
}
});
}
// -----------------------------------------------------------------------------
// handler
// -----------------------------------------------------------------------------
async function handler(req: Request): Promise<Response> {
const url = new URL(req.url);
const path = url.pathname;
if (req.method !== "GET") return methodNotAllowed();
if (path === "/health") {
return ok("healthy");
} else if (path === "/oidc/health") {
return ok("healthy");
} else if (path === "/oidc/yolo") {
return await yolo_auth(req);
} else if (path === "/oidc/monitoring") {
return await monitoring_auth(req);
} else if (path === "/oidc/redirect") {
return redirect(req);
} else if (path === "/oidc/tokenize") {
return await tokenize(req);
} else if (path === "/oidc/auth") {
return await auth(req);
} else {
return notFound();
}
}
// -----------------------------------------------------------------------------
// main
// -----------------------------------------------------------------------------
function main() {
console.log(`KEYCLOAK_ORIGIN: ${KEYCLOAK_ORIGIN}`);
console.log(`KEYCLOAK_ORIGIN_INTERNAL: ${KEYCLOAK_ORIGIN_INTERNAL}`);
console.log(`KEYCLOAK_REALM: ${KEYCLOAK_REALM}`);
console.log(`KEYCLOAK_CLIENT_ID: ${KEYCLOAK_CLIENT_ID}`);
console.log(`KEYCLOAK_MODE: ${KEYCLOAK_MODE}`);
console.log(`JWT_ALG: ${JWT_ALG}`);
console.log(`JWT_HASH: ${JWT_HASH}`);
console.log(`JWT_APP_ID: ${JWT_APP_ID}`);
console.log(`JWT_APP_SECRET: *** masked ***`);
console.log(`JWT_EXP_SECOND: ${JWT_EXP_SECOND}`);
console.log(`HOSTNAME: ${HOSTNAME}`);
console.log(`PORT: ${PORT}`);
console.log(`DEBUG: ${DEBUG}`);
if (PERMISSIONS_FILE) {
console.log(`PERMISSIONS_FILE: ${PERMISSIONS_FILE}`);
}
serve(handler, {
hostname: HOSTNAME,
port: PORT
});
}
// -----------------------------------------------------------------------------
main();