mirror of
https://github.com/hanzoai/passport-jwt.git
synced 2026-09-24 17:01:40 +00:00
40 lines
1.5 KiB
TypeScript
40 lines
1.5 KiB
TypeScript
import {DefaultPayload, JwtDriver, JwtResult, JwtResultInternal} from "./base";
|
|
import type {VerifyOptions, jwtVerify, KeyLike} from "jose";
|
|
import {createSecretKey} from "crypto";
|
|
import {ErrorMessages} from "../error_messages";
|
|
|
|
type JoseDriverCore = { jwtVerify: typeof jwtVerify }
|
|
|
|
export class JoseDriver extends JwtDriver<JoseDriverCore, VerifyOptions, KeyLike | string> {
|
|
|
|
protected defaultOptions: VerifyOptions = {algorithms: ["HS256"]};
|
|
|
|
constructor(
|
|
core: JoseDriverCore,
|
|
options?: VerifyOptions
|
|
) {
|
|
if (typeof core !== "object" || !("jwtVerify" in core) || typeof core["jwtVerify"] !== "function") {
|
|
throw new TypeError(ErrorMessages.JOSE_CORE_INCOMPATIBLE);
|
|
}
|
|
super(core, options);
|
|
}
|
|
|
|
public async validate<Payload extends DefaultPayload>(token: string, keyOrSecret: string | KeyLike): Promise<JwtResult<Payload>> {
|
|
let jwk: KeyLike | undefined = undefined;
|
|
if (typeof keyOrSecret === "string") {
|
|
jwk = createSecretKey(Buffer.from(keyOrSecret));
|
|
} else {
|
|
jwk = keyOrSecret;
|
|
}
|
|
const result: JwtResultInternal = {success: false, message: undefined};
|
|
try {
|
|
const validation = await this.core.jwtVerify(token, jwk, this.getOptions());
|
|
result.success = true;
|
|
result.payload = validation.payload;
|
|
} catch (err: any) {
|
|
result.message = err?.message;
|
|
}
|
|
return result as JwtResult<Payload>;
|
|
}
|
|
|
|
} |