mirror of
https://github.com/sasjs/server.git
synced 2026-01-09 07:20:05 +00:00
chore: move brute force protection logic to middleware and a singleton class
This commit is contained in:
21
api/src/middlewares/bruteForceProtection.ts
Normal file
21
api/src/middlewares/bruteForceProtection.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { RequestHandler } from 'express'
|
||||
import { RateLimiter, secondsToHms } from '../utils'
|
||||
|
||||
export const bruteForceProtection: RequestHandler = async (req, res, next) => {
|
||||
const ip = req.ip
|
||||
const username = req.body.username
|
||||
|
||||
const rateLimiter = RateLimiter.getInstance()
|
||||
|
||||
const retrySecs = await rateLimiter.check(ip, username)
|
||||
|
||||
if (retrySecs > 0) {
|
||||
res
|
||||
.status(429)
|
||||
.send(`Too Many Requests! Retry after ${secondsToHms(retrySecs)}`)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
next()
|
||||
}
|
||||
@@ -4,3 +4,4 @@ export * from './csrfProtection'
|
||||
export * from './desktop'
|
||||
export * from './verifyAdmin'
|
||||
export * from './verifyAdminIfNeeded'
|
||||
export * from './bruteForceProtection'
|
||||
|
||||
Reference in New Issue
Block a user