This repository has been archived on 2025-11-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
trafficcue-server/src/auth.ts
Jannik 390594bb39
Some checks failed
TrafficCue Server CI / check (push) Has been cancelled
feat: add tests
2025-08-30 10:12:20 +02:00

52 lines
1.3 KiB
TypeScript

import type { JWTPayload } from "hono/utils/jwt/types";
import { decode, verify, type Algorithm } from "jsonwebtoken";
import jwkToPem, { type JWK } from "jwk-to-pem";
const JWKS = process.env.OIDC_JWKS_URL || "";
interface JWKSResponse {
keys: {
kid: string;
kty: string;
use: string;
alg: Algorithm;
n: string;
e: string;
}[];
}
export async function verifyToken(token: string): Promise<boolean> {
const decoded = decode(token, { complete: true });
const jwks = await fetch(JWKS).then(
(res) => res.json() as Promise<JWKSResponse>,
);
if (!decoded || !decoded.header || !decoded.header.kid) {
return false;
}
const key = jwks.keys.find((k) => k.kid === decoded.header.kid);
if (!key) {
return false;
}
const pem = jwkToPem(key as JWK);
try {
const res = verify(token, pem, { algorithms: [key.alg] });
console.log(res);
return typeof res === "object" && "sub" in res;
} catch (_err) {
return false;
}
}
export function getTokenUID(token: string): string | null {
const decoded = decode(token);
if (typeof decoded === "object" && decoded !== null && "sub" in decoded) {
return decoded.sub as string;
}
return null;
}
export function getTokenData(token: string): JWTPayload | null {
return decode(token) as JWTPayload | null;
}