-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebserver.ts
123 lines (108 loc) · 3.42 KB
/
webserver.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
import { Application, Router } from "https://deno.land/x/oak@v7.6.3/mod.ts";
import { verify, create } from "https://deno.land/x/djwt@v2.2/mod.ts";
import * as s from "https://deno.land/x/scrypt@v2.0.0/mod.ts";
import { UserDB, LoginForm, RegisterForm } from "./types.ts";
const secret = "nsatoienarstunsarietnseitnarsientesrntoin";
//using in memory db
const userDB: UserDB = {};
//example user
(async () => {
userDB.upwork = {
name: "test",
email: "test@test.com",
pos: "Developer",
password: await s.hash("upwork"),
devices: [],
};
})();
const authRouter = new Router({ prefix: "/api" })
.post("/token_refresh", async (ctx) => {
const refreshToken = ctx.cookies.get("rt")!;
const { iss } = await verify(refreshToken, secret, "HS512");
//check if refresh token is in the device list
if (
!userDB[iss!].devices.some(async (e) => await s.verify(refreshToken, e))
)
throw "Invalid Refresh Token";
const acessToken = await newJwt(iss!, 5);
ctx.response.body = `Bearer ${acessToken}`;
})
.post("/login", async (ctx) => {
const { name, password }: LoginForm = await ctx.request.body().value;
if (!(await s.verify(password, userDB[name].password)))
throw "bad password";
const refreshToken = await newJwt(name, 60 * 24);
const acessToken = await newJwt(name, 5);
ctx.cookies.set("rt", refreshToken, { sameSite: "lax" });
userDB[name].devices.push(await s.hash(refreshToken));
ctx.response.body = `Bearer ${acessToken}`;
})
.post("/logout", async (ctx) => {
const refreshToken = ctx.cookies.get("rt")!;
const { iss } = await verify(refreshToken, secret, "HS512");
//remove refresh token from devices
userDB[iss!].devices = userDB[iss!].devices.filter(
async (e) => !(await s.verify(refreshToken, e))
);
ctx.cookies.delete("rt");
ctx.response.body = "OK";
})
.post("/register", async (ctx) => {
const user: RegisterForm = await ctx.request.body().value;
if (userDB[user.name]) throw `${user.name} already exists`;
userDB[user.name] = {
...user,
password: await s.hash(user.password),
devices: [],
};
ctx.response.body = "OK";
});
const privRouter = new Router({ prefix: "/api" }).get(
"/hashedPass",
async (ctx) => {
const acessToken = ctx.request.headers
.get("Authorization")!
.replace("Bearer ", "");
const { iss } = await verify(acessToken, secret, "HS512");
ctx.response.body = JSON.stringify(userDB[iss!]);
}
);
//middle ware (non associative)
const app = new Application()
//error handling
.use(async (ctx, next) => {
try {
console.log(ctx.request.headers);
await next();
} catch (e) {
console.error(e);
ctx.response.status = 401;
ctx.response.body = JSON.stringify(e);
}
})
//routes
.use(privRouter.routes())
.use(authRouter.routes())
//static files
.use(async (ctx) => {
if (
[".js", ".css", ".json", ".ico", ".svg"].some((extension) =>
ctx.request.url.pathname.endsWith(extension)
)
) {
await ctx.send({ root: `${Deno.cwd()}/client/build` });
} else {
await ctx.send({
root: `${Deno.cwd()}/client/build`,
path: "index.html",
});
}
});
await app.listen({ port: 8000 });
function newJwt(name: string, minutes: number) {
return create(
{ alg: "HS512", typ: "JWT" },
{ iss: name, exp: Date.now() + minutes * 60000 },
secret
);
}