( token: string, secret: string, now = nowSec(), )
| 25 | } |
| 26 | |
| 27 | export function verifyAccessToken( |
| 28 | token: string, |
| 29 | secret: string, |
| 30 | now = nowSec(), |
| 31 | ): AccessClaims | null { |
| 32 | const parts = token.split("."); |
| 33 | if (parts.length !== 3) return null; |
| 34 | const [h, b, s] = parts as [string, string, string]; |
| 35 | const expected = b64(sig(`${h}.${b}`, secret)); |
| 36 | const got = Buffer.from(s); |
| 37 | const exp = Buffer.from(expected); |
| 38 | if (got.length !== exp.length || !timingSafeEqual(got, exp)) return null; |
| 39 | let claims: AccessClaims; |
| 40 | try { |
| 41 | claims = JSON.parse(Buffer.from(b, "base64url").toString("utf8")); |
| 42 | } catch { |
| 43 | return null; |
| 44 | } |
| 45 | if (typeof claims.exp !== "number" || claims.exp < now || typeof claims.sub !== "string") { |
| 46 | return null; |
| 47 | } |
| 48 | return claims; |
| 49 | } |
| 50 | |
| 51 | /** Opaque refresh token; only its hash is persisted. */ |
| 52 | export function generateRefreshToken(): { token: string; tokenHash: string } { |
no test coverage detected