Skip to content

Commit

Permalink
major: Update csrf and fix some typings (#114)
Browse files Browse the repository at this point in the history
* move types into types folder
use FastifyPluginAsync instead of FastifyPlugin as type

* move example to examples folder

* use .taprc

* add pre-commit
split lint from test

* update @fastify/csrf
update typings

* add getUserInfo typings

* Update index.d.ts

According to the docs getUserInfo returns a string
  • Loading branch information
Uzlopak authored Sep 24, 2022
1 parent 286493f commit 33984cf
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
16 changes: 5 additions & 11 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/// <reference types="node" />

import { FastifyPluginAsync, FastifyRequest } from 'fastify';
import { Options as CSRFOptions } from "@fastify/csrf";
import { CookieSerializeOptions as FastifyCookieSerializeOptions } from "@fastify/cookie";

declare module 'fastify' {
interface FastifyInstance {
Expand All @@ -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';
}
Expand Down
22 changes: 18 additions & 4 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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' }))

0 comments on commit 33984cf

Please sign in to comment.