Skip to content

Commit

Permalink
v1.0.8 chore: update typescript and defination
Browse files Browse the repository at this point in the history
  • Loading branch information
rikkei-huylt committed Jun 8, 2023
1 parent 9f33101 commit b807daf
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ncsrf",
"version": "1.0.7",
"version": "1.0.8",
"description": "Simple NestJS CSRF verify token",
"main": "dist/index.js",
"types": "dist/index.ts",
Expand Down
46 changes: 38 additions & 8 deletions src/common/csrf.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
import Cookie from "cookie";
import { sign } from "cookie-signature";
import Tokens from "csrf";
import { Request, Response } from "express";

type NestCsrfOptions = {
interface CookieConfig {
signed?: boolean;
key?: string;
path?: string;
httpOnly?: boolean;
maxAge?: number;
ttl?: number;
};
}

export interface NestCsrfOptions {
signed?: boolean;
key?: string;
ttl?: number;
}

export interface NestCsrfRequest extends Request {
secret?: string;
cookieConfig?: NestCsrfOptions;
csrfToken?: () => string;
}

const tokenProvider = new Tokens({
secretLength: 16,
Expand All @@ -15,15 +31,15 @@ const tokenProvider = new Tokens({

const nestCsrf = (options?: NestCsrfOptions) => {
const sessionKey = "session";
const cookieConfig = {
const cookieConfig: CookieConfig = {
signed: false,
key: "_csrf",
path: "/",
httpOnly: true,
maxAge: options && options.ttl ? options.ttl : 300,
...options,
};
return function csrf(req, res, next) {
return function csrf(req: NestCsrfRequest, res: Response, next) {
let csrfTokenValue = "";
let secret = getSecretFromRequest(req, sessionKey, cookieConfig);
if (!secret) {
Expand All @@ -40,7 +56,11 @@ const nestCsrf = (options?: NestCsrfOptions) => {
};
};

const getSecretFromRequest = (req, sessionKey, cookie) => {
const getSecretFromRequest = (
req: NestCsrfRequest,
sessionKey: string,
cookie: CookieConfig
) => {
var bag = getSecretBag(req, sessionKey, cookie);
var key = cookie ? cookie.key : "csrfSecret";
if (!bag) {
Expand All @@ -49,7 +69,7 @@ const getSecretFromRequest = (req, sessionKey, cookie) => {
return bag[key];
};

const getCsrfFromRequest = (req) => {
const getCsrfFromRequest = (req: NestCsrfRequest) => {
return (
(req.body && req.body._csrf) ||
(req.query && req.query._csrf) ||
Expand All @@ -72,7 +92,13 @@ const setCookie = (res, name, value, options) => {
res.setHeader("set-cookie", header);
};

const setSecret = (req, res, sessionKey, value, cookie) => {
const setSecret = (
req: NestCsrfRequest,
res,
sessionKey: string,
value: string,
cookie: CookieConfig
) => {
if (cookie) {
if (cookie.signed) {
value = "s:" + sign(value, req.secret);
Expand All @@ -83,7 +109,11 @@ const setSecret = (req, res, sessionKey, value, cookie) => {
}
};

const getSecretBag = (req, sessionKey, cookie) => {
const getSecretBag = (
req: NestCsrfRequest,
sessionKey: string,
cookie: CookieConfig
) => {
if (cookie) {
var cookieKey = cookie.signed ? "signedCookies" : "cookies";
return req[cookieKey];
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"target": "es2017",
"module": "commonjs",
"lib": ["es2017", "es7", "es6"],
"declaration": false,
"declaration": true,
"declarationMap": false,
"sourceMap": false,
"outDir": "./dist",
Expand Down

0 comments on commit b807daf

Please sign in to comment.