2019-05-29 05:21:06 -04:00
|
|
|
const RateLimiter = require('../../infrastructure/RateLimiter')
|
2019-11-20 15:28:42 -05:00
|
|
|
const { promisifyAll } = require('../../util/promises')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
|
|
|
const ONE_MIN = 60
|
|
|
|
const ATTEMPT_LIMIT = 10
|
|
|
|
|
2019-11-20 15:28:42 -05:00
|
|
|
function processLoginRequest(email, callback) {
|
|
|
|
const opts = {
|
|
|
|
endpointName: 'login',
|
|
|
|
throttle: ATTEMPT_LIMIT,
|
|
|
|
timeInterval: ONE_MIN * 2,
|
|
|
|
subjectName: email
|
|
|
|
}
|
|
|
|
RateLimiter.addCount(opts, (err, shouldAllow) => callback(err, shouldAllow))
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-11-20 15:28:42 -05:00
|
|
|
function recordSuccessfulLogin(email, callback) {
|
|
|
|
if (callback == null) {
|
|
|
|
callback = function() {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-11-20 15:28:42 -05:00
|
|
|
RateLimiter.clearRateLimit('login', email, callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-11-20 15:28:42 -05:00
|
|
|
|
|
|
|
const LoginRateLimiter = {
|
|
|
|
processLoginRequest,
|
|
|
|
recordSuccessfulLogin
|
|
|
|
}
|
|
|
|
LoginRateLimiter.promises = promisifyAll(LoginRateLimiter)
|
|
|
|
|
|
|
|
module.exports = LoginRateLimiter
|