diff --git a/package.json b/package.json index e3d6940..2768d16 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ }, "homepage": "https://github.com/fastify/fastify-csrf#readme", "dependencies": { - "@fastify/csrf": "^5.1.0", + "@fastify/csrf": "^6.0.0", "@fastify/error": "^3.0.0", "fastify-plugin": "^4.0.0" }, diff --git a/types/index.d.ts b/types/index.d.ts index bcdf105..75ae069 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,6 +1,8 @@ /// import { FastifyPluginAsync, FastifyRequest } from 'fastify'; +import { Options as CSRFOptions } from "@fastify/csrf"; +import { CookieSerializeOptions as FastifyCookieSerializeOptions } from "@fastify/cookie"; declare module 'fastify' { interface FastifyInstance { @@ -18,24 +20,16 @@ declare module 'fastify' { } } -export interface CookieSerializeOptions { - domain?: string; - encode?(val: string): string; - expires?: Date; - httpOnly?: boolean; - maxAge?: number; - path?: string; - sameSite?: boolean | 'lax' | 'strict' | 'none'; - secure?: boolean; - signed?: boolean; -} +export type CookieSerializeOptions = FastifyCookieSerializeOptions export type GetTokenFn = (req: FastifyRequest) => string | void; export interface FastifyCsrfOptions { + csrfOpts?: CSRFOptions; cookieKey?: string; cookieOpts?: CookieSerializeOptions; sessionKey?: string; + getUserInfo?: (req: FastifyRequest) => string; getToken?: GetTokenFn; sessionPlugin?: '@fastify/cookie' | '@fastify/session' | '@fastify/secure-session'; } diff --git a/types/index.test-d.ts b/types/index.test-d.ts index 8b93aa1..072f3a5 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -1,11 +1,14 @@ import Fastify from 'fastify' import FastifyCookie from '@fastify/cookie' -import FastifyCsrf from '..' +import FastifyCsrfProtection from '..' +import { expectError } from 'tsd' +import FastifySession from '@fastify/session' -async function run () { - const fastify = Fastify() +const fastify = Fastify() + +async function run() { await fastify.register(FastifyCookie) - await fastify.register(FastifyCsrf) + await fastify.register(FastifyCsrfProtection) fastify.route({ method: 'GET', @@ -24,3 +27,14 @@ async function run () { } }) } + + +fastify.register(FastifyCsrfProtection, { csrfOpts: { algorithm: 'sha1' } }) +expectError(fastify.register(FastifyCsrfProtection, { csrfOpts: { algorithm: 1 } })) + +fastify.register(FastifySession) +fastify.register(FastifyCsrfProtection, { getUserInfo(req) { + return req.session.get('username') +}}) +expectError(fastify.register(FastifyCsrfProtection, { getUserInfo: 'invalid' })) +