mirror of
https://github.com/luxfi/pics.git
synced 2026-10-30 15:22:15 +00:00
29 lines
541 B
JavaScript
29 lines
541 B
JavaScript
const OTPAuth = require('otpauth')
|
|
, redis = require(__dirname+'/../redis/redis.js');
|
|
|
|
module.exports = async (username, totpSecret, userInput) => {
|
|
|
|
const totp = new OTPAuth.TOTP({
|
|
secret: totpSecret,
|
|
algorithm: 'SHA256',
|
|
});
|
|
|
|
let delta = totp.validate({
|
|
token: userInput,
|
|
algorithm: 'SHA256',
|
|
window: 1,
|
|
});
|
|
|
|
if (delta !== null) {
|
|
const key = `twofactor_success:${username}:${userInput}`;
|
|
const uses = await redis.incr(key);
|
|
redis.expire(key, 30);
|
|
if (uses && uses > 1) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
return delta;
|
|
|
|
};
|