Refactor new OTPAuth...validate pattern, remove await -- it isn't and shouldn't be async

indiachan-spamvector
Thomas Lynch 2 years ago
parent e5d0f9871f
commit d9288a137a
  1. 14
      lib/misc/dotwofactor.js
  2. 12
      models/forms/changepassword.js
  3. 12
      models/forms/login.js
  4. 12
      models/forms/twofactor.js

@ -0,0 +1,14 @@
const OTPAuth = require('otpauth');
module.exports = (totpSecret, userInput) => {
const totp = new OTPAuth.TOTP({
secret: totpSecret,
algorithm: 'SHA256',
});
const delta = totp.validate({
token: userInput,
algorithm: 'SHA256',
window: 1,
});
return { totp, delta };
};

@ -3,7 +3,7 @@
const bcrypt = require('bcrypt') const bcrypt = require('bcrypt')
, dynamicResponse = require(__dirname+'/../../lib/misc/dynamic.js') , dynamicResponse = require(__dirname+'/../../lib/misc/dynamic.js')
, redis = require(__dirname+'/../../lib/redis/redis.js') , redis = require(__dirname+'/../../lib/redis/redis.js')
, OTPAuth = require('otpauth') , doTwoFactor = require(__dirname+'/../../lib/misc/dotwofactor.js')
, { Accounts } = require(__dirname+'/../../db/'); , { Accounts } = require(__dirname+'/../../db/');
module.exports = async (req, res) => { module.exports = async (req, res) => {
@ -37,15 +37,7 @@ module.exports = async (req, res) => {
} }
if (account.twofactor) { if (account.twofactor) {
const totp = new OTPAuth.TOTP({ const { delta } = doTwoFactor(account.twofactor, req.body.twofactor);
secret: account.twofactor,
algorithm: 'SHA256',
});
const delta = await totp.validate({
token: req.body.twofactor,
algorithm: 'SHA256',
window: 1,
});
if (delta === null) { if (delta === null) {
return dynamicResponse(req, res, 403, 'message', { return dynamicResponse(req, res, 403, 'message', {
'title': 'Forbidden', 'title': 'Forbidden',

@ -3,7 +3,7 @@
const bcrypt = require('bcrypt') const bcrypt = require('bcrypt')
, dynamicResponse = require(__dirname+'/../../lib/misc/dynamic.js') , dynamicResponse = require(__dirname+'/../../lib/misc/dynamic.js')
, { Accounts } = require(__dirname+'/../../db/') , { Accounts } = require(__dirname+'/../../db/')
, OTPAuth = require('otpauth'); , doTwoFactor = require(__dirname+'/../../lib/misc/dotwofactor.js');
module.exports = async (req, res) => { module.exports = async (req, res) => {
@ -41,15 +41,7 @@ module.exports = async (req, res) => {
} }
if (account.twofactor) { if (account.twofactor) {
const totp = new OTPAuth.TOTP({ const { delta } = doTwoFactor(account.twofactor, req.body.twofactor);
secret: account.twofactor,
algorithm: 'SHA256',
});
const delta = await totp.validate({
token: req.body.twofactor,
algorithm: 'SHA256',
window: 1,
});
if (delta === null) { if (delta === null) {
return dynamicResponse(req, res, 403, 'message', { return dynamicResponse(req, res, 403, 'message', {
'title': 'Forbidden', 'title': 'Forbidden',

@ -3,7 +3,7 @@
const redis = require(__dirname+'/../../lib/redis/redis.js') const redis = require(__dirname+'/../../lib/redis/redis.js')
, dynamicResponse = require(__dirname+'/../../lib/misc/dynamic.js') , dynamicResponse = require(__dirname+'/../../lib/misc/dynamic.js')
, { Accounts } = require(__dirname+'/../../db/') , { Accounts } = require(__dirname+'/../../db/')
, OTPAuth = require('otpauth'); , doTwoFactor = require(__dirname+'/../../lib/misc/dotwofactor.js');
module.exports = async (req, res) => { module.exports = async (req, res) => {
@ -20,15 +20,7 @@ module.exports = async (req, res) => {
} }
// Validate totp // Validate totp
const totp = new OTPAuth.TOTP({ const { delta } = doTwoFactor(tempSecret, req.body.twofactor);
secret: tempSecret,
algorithm: 'SHA256',
});
const delta = await totp.validate({
token: req.body.twofactor,
algorithm: 'SHA256',
window: 1,
});
// Check if code was valid // Check if code was valid
if (delta === null) { if (delta === null) {

Loading…
Cancel
Save