Problems with authentication from passport-totp authenticationn

Viewed 142

I'm trying to use a passport strategy TOTP, but I'm having problems with the authentication part that is always refused. I can log in with username and password, generate the QR code for token generation through the Google Authenticator app, but when the code token is informed, the authentication is refused. The key is being stored on Redis. Here's my code:

base32 = require('thirty-two')
const passport = require('passport')
const TotpStrategy = require('passport-totp').Strategy

passport.use(new TotpStrategy(
  async function (user, done) {
    const email = user.emails[0].value
    try {
      const key = await client.get(email)
      console.log('estratégia', key)
      if (!key) {
        return done(new Error(('Sem Chave')))
      } else {
        return done(null, key, 30)
        // return done(null, key, 30)
      }
    } catch (err) {
      console.log(err)
    }
  }
))

const randomKey = function (len) {
  const buf = []
  const chars = 'abcdefghijklmnopqrstuvwxyz0123456789'
  const charlen = chars.length
  for (let i = 0; i < len; ++i) {
    buf.push(chars[getRandomInt(0, charlen - 1)])
  }

  return buf.join('')
}

function getRandomInt (min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min
}

  exports.getToken = async (req, res) => {
    console.log('Tela de Token ')
    const email = req.user.emails[0].value
    const chave = await client.get(email)
    console.log('Controller', chave)

    if (chave) {
      const encodedKey = base32.encode(chave)
    console.log(encodedKey)

    let url = null
    const qrData = ('otpauth://totp/' + req.user.emails[0].value + '?secret=' + encodedKey)
    url = 'https://chart.googleapis.com/chart?chs=166x166&chld=L|0&cht=qr&chl=' + qrData

    res.render('token', {
      // In case of loss of key, change the value from "QR" to "url". 
      qr: null,
      user: req.user,
      key: encodedKey,
      pageTitle: 'Token',
      path: '/token'
    })
  } else {
    const secret = randomKey(10)
    const encodedKey = base32.encode(secret)

    try {
      await client.set(email, secret)
      console.log(chave)
      let url = null
      const qrData = ('otpauth://totp/' + req.user.emails[0].value + '?secret=' + encodedKey)
      url = 'https://chart.googleapis.com/chart?chs=166x166&chld=L|0&cht=qr&chl=' + qrData

      res.render('token', {
        qr: url,
        user: req.user,
        key: encodedKey,
        pageTitle: 'Token',
        path: '/token'
      })
    } catch (err) {
      console.log(err)
    }
  }
}

router.post('/token',
passport.authenticate('totp', { failureRedirect: '/token' }),
function (req, res) {
  req.session.secondFactor = 'totp'
  res.redirect('/')
}

)

As I said, I'm having problems with the passport.authenticate function which always refuses the code entered in Google Authenticator. (The complete code was not informed, only those referring to TOTP, but if you need help, just ask me). Grateful.

0 Answers
Related