-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (41 loc) · 1.29 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
var parallel = require('run-parallel')
var Debouncer = require('debouncer')
var ms = require('ms')
var delayTimes = ['0 s', '5 s', '30 s', '5 m', '10 m', '30 m', '1 hr'].map(ms)
module.exports = function (core, db) {
var beginAuth = Object.create(core).beginAuthentication
var debounce = new Debouncer(db, { delayTimeMs: delayTimes })
function debounceKey(key) {
return function (done) {
debounce(key, function (err, allowed, remaining) {
if (err) {
done(err)
} else {
done(null, {
allowed: allowed,
remaining: remaining
})
}
})
}
}
core.beginAuthentication = function beginAuthentication(sessionId, emailAddress, cb) {
parallel({
email: debounceKey(emailAddress),
session: debounceKey(sessionId)
}, function (err, result) {
if (err) {
cb && cb(err)
} else if (result.email.allowed && result.session.allowed) { // This is what we want
beginAuth.call(null, sessionId, emailAddress, cb)
} else {
var debounceError = new Error('Email and/or session debounce failure')
debounceError.debounce = true
cb && cb(debounceError, {
allowed: result.email.allowed && result.session.allowed,
remaining: Math.max(result.email.remaining, result.session.remaining)
})
}
})
}
}