diff --git a/jwt/index.ts b/jwt/index.ts index d0e66a2..0a9d23a 100644 --- a/jwt/index.ts +++ b/jwt/index.ts @@ -3,31 +3,33 @@ import * as jose from "jose"; export type IdentifiersMap = Record; export interface IIdentificator { - verify: (token: string | undefined) => boolean; - verifyAndFetch(token: string | undefined): Promise; + verify: (token: string) => Promise; + verifyAndFetch(token: string): Promise; generateToken: (identifiers: I) => Promise; } export const identificator = ( secret: string, - exp: string | number, + exp: string | number ) => { const secretEncoder = new TextEncoder().encode(secret); const alg = "HS256"; return { - verify: (token: string | undefined) => { - if (token) return !!jose.jwtVerify(token, secretEncoder); + verify: async (token: string) => { + await jose.jwtVerify(token, secretEncoder, { + maxTokenAge: exp, + }); + + return true; }, - verifyAndFetch: async (token: string | undefined) => { - if (token) { - const { payload } = await jose.jwtVerify(token, secretEncoder, { - algorithms: [alg], - }); - - if (typeof payload.ext == "string") { - return JSON.parse(payload.ext) as I; - } + verifyAndFetch: async (token: string) => { + const { payload } = await jose.jwtVerify(token, secretEncoder, { + algorithms: [alg], + }); + + if (typeof payload.ext == "string") { + return JSON.parse(payload.ext) as I; } return null; @@ -36,6 +38,7 @@ export const identificator = ( const token = await new jose.SignJWT({ ext: JSON.stringify(identifiers) }) .setProtectedHeader({ alg }) .setExpirationTime(exp) + .setIssuedAt() .sign(secretEncoder); return token;