# Upgrading

This file intends to document and capture any complicated and breaking changes between major versions.

## Version 3 -> 4

Version 4 comes with a variety of breaking changes that you may need to address in your codebase when upgrading. This intends to be a comprehensive guide on upgrading from v3 to v4, if you find anything that is not documented please raise an issue or a pull request.

### CSRF token format has changed

CSRF tokens are now generated using `createHmac` instead of `createHash`. Here is how CSRF token generation works from v4:

1. A `randomValue` is generated via `randomBytes`
2. A `message` is constructed in the format `[uniqueIdentifier.length, uniqueIdentifier, randomValue.length, randomValue].join(messageDelimiter)` where:
     - `randomValue` is from step 1; and
     - `uniqueIdentifier` is the value derived from the configured `getSessionIdentifier`; and
     - `messageDelimiter` is the new `messageDelimiter` configuration option
3. A hmac is generated from the `message` via `createHmac` using the secret returned by `getSecret` at the time of generation
4. The CSRF token takes the format `${hmac}${csrfTokenDelimiter}${randomValue}` where:
   - `hmac` is the value from step 3; and
   - `csrfTokenDelimiter` is the new `csrfTokenDelimiter` configuration option; and
   - `randomValue` is from step 1 
5. The value returned by `generateCsrfToken` or `req.csrfToken` will now be the same, the new construction means the tokens validity can be better confirmed without needing to separately track a hash and unhashed version of the CSRF token.

### Zero Downtime Upgrade (ZDU)

Tokens previously generated with version 3 will not be compatible once you upgrade to version 4. You may need to consider clearing existing CSRF tokens or forcing new CSRF token generation.


### Configuration changes

#### delimiter -> csrfTokenDelimiter

The `delimiter` configuration option has been renamed to `csrfTokenDelimiter` and is now used to separate the `hmac` and the `randomValue` to form the CSRF token. This defaults to `.`.

#### getSessionIdentifier (now required)

The `getSessionIdentifier` configuration option is now required and it has to return the unique identifier associated with the request. This should be the session identifier if you are using sessions, the JWT if you're using JWT, or whatever other unique value you are using to identify requests. The value returned should be unique per user, per session. That is if a user logs in, then they logout, then they login again, the identifier should be different.

If you do not use `getSessionIdentifier` correctly, or you force it to return an empty string, you could be compromising your security.

#### getTokenFromRequest -> getCsrfTokenFromRequest

The `getTokenFromRequest` configuration option has been renamed to `getCsrfTokenFromRequest`. 

```js
// before
const { doubleCsrfProtection } = doubleCsrf({
    getSecret: (req) => "...",
    getTokenFromRequest: (req) => req.headers['x-csrf-token']
});
// after
const { doubleCsrfProtection } = doubleCsrf({
    getSecret: (req) => "...",
    getSessionIdentifier: (req) => req.session.id,
    getCsrfTokenFromRequest: (req) => req.headers['x-csrf-token']
});

// before
const { doubleCsrfProtection } = doubleCsrf({
    getSecret: (req) => "...",
    getTokenFromRequest: function (req) {
        return req.headers['x-csrf-token'];
    }
});
// after
const { doubleCsrfProtection } = doubleCsrf({
    getSecret: (req) => "...",
    getSessionIdentifier: (req) => req.session.id,
    getTokenFromRequest: function (req) {
        return req.headers['x-csrf-token'];
    }
});
```

#### hmacAlgorithm (new)

The `hmacAlgorithm` configuration option is new and is passed into the `createHmac` call to specify the algorithm to use. This defaults to `sha256`.

#### messageDelimiter (new)

The `messageDelimiter` configuration option is new and is used to separate the values in the message used to construct the hmac. This defaults to `|`.

#### signed (removed)

The `signed` configuration option from the `cookieOptions` has been removed and is no longer available, this option is a redundant overhead for `csrf-csrf`. Tokens generated by `csrf-csrf` are inherently signed already. There is no benefit to signing it twice.

#### size

The `size` configuration option now sets the size of the `randomValue` used to construct the hmac. It now defaults to 32 instead of 64.

### Utility Changes

#### generateToken -> generateCsrfToken

The `generateToken` function returned by `doubleCsrf` has been renamed to `generateCsrfToken` and has had it's paramters updated. The last two parameters `overwrite` and `validateOnReuse` have been merged into a single object parameter.

The default value for `validateOnReuse` has been changed to false. If you were previously dependent on this being true you will need to manually set it.

Additionally the third object parameter also accepts `cookieOptions` to override cookie options at generation time.

As the `Request#csrfToken` utility delegates directly to the `generateCsrfToken` function, it has also changed, examples of necessary changes below.

```js
// before
const { generateToken, doubleCsrfProtection } = doubleCsrf({ ... });
//after
const { generateCsrfToken, doubleCsrfProtection } = doubleCsrf({ ... });

// before
generateToken(req, res);
// after
generateCsrfToken(req, res, { validateOnReuse: true });
// before
generateToken(req, res, true);
// after
generateCsrfToken(req, res, { overwrite: true, validateOnReuse: true });

// before
generateToken(req, res, true, false);
// after
generateCsrfToken(req, res, { overwrite: true });

// before
req.csrfToken(false, false);
// after
req.csrfToken();

// before 
req.csrfToken(true);
// after
req.csrfToken({ overwrite: true, validateOnReuse: true });

// before
req.csrfToken(true, false);
// after
req.csrfToken({ overwrite: true });

// before
req.csrfToken(true, true);
// after
req.csrfToken({ overwrite: true, validateOnReuse: true });
```

### Typescript

This is not an exhaustive list of type changes, only those that are not implicit based on other documented changes.

* Type `CsrfTokenCookieOverrides` renamed to `CsrfTokenCookieOptions`
* Type `CsrfTokenCreator` renamed to `CsrfTokenGenerator`
* Type `doubleCsrfProtection` renamed to `DoubleCsrfProtection`
* Type `RequestMethod` renamed to `CsrfRequestMethod`
* Type `CsrfIgnoredMethods` renamed to `CsrfIgnoredRequestMethods`
* Type `CsrfTokenGeneratorRequestUtil` added for `Request#csrfToken`

