-
Notifications
You must be signed in to change notification settings - Fork 9
/
adapter.ts
339 lines (299 loc) · 11.1 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
import { STATUS_CODE } from "jsr:@std/http/status";
import { create, getNumericDate } from "jsr:@emrahcom/jwt";
import type { Algorithm } from "jsr:@emrahcom/jwt/algorithm";
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,
} 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>,
sub: string,
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: 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") || "";
const room = path.split("/").reverse()[0];
const tenant = path.split("/").reverse()[1]?.toLowerCase();
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();
// generate JWT
const jwt = await generateJWT(userInfo, tenant || host, room);
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 sanitizedPath = path.replace(/\/+/g, "/");
if (!sanitizedPath.match("^/")) throw ("invalid path");
const bundle = `path=${encodeURIComponent(sanitizedPath)}` +
`&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 sanitized: ${sanitizedPath}`);
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 {
try {
return oidcRedirectForCode(req, "none");
} catch {
return unauthorized();
}
}
// -----------------------------------------------------------------------------
// Redirect to Keycloak auth service to get a short-term authorization code.
// Ask for a credential if auth fails
// -----------------------------------------------------------------------------
function auth(req: Request): Response {
try {
return oidcRedirectForCode(req, "login");
} catch {
return unauthorized();
}
}
// -----------------------------------------------------------------------------
// 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/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}`);
Deno.serve({
hostname: HOSTNAME,
port: PORT,
}, handler);
}
// -----------------------------------------------------------------------------
main();