Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 44x 775x 775x 753x 196x 196x 3x 3x 193x 557x 361x 361x 6x 6x 355x 355x 355x 14x 9x 5x 14x 196x 196x 733x 733x 345x 342x 3x 733x 3x 733x 180x 180x 180x 180x 733x 1x 775x 775x 22x 22x 753x 196x 557x 557x 557x 1x 557x 557x 9x 548x 1x 1332x 1x 199x 199x 199x 199x 199x 1x | import UnauthorizedError from '@app/errors/unauthorized-error';
import config from '@config';
import Tokens from 'csrf';
import {generateToken, cookieOptions} from './jwt-util';
import jsonwebtoken from 'jsonwebtoken';
import debug from 'debug';
import {NextFunction, RequestHandler, Request, Response} from 'express';
const error = debug('group-car:jwt:error');
const warn = debug('group-car:jwt:warn');
/**
* Save secret name is constant for shorter code.
*/
const secretName = config.jwt.securityOptions.secretName;
/**
* Creates a middleware for csrf protection with jwt tokens.
*
* Expects the jwt token to in a cookie.
*/
const jwtCsrf: () => RequestHandler = () => {
return (req: Request, res: Response, next: NextFunction): void => {
const tokens = new Tokens();
/**
* Get the jwt token from the request.
* Verify is used but the error is caught and
* a null value is instead returned.
* Because an existing jwt is only import
* for not ignored methods, but only in the
* case for csrf protection.
* Api protection with jwt is not covered in this handler.
*/
const jwt = checkRequestConfig(req);
let secret: string;
let token: string | undefined;
if (jwt && isIgnoredMethod(req)) {
/*
* If the jwt exists on the request and the request
* is an ignored method. The secret will be retrieved,
* if no secret can be extracted from the jwt
* the jwt is misconfigured.
* Replaced the jwt with a simple pre-login jwt
*/
const _secret = getSecret(jwt);
if (_secret === null) {
warn('Misconfigured jwt on ignored method.' +
' Jwt will be replaced with pre-login jwt');
secret = setSecret(tokens, req, res);
} else {
secret = _secret;
}
} else if (jwt && !isIgnoredMethod(req)) {
/*
* If the jwt exists on the request and the request
* is not an ignored method. The secret will be retrieved,
* if no secret exists on the jwt token an Error will
* be thrown. If the secret was retrieved it will search
* for the header which contains the csrf token.
* If it doesn't exist an error will be thrown.
* If it does exist the secret will be verified against it.
* If it fails an error is thrown because the secret is either
* not up to date or was stolen. If it doesn't fail the user
* can proceed as he would expect.
*/
const _secret = getSecret(jwt);
if (_secret === null) {
error('Misconfigured jwt. Request with jwt but without secret.');
throw new UnauthorizedError();
} else {
secret = _secret;
}
// Check if header with token exists
token = req.get(config.jwt.securityOptions.tokenName);
if (token === undefined || !tokens.verify(secret, token)) {
if (token === undefined) {
error('Request is missing jwt token header');
} else {
error('Secret not matching token');
}
throw new UnauthorizedError();
}
} else Eif (!jwt && isIgnoredMethod(req)) {
/*
* If the method is an ignored method and the jwt doesn't exist on it
* a new secret will be generated and a jwt token will be set as cookie.
*/
secret = setSecret(tokens, req, res);
} else {
error('Missing jwt');
throw new UnauthorizedError();
}
req.jwtToken = req.cookies['jwt'] ?
req.cookies['jwt'] :
req.jwtToken;
/**
* Add function to request which gets the csrf token for
* the secret in the request.
*/
req.getCsrfToken = () => {
if (token === undefined) {
return tokens.create(secret);
} else {
return token;
}
};
/**
* Add function to request which returns the secret.
*/
req.getSecret = () => {
return secret;
};
// Add function to response which automatically sets the given
// payload to the response
res.setJwtToken = (
payload: Record<string, unknown>,
subject?: string,
): void => {
// Create new payload which contains the csrf secret
const _payload = Object.assign({
[secretName]: secret,
}, payload);
const jwtToken = generateToken(_payload, subject);
req.jwtToken = jwtToken;
// Set the cookie on the response
res.cookie(config.jwt.name,
jwtToken,
cookieOptions);
};
next();
};
};
/**
* Returns whether or not the req has a jwt and the jwt contains
* the secret.
* @param req - The http request
* @returns The jwt payload or null if the jwt doesn't exist
*/
const checkRequestConfig = (req: Request):
// eslint-disable-next-line @typescript-eslint/ban-types
string | object | null => {
// Get jwt token from request
const jwtToken = config.jwt.getToken(req);
if (jwtToken == null && !isIgnoredMethod(req)) {
error('Missing jwt on covered method');
throw new UnauthorizedError();
} else if (jwtToken === null) {
return null;
}
let jwt;
try {
jwt = jsonwebtoken.verify(jwtToken, config.jwt.secret);
} catch (err) {
warn('Malformed jwt. Couldn\'t verify.');
return null;
}
return jwt;
};
/**
* Gets the secret from the request.
* @param req - The http request
* @returns the secret or null if either the
* jwt or the csrf secret inside the jwt doesn't exist
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const getSecret = (jwt: any): string | null => {
// Check if jwt available
Iif (!jwt) {
return null;
}
// Check if jwt has csrf secret
if (!jwt[secretName]) {
return null;
}
return jwt[secretName];
};
/**
* Returns whether or not the request should be ignored.
* @param req - The request
* @returns Whether or not the request should be ignored
*/
const isIgnoredMethod = (req: Request): boolean => {
return config.jwt.securityOptions.ignoredMethods
.includes(req.method.toUpperCase());
};
/**
* Generates a new secret and sets the secret
* on the request in form of a jwt cookie.
* @param req - The http request
* @returns The new secret
*/
const setSecret = (tokens: Tokens, req: Request, res: Response): string => {
// Generate new secret
const secret = tokens.secretSync();
const jwtToken = generateToken({[secretName]: secret});
req.jwtToken = jwtToken;
res.cookie(
config.jwt.name,
jwtToken,
{
secure: process.env.NODE_ENV === 'production',
sameSite: process.env.NODE_ENV === 'production',
httpOnly: true,
},
);
return secret;
};
export default jwtCsrf;
|