style: add eslint and prettier
This commit is contained in:
22
src/auth.ts
22
src/auth.ts
@ -3,19 +3,27 @@ import jwkToPem, { type JWK } from "jwk-to-pem";
|
||||
|
||||
const JWKS = process.env.OIDC_JWKS_URL || "";
|
||||
|
||||
type JWKSResponse = {
|
||||
keys: Array<{ kid: string; kty: string; use: string; alg: Algorithm; n: string; e: string }>;
|
||||
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>);
|
||||
|
||||
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);
|
||||
const key = jwks.keys.find((k) => k.kid === decoded.header.kid);
|
||||
if (!key) {
|
||||
return false;
|
||||
}
|
||||
@ -24,7 +32,7 @@ export async function verifyToken(token: string): Promise<boolean> {
|
||||
const res = verify(token, pem, { algorithms: [key.alg] });
|
||||
console.log(res);
|
||||
return typeof res === "object" && "sub" in res;
|
||||
} catch (err) {
|
||||
} catch (_err) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user