2022-10-05 08:21:34 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const utils = require('@strapi/utils');
|
2022-11-02 18:49:05 +01:00
|
|
|
const { toLower } = require('lodash/fp');
|
2022-10-05 08:21:34 -07:00
|
|
|
|
|
|
|
const { RateLimitError } = utils.errors;
|
|
|
|
|
|
|
|
module.exports =
|
|
|
|
(config, { strapi }) =>
|
|
|
|
async (ctx, next) => {
|
|
|
|
const ratelimit = require('koa2-ratelimit').RateLimit;
|
|
|
|
|
2022-11-02 18:49:05 +01:00
|
|
|
const userEmail = toLower(ctx.request.body.email) || 'unknownEmail';
|
2022-10-07 07:50:26 -07:00
|
|
|
|
2022-10-05 08:21:34 -07:00
|
|
|
return ratelimit.middleware({
|
2022-10-07 07:50:26 -07:00
|
|
|
interval: { min: 5 },
|
2022-10-05 08:21:34 -07:00
|
|
|
max: 5,
|
2022-10-07 07:50:49 -07:00
|
|
|
prefixKey: `${userEmail}:${ctx.request.path}:${ctx.request.ip}`,
|
2022-10-05 08:21:34 -07:00
|
|
|
handler() {
|
|
|
|
throw new RateLimitError();
|
|
|
|
},
|
|
|
|
...strapi.config.get('admin.ratelimit'),
|
|
|
|
...config,
|
|
|
|
})(ctx, next);
|
|
|
|
};
|