-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathjwt.ts
More file actions
81 lines (72 loc) · 1.69 KB
/
jwt.ts
File metadata and controls
81 lines (72 loc) · 1.69 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
77
78
79
80
81
import { createHash, randomBytes, randomInt } from 'node:crypto'
import { env } from './env.js'
type AccessTokenPayload = {
typ: 'access'
sub: string // userId
sid: string // sessionId
wal?: { chain: string; address: string } // session wallet (when JWT created by wallet)
iss: string
aud: string[]
iat: number
exp: number
}
type RefreshTokenPayload = {
typ: 'refresh'
sub: string // userId
sid: string // sessionId
jti: string // refresh token JTI
iss: string
aud: string[]
iat: number
exp: number
}
export function hashToken(token: string): string {
return createHash('sha256').update(token).digest('hex')
}
export function generateToken(): string {
return randomBytes(32).toString('base64url')
}
/** Returns 6-digit code (100000–999999) for magic link login. */
export function generateLoginCode(): string {
return String(randomInt(100000, 1000000))
}
export function generateJti(): string {
return randomBytes(16).toString('base64url')
}
export function createAccessTokenPayload({
userId,
sessionId,
wallet,
}: {
userId: string
sessionId: string
wallet?: { chain: string; address: string }
}): Omit<AccessTokenPayload, 'iat' | 'exp'> {
return {
typ: 'access',
sub: userId,
sid: sessionId,
...(wallet && { wal: wallet }),
iss: env.JWT_ISSUER,
aud: env.JWT_AUDIENCE,
}
}
export function createRefreshTokenPayload({
userId,
sessionId,
jti,
}: {
userId: string
sessionId: string
jti: string
}): Omit<RefreshTokenPayload, 'iat' | 'exp'> {
return {
typ: 'refresh',
sub: userId,
sid: sessionId,
jti,
iss: env.JWT_ISSUER,
aud: env.JWT_AUDIENCE,
}
}
export type { AccessTokenPayload, RefreshTokenPayload }