Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types: add key option to sign and verify #314

Merged
merged 3 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
}