Skip to content

Commit

Permalink
Limit repeated failed requests to auth endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
hagopj13 committed Nov 19, 2019
1 parent a8e50c4 commit f8e4a51
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-mongo-sanitize": "^1.3.2",
"express-rate-limit": "^5.0.0",
"helmet": "^3.21.2",
"http-status": "^1.4.0",
"jsonwebtoken": "^8.5.1",
Expand Down
6 changes: 6 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const httpStatus = require('http-status');
const config = require('./config/config');
const morgan = require('./config/morgan');
const { jwtStrategy } = require('./config/passport');
const { authLimiter } = require('./middlewares/rateLimiter');
const routes = require('./routes/v1');
const { errorConverter, errorHandler } = require('./middlewares/error');
const AppError = require('./utils/AppError');
Expand Down Expand Up @@ -44,6 +45,11 @@ app.options('*', cors());
app.use(passport.initialize());
passport.use('jwt', jwtStrategy);

// limit repeated failed requests to auth endpoints
if (config.env === 'production') {
app.use('/v1/auth', authLimiter);
}

// v1 api routes
app.use('/v1', routes);

Expand Down
11 changes: 11 additions & 0 deletions src/middlewares/rateLimiter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const rateLimit = require('express-rate-limit');

const authLimiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 20,
skipSuccessfulRequests: true,
});

module.exports = {
authLimiter,
};
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2269,6 +2269,11 @@ express-mongo-sanitize@^1.3.2:
resolved "https://registry.yarnpkg.com/express-mongo-sanitize/-/express-mongo-sanitize-1.3.2.tgz#fba404f6c041577cbeeec4dd9057cefbb439de5a"
integrity sha1-+6QE9sBBV3y+7sTdkFfO+7Q53lo=

express-rate-limit@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/express-rate-limit/-/express-rate-limit-5.0.0.tgz#9a6f4cacc388c1a1da7ba2f65db69f7395e9b04e"
integrity sha512-dhT57wqxfqmkOi4HM7NuT4Gd7gbUgSK2ocG27Y6lwm8lbOAw9XQfeANawGq8wLDtlGPO1ZgDj0HmKsykTxfFAg==

express@^4.17.1:
version "4.17.1"
resolved "https://registry.yarnpkg.com/express/-/express-4.17.1.tgz#4491fc38605cf51f8629d39c2b5d026f98a4c134"
Expand Down

0 comments on commit f8e4a51

Please sign in to comment.