-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
55 lines (44 loc) · 1.59 KB
/
main.ts
File metadata and controls
55 lines (44 loc) · 1.59 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
import { Application, Router } from "./deps.ts";
import { AuthService } from "./auth/auth.ts";
// Oak server + middleware
const app = new Application();
const router = new Router();
const selfTester = new Router();
// Services
const auth = new AuthService();
router
.get("/oauth/callback", async ctx => {
const params = ctx.request.url.searchParams;
const code = params.get("code") ?? "";
const state = params.get("state") ?? "";
if (!code) ctx.throw(400);
// Sanitize state to get app namespace (just in case)
const app = state.replace(/[^a-zA-Z]/g, "");
const base = Deno.env.get("REDIRECT_BASE") ?? "";
const redir = app ? `${app}.${base}` : base;
const protocol = Deno.env.get("REDIRECT_PROTOCOL") ?? "http";
const jwt = await auth.authorize(code, state);
ctx.response.redirect(`${protocol}://${redir}/auth?jwt=${jwt}`);
})
.get("/oauth/login", ctx => {
const params = ctx.request.url.searchParams;
ctx.response.redirect(auth.getAuthURL(params.get("app") ?? ""));
})
.get("/oauth/manage", ctx => {
ctx.response.redirect(auth.getManagementURL());
});
selfTester
.get("/auth", async ctx => {
const params = ctx.request.url.searchParams;
const jwt = params.get("jwt") ?? "";
const uuid = await auth.getAuthorization(jwt);
ctx.response.body = `JWT: ${jwt}\nVerified As: ${uuid}`;
});
app.use(router.allowedMethods());
app.use(router.routes());
app.use(selfTester.allowedMethods());
app.use(selfTester.routes());
const port = parseInt(Deno.env.get("PORT")??"8999");
app.listen({ port: port });
console.log(`Port: ${port}`);
console.log(`http://localhost:${port}/oauth/login`);