-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ts
36 lines (33 loc) · 1.25 KB
/
main.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
import { serve, ServerRequest } from "https://deno.land/[email protected]/http/server.ts";
import { decode } from "https://deno.land/[email protected]/encoding/base64.ts";
// simple basic auth implementation
// we can use CS_USERNAME and CS_PASSWORD which come from Container Spawner
const basicAuthUser = Deno.env.get('CS_USERNAME');
const basicAuthPassword = Deno.env.get('CS_PASSWORD');
const verifyBasicAuth = (req: ServerRequest) => {
const authorization = req.headers.get('authorization');
if (!authorization) {
return false;
}
const payload = authorization.split(' ')[1];
const authText = new TextDecoder().decode(decode(payload));
const [user, password] = authText.split(':');
return (user === basicAuthUser && password === basicAuthPassword);
};
// start app
const PORT = 1993;
const s = serve(`0.0.0.0:${PORT}`);
console.log(`Server started on port ${PORT}`);
for await (const req of s) {
if (verifyBasicAuth(req)) {
// verified!
const body = new TextEncoder().encode("Hello World\n");
req.respond({ body });
} else {
// not verified
const body = new TextEncoder().encode("Not allowed\n");
const headers = new Headers();
headers.set('www-authenticate', 'Basic realm="example"');
req.respond({ status: 401, body, headers });
}
}