-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwtUtils.ts
34 lines (30 loc) · 887 Bytes
/
jwtUtils.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
import * as jose from "jose";
export class JwtUtils {
private secret: Uint8Array;
private issuer: string;
private audience: string;
constructor(audience: string) {
this.secret = new TextEncoder().encode(
Bun.env["JWT_SECRET"] || "JWT Secret"
);
this.issuer =
"https://showdown.space/events/browser-automation-challenges/";
this.audience = audience;
}
async sign(payload: jose.JWTPayload) {
return new jose.SignJWT(payload)
.setProtectedHeader({ alg: "HS256" })
.setExpirationTime("12h")
.setIssuer(this.issuer)
.setAudience(this.audience)
.sign(this.secret);
}
async verify(token: string): Promise<jose.JWTPayload> {
const { payload } = await jose.jwtVerify(token, this.secret, {
algorithms: ["HS256"],
issuer: this.issuer,
audience: this.audience,
});
return payload;
}
}