2018-09-20 16:03:33 +03:00
|
|
|
const url = require('url');
|
|
|
|
const spamPrevention = require('./api/spam-prevention');
|
2016-11-08 14:33:19 +03:00
|
|
|
|
2017-01-24 00:44:39 +03:00
|
|
|
/**
|
|
|
|
* We set ignoreIP to false, because we tell brute-knex to use `req.ip`.
|
|
|
|
* We can use `req.ip`, because express trust proxy option is enabled.
|
|
|
|
*/
|
2016-11-08 14:33:19 +03:00
|
|
|
module.exports = {
|
2017-01-24 00:44:39 +03:00
|
|
|
/**
|
|
|
|
* block per route per ip
|
|
|
|
*/
|
2018-09-20 16:03:33 +03:00
|
|
|
globalBlock(req, res, next) {
|
2017-03-13 23:07:12 +03:00
|
|
|
return spamPrevention.globalBlock().getMiddleware({
|
|
|
|
ignoreIP: false,
|
2020-10-20 02:02:56 +03:00
|
|
|
key: function (_req, _res, _next) {
|
|
|
|
_next(url.parse(_req.url).pathname);
|
2017-03-13 23:07:12 +03:00
|
|
|
}
|
|
|
|
})(req, res, next);
|
|
|
|
},
|
2017-01-24 00:44:39 +03:00
|
|
|
/**
|
|
|
|
* block per route per ip
|
|
|
|
*/
|
2018-09-20 16:03:33 +03:00
|
|
|
globalReset(req, res, next) {
|
2017-03-13 23:07:12 +03:00
|
|
|
return spamPrevention.globalReset().getMiddleware({
|
|
|
|
ignoreIP: false,
|
2020-10-20 02:02:56 +03:00
|
|
|
key(_req, _res, _next) {
|
|
|
|
_next(url.parse(_req.url).pathname);
|
2017-03-13 23:07:12 +03:00
|
|
|
}
|
|
|
|
})(req, res, next);
|
|
|
|
},
|
2017-01-24 00:44:39 +03:00
|
|
|
/**
|
|
|
|
* block per user
|
|
|
|
* username === email!
|
|
|
|
*/
|
2018-09-20 16:03:33 +03:00
|
|
|
userLogin(req, res, next) {
|
2017-03-13 23:07:12 +03:00
|
|
|
return spamPrevention.userLogin().getMiddleware({
|
|
|
|
ignoreIP: false,
|
2020-10-20 02:02:56 +03:00
|
|
|
key(_req, _res, _next) {
|
|
|
|
if (_req.body.username) {
|
|
|
|
return _next(`${_req.body.username}login`);
|
2017-03-13 23:07:12 +03:00
|
|
|
}
|
2016-11-17 16:02:56 +03:00
|
|
|
|
2020-10-20 02:02:56 +03:00
|
|
|
if (_req.body.authorizationCode) {
|
|
|
|
return _next(`${_req.body.authorizationCode}login`);
|
2017-03-13 23:07:12 +03:00
|
|
|
}
|
2016-11-17 16:02:56 +03:00
|
|
|
|
2020-10-20 02:02:56 +03:00
|
|
|
if (_req.body.refresh_token) {
|
|
|
|
return _next(`${_req.body.refresh_token}login`);
|
2017-03-13 23:07:12 +03:00
|
|
|
}
|
2016-11-17 16:02:56 +03:00
|
|
|
|
2020-10-20 02:02:56 +03:00
|
|
|
return _next();
|
2017-03-13 23:07:12 +03:00
|
|
|
}
|
|
|
|
})(req, res, next);
|
|
|
|
},
|
2017-01-24 00:44:39 +03:00
|
|
|
/**
|
|
|
|
* block per user
|
|
|
|
*/
|
2018-09-20 16:03:33 +03:00
|
|
|
userReset(req, res, next) {
|
2017-03-13 23:07:12 +03:00
|
|
|
return spamPrevention.userReset().getMiddleware({
|
|
|
|
ignoreIP: false,
|
2020-10-20 02:02:56 +03:00
|
|
|
key(_req, _res, _next) {
|
|
|
|
_next(`${_req.body.username}reset`);
|
2017-03-13 23:07:12 +03:00
|
|
|
}
|
|
|
|
})(req, res, next);
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* block per ip
|
|
|
|
*/
|
2018-09-20 16:03:33 +03:00
|
|
|
privateBlog(req, res, next) {
|
2017-03-13 23:07:12 +03:00
|
|
|
return spamPrevention.privateBlog().getMiddleware({
|
|
|
|
ignoreIP: false,
|
2020-10-20 02:02:56 +03:00
|
|
|
key(_req, _res, _next) {
|
|
|
|
_next('privateblog');
|
2017-03-13 23:07:12 +03:00
|
|
|
}
|
|
|
|
})(req, res, next);
|
2019-01-07 16:03:46 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* protect content api from brute force
|
|
|
|
*/
|
|
|
|
contentApiKey(req, res, next) {
|
|
|
|
return spamPrevention.contentApiKey().getMiddleware({
|
|
|
|
ignoreIP: false
|
|
|
|
})(req, res, function (err, ...rest) {
|
|
|
|
if (!err) {
|
|
|
|
// Reset any blocks if the request is authorized
|
|
|
|
// This ensures that the count only goes up for
|
|
|
|
// unauthorized requests.
|
|
|
|
res.on('finish', function () {
|
|
|
|
if (res.statusCode < 400) {
|
|
|
|
req.brute.reset();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return next(err, ...rest);
|
|
|
|
});
|
2021-07-19 11:40:38 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
*/
|
|
|
|
membersAuth(req, res, next) {
|
|
|
|
return spamPrevention.userLogin().getMiddleware({
|
|
|
|
ignoreIP: false,
|
|
|
|
key(_req, _res, _next) {
|
|
|
|
if (_req.body.email) {
|
|
|
|
return _next(`${_req.body.email}login`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _next();
|
|
|
|
}
|
|
|
|
})(req, res, next);
|
2017-03-13 23:07:12 +03:00
|
|
|
}
|
2016-11-08 14:33:19 +03:00
|
|
|
};
|