2023-01-30 07:45:48 -05:00
|
|
|
const { RateLimiter } = require('../../infrastructure/RateLimiter')
|
2023-10-19 07:38:17 -04:00
|
|
|
const { promisifyAll } = require('@overleaf/promise-utils')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2023-01-30 07:45:48 -05:00
|
|
|
const rateLimiter = new RateLimiter('login', {
|
|
|
|
points: 10,
|
|
|
|
duration: 120,
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-11-20 15:28:42 -05:00
|
|
|
function processLoginRequest(email, callback) {
|
2023-01-30 07:45:48 -05:00
|
|
|
rateLimiter
|
2024-02-13 03:51:54 -05:00
|
|
|
.consume(email, 1, { method: 'email' })
|
2023-01-30 07:45:48 -05:00
|
|
|
.then(() => {
|
|
|
|
callback(null, true)
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
if (err instanceof Error) {
|
|
|
|
callback(err)
|
|
|
|
} else {
|
|
|
|
callback(null, false)
|
|
|
|
}
|
|
|
|
})
|
2019-11-20 15:28:42 -05:00
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-11-20 15:28:42 -05:00
|
|
|
function recordSuccessfulLogin(email, callback) {
|
2023-01-30 07:45:48 -05:00
|
|
|
rateLimiter
|
|
|
|
.delete(email)
|
|
|
|
.then(() => {
|
|
|
|
callback()
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
callback(err)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-11-20 15:28:42 -05:00
|
|
|
|
|
|
|
const LoginRateLimiter = {
|
|
|
|
processLoginRequest,
|
2021-04-27 03:52:58 -04:00
|
|
|
recordSuccessfulLogin,
|
2019-11-20 15:28:42 -05:00
|
|
|
}
|
|
|
|
LoginRateLimiter.promises = promisifyAll(LoginRateLimiter)
|
|
|
|
|
|
|
|
module.exports = LoginRateLimiter
|