Skip to content

Commit

Permalink
refactor!: swap generateToken request and response parameter order
Browse files Browse the repository at this point in the history
BREAKING CHANGE: generateToken(req, res, overwrite)
  • Loading branch information
psibean committed Aug 18, 2023
1 parent f108720 commit 54f6c06
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ const {

```js
const myRoute = (request, response) => {
const csrfToken = generateToken(response, request);
const csrfToken = generateToken(request, response);
// You could also pass the token into the context of a HTML response.
res.json({ csrfToken });
};
Expand All @@ -125,7 +125,7 @@ const myProtectedRoute = (req, res) =>
<p>Instead of importing and using <code>generateToken</code>, you can also use <code>req.csrfToken</code> any time after the <code>doubleCsrfProtection</code> middleware has executed on your incoming request.</p>

```js
request.csrfToken(); // same as generateToken(res, req);
request.csrfToken(); // same as generateToken(req, res);
```

<p>
Expand Down Expand Up @@ -324,20 +324,20 @@ number;
<h3>generateToken</h3>

```ts
(response: Response, request: Request, overwrite?: boolean) => string;
(request: Request, response: Response, overwrite?: boolean) => string;
```

<p>By default if a csrf-csrf cookie already exists on an incoming request, generateToken will not overwrite it, it will simply return the existing token. If you wish to force a token generation, you can use the third parameter:</p>

```ts
generateToken(res, req, true); // This will force a new token to be generated, and a new cookie to be set, even if one already exists
generateToken(req, res, true); // This will force a new token to be generated, and a new cookie to be set, even if one already exists
```

<p>Instead of importing and using generateToken, you can also use req.csrfToken any time after the doubleCsrfProtection middleware has executed on your incoming request.</p>

```ts
req.csrfToken(); // same as generateToken(res, req) and generateToken(res, req, false);
req.csrfToken(true); // same as generateToken(res, req, true);
req.csrfToken(); // same as generateToken(req, res) and generateToken(req, res, false);
req.csrfToken(true); // same as generateToken(req, res, true);
```

<p>The <code>generateToken</code> function serves the purpose of establishing a CSRF (Cross-Site Request Forgery) protection mechanism by generating a token and an associated cookie. This function also provides the option to utilize a third parameter called <code>overwrite</code>. By default, this parameter is set to <em>false</em>.</p>
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ export type CsrfCookieSetter = (
options: DoubleCsrfCookieOptions
) => void;
export type CsrfTokenCreator = (
res: Response,
req: Request,
res: Response,
ovewrite?: boolean
) => string;

Expand Down Expand Up @@ -131,8 +131,8 @@ export function doubleCsrf({
// Do NOT send the csrfToken as a cookie, embed it in your HTML response, or as JSON.

const generateToken: CsrfTokenCreator = (
res: Response,
req: Request,
res: Response,
overwrite?: boolean
) => {
const { csrfToken, csrfTokenHash } = generateTokenAndHash(req, overwrite);
Expand Down Expand Up @@ -180,7 +180,7 @@ export function doubleCsrf({
};

const doubleCsrfProtection: doubleCsrfProtection = (req, res, next) => {
req.csrfToken = (overwrite?: boolean) => generateToken(res, req, overwrite);
req.csrfToken = (overwrite?: boolean) => generateToken(req, res, overwrite);
if (ignoredMethodsSet.has(req.method as RequestMethod)) {
next();
} else if (validateRequest(req)) {
Expand Down
8 changes: 4 additions & 4 deletions src/tests/testsuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export const createTestSuite: CreateTestsuite = (name, doubleCsrfOptions) => {
mockResponse.setHeader("set-cookie", []);

// overwrite is false by default
const generatedToken = generateToken(mockResponse, mockRequest);
const generatedToken = generateToken(mockRequest, mockResponse);
const newCookieValue = getCookieFromResponse(mockResponse);

assert.equal(generatedToken, csrfToken);
Expand All @@ -105,7 +105,7 @@ export const createTestSuite: CreateTestsuite = (name, doubleCsrfOptions) => {
// reset the mock response to have no cookies (in reality this would just be a new instance of Response)
mockResponse.setHeader("set-cookie", []);

const generatedToken = generateToken(mockResponse, mockRequest, true);
const generatedToken = generateToken(mockRequest, mockResponse, true);
const newCookieValue = getCookieFromResponse(mockResponse);

assert.notEqual(newCookieValue, oldCookieValue);
Expand All @@ -124,7 +124,7 @@ export const createTestSuite: CreateTestsuite = (name, doubleCsrfOptions) => {
: (mockRequest.cookies[cookieName] =
(decodedCookieValue as string).split("|")[0] + "|invalid-hash");

expect(() => generateToken(mockResponse, mockRequest)).to.throw(
expect(() => generateToken(mockRequest, mockResponse)).to.throw(
invalidCsrfTokenError.message
);

Expand All @@ -136,7 +136,7 @@ export const createTestSuite: CreateTestsuite = (name, doubleCsrfOptions) => {
)}`)
: (mockRequest.cookies[cookieName] = "invalid-value");

expect(() => generateToken(mockResponse, mockRequest)).to.throw(
expect(() => generateToken(mockRequest, mockResponse)).to.throw(
invalidCsrfTokenError.message
);
});
Expand Down
2 changes: 1 addition & 1 deletion src/tests/utils/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const generateMocksWithToken = ({
}: GenerateMocksWithTokenOptions) => {
const { mockRequest, mockResponse, mockResponseHeaders } = generateMocks();

const csrfToken = generateToken(mockResponse, mockRequest);
const csrfToken = generateToken(mockRequest, mockResponse);
const { setCookie, cookieValue } = getCookieValueFromResponse(mockResponse);
mockRequest.headers.cookie = `${cookieName}=${cookieValue};`;
const decodedCookieValue = signed
Expand Down

0 comments on commit 54f6c06

Please sign in to comment.