Repository navigation
Expand file tree
/
Copy pathsession.ts
More file actions
76 lines (66 loc) · 2.56 KB
/
Copy pathsession.ts
File metadata and controls
76 lines (66 loc) · 2.56 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { SignJWT, jwtVerify } from "jose";
// This module runs in both the Edge middleware and Node server code, so it
// only uses Web Crypto (via jose) and never touches node:crypto.
export const SESSION_COOKIE_NAME = "rushhq_session";
export type SessionTier = "brother" | "admin";
/**
* Brothers stay signed in for the whole rush season on their own phones.
* Admin sessions are short because an admin cookie can rewrite statuses and
* delete records, so a lost or shared device should not stay privileged long.
*/
export const SESSION_TTL_SECONDS: Record<SessionTier, number> = {
brother: 60 * 60 * 24 * 30,
admin: 60 * 60 * 12
};
const SESSION_ISSUER = "rushhq";
const SESSION_ALGORITHM = "HS256";
const MIN_SECRET_LENGTH = 32;
type SessionOptions = {
/** Defaults to process.env.SESSION_SECRET. */
secret?: string;
/** Override the clock, used by tests. */
now?: Date;
};
function getSessionKey(secret = process.env.SESSION_SECRET) {
if (!secret || secret.length < MIN_SECRET_LENGTH) {
throw new Error(
`SESSION_SECRET must be set to a random string of at least ${MIN_SECRET_LENGTH} characters ` +
"(for example: openssl rand -base64 48)."
);
}
return new TextEncoder().encode(secret);
}
function isSessionTier(value: unknown): value is SessionTier {
return value === "brother" || value === "admin";
}
export async function createSessionToken(tier: SessionTier, options: SessionOptions = {}) {
const key = getSessionKey(options.secret);
const issuedAt = Math.floor((options.now ?? new Date()).getTime() / 1000);
return new SignJWT({ tier })
.setProtectedHeader({ alg: SESSION_ALGORITHM })
.setIssuer(SESSION_ISSUER)
.setIssuedAt(issuedAt)
.setExpirationTime(issuedAt + SESSION_TTL_SECONDS[tier])
.sign(key);
}
/**
* Returns the tier encoded in a valid session token, or null for a missing,
* malformed, tampered, expired, or wrongly signed token. Throws only when the
* server itself is misconfigured (missing or short SESSION_SECRET), so a bad
* deployment fails closed and loudly instead of treating everyone as public.
*/
export async function verifySessionToken(token: string | undefined, options: SessionOptions = {}): Promise<SessionTier | null> {
const key = getSessionKey(options.secret);
if (!token) return null;
try {
const { payload } = await jwtVerify(token, key, {
algorithms: [SESSION_ALGORITHM],
issuer: SESSION_ISSUER,
requiredClaims: ["exp", "iat"],
currentDate: options.now
});
return isSessionTier(payload.tier) ? payload.tier : null;
} catch {
return null;
}
}