swaf/dist/components/CsrfProtectionComponent.js
2020-04-22 17:49:58 +02:00

57 lines
7.1 KiB
JavaScript

var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import ApplicationComponent from "../ApplicationComponent";
import crypto from "crypto";
import { BadRequestError } from "../HttpError";
export default class CsrfProtectionComponent extends ApplicationComponent {
start(app, router) {
return __awaiter(this, void 0, void 0, function* () {
router.use((req, res, next) => {
if (!req.session) {
throw new Error('Session is unavailable.');
}
res.locals.getCSRFToken = () => {
if (typeof req.session.csrf !== 'string') {
req.session.csrf = crypto.randomBytes(64).toString('base64');
}
return req.session.csrf;
};
if (!['GET', 'HEAD', 'OPTIONS'].find(s => s === req.method)) {
if (req.session.csrf === undefined) {
throw new InvalidCsrfTokenError(req.baseUrl, `You weren't assigned any CSRF token.`);
}
else if (req.body.csrf === undefined) {
throw new InvalidCsrfTokenError(req.baseUrl, `You didn't provide any CSRF token.`);
}
else if (req.session.csrf !== req.body.csrf) {
throw new InvalidCsrfTokenError(req.baseUrl, `Tokens don't match.`);
}
}
next();
});
});
}
stop() {
return __awaiter(this, void 0, void 0, function* () {
});
}
}
class InvalidCsrfTokenError extends BadRequestError {
constructor(url, details, cause) {
super(`Invalid CSRF token`, `${details} We can't process this request. Please try again.`, url, cause);
}
get name() {
return 'Invalid CSRF Token';
}
get errorCode() {
return 401;
}
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiQ3NyZlByb3RlY3Rpb25Db21wb25lbnQuanMiLCJzb3VyY2VSb290IjoiLi8iLCJzb3VyY2V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