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,
|
|
|
|
key: function (req, res, next) {
|
|
|
|
next(url.parse(req.url).pathname);
|
|
|
|
}
|
|
|
|
})(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,
|
2018-09-20 16:03:33 +03:00
|
|
|
key(req, res, next) {
|
2017-03-13 23:07:12 +03:00
|
|
|
next(url.parse(req.url).pathname);
|
|
|
|
}
|
|
|
|
})(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,
|
2018-09-20 16:03:33 +03:00
|
|
|
key(req, res, next) {
|
2017-03-13 23:07:12 +03:00
|
|
|
if (req.body.username) {
|
2018-09-20 16:03:33 +03:00
|
|
|
return next(`${req.body.username}login`);
|
2017-03-13 23:07:12 +03:00
|
|
|
}
|
2016-11-17 16:02:56 +03:00
|
|
|
|
2017-03-13 23:07:12 +03:00
|
|
|
if (req.body.authorizationCode) {
|
2018-09-20 16:03:33 +03:00
|
|
|
return next(`${req.body.authorizationCode}login`);
|
2017-03-13 23:07:12 +03:00
|
|
|
}
|
2016-11-17 16:02:56 +03:00
|
|
|
|
2017-03-13 23:07:12 +03:00
|
|
|
if (req.body.refresh_token) {
|
2018-09-20 16:03:33 +03:00
|
|
|
return next(`${req.body.refresh_token}login`);
|
2017-03-13 23:07:12 +03:00
|
|
|
}
|
2016-11-17 16:02:56 +03:00
|
|
|
|
2017-03-13 23:07:12 +03:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
})(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,
|
2018-09-20 16:03:33 +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,
|
2018-09-20 16:03:33 +03:00
|
|
|
key(req, res, next) {
|
2017-03-13 23:07:12 +03:00
|
|
|
next('privateblog');
|
|
|
|
}
|
|
|
|
})(req, res, next);
|
|
|
|
}
|
2016-11-08 14:33:19 +03:00
|
|
|
};
|