mirror of https://gitgud.io/fatchan/jschan.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
541 B
28 lines
541 B
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;
|
|
|
|
};
|
|
|