Skip to content

Commit

Permalink
types: add key option to sign and verify (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
KirillTregubov authored Nov 11, 2023
1 parent 87d5046 commit bd9f9bb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 2 additions & 0 deletions types/jwt.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,13 @@ declare namespace fastifyJwt {
export interface SignOptions extends Omit<SignerOptions, "expiresIn" | "notBefore"> {
expiresIn: number | string;
notBefore: number | string;
key?: string | Buffer
}

export interface VerifyOptions extends Omit<VerifierOptions, "maxAge"> {
maxAge: number | string;
onlyCookie: boolean;
key?: string | Buffer
}

export interface FastifyJWTOptions {
Expand Down
38 changes: 37 additions & 1 deletion types/jwt.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fastify from 'fastify';
import fastifyJwt, { FastifyJWTOptions, FastifyJwtNamespace, JWT } from '..'
import fastifyJwt, { FastifyJWTOptions, FastifyJwtNamespace, JWT, SignOptions, VerifyOptions } from '..'
import { expectAssignable, expectType } from 'tsd'

const app = fastify();
Expand Down Expand Up @@ -166,3 +166,39 @@ expectType<JWT['verify']>(({} as FastifyJwtNamespace<{ namespace: 'security', jw
expectType<JWT['decode']>(({} as FastifyJwtNamespace<{ jwtDecode: 'decode'}>).decode)
expectType<JWT['sign']>(({} as FastifyJwtNamespace<{ jwtSign: 'sign'}>).sign)
expectType<JWT['verify']>(({} as FastifyJwtNamespace<{ jwtVerify: 'verify'}>).verify)

let signOptions: SignOptions = {
key: "supersecret",
algorithm: "HS256",
mutatePayload: true,
expiresIn: 3600,
notBefore: 0,
}

signOptions = {
key: Buffer.from("supersecret", "utf-8"),
algorithm: "HS256",
mutatePayload: true,
expiresIn: 3600,
notBefore: 0,
}

let verifyOptions: VerifyOptions = {
key: "supersecret",
algorithms: ["HS256"],
complete: true,
cache: true,
cacheTTL: 3600,
maxAge: "1 hour",
onlyCookie: false,
}

verifyOptions = {
key: Buffer.from("supersecret", "utf-8"),
algorithms: ["HS256"],
complete: true,
cache: 3600,
cacheTTL: 3600,
maxAge: 3600,
onlyCookie: true,
}

0 comments on commit bd9f9bb

Please sign in to comment.