|
| 1 | +const speakeasy = require('speakeasy'); |
| 2 | +const { toDataURL } = require('qrcode'); |
| 3 | + |
| 4 | +// TODO: move this use to db |
| 5 | +const user = { |
| 6 | + firstName: 'Jon', |
| 7 | + lastName: 'Doe', |
| 8 | + email: 'jon.doe@gmail.com', |
| 9 | + password: 'test' |
| 10 | +}; |
| 11 | + |
| 12 | +const setup2Auth = async (req, res, next) => { |
| 13 | + try { |
| 14 | + const secret = speakeasy.generateSecret({ length: 10 }); |
| 15 | + const dataUrl = await toDataURL(secret.otpauth_url); |
| 16 | + |
| 17 | + // TODO: save to logged in user. |
| 18 | + user.twofactor = { |
| 19 | + secret: '', |
| 20 | + tempSecret: secret.base32, |
| 21 | + dataUrl, |
| 22 | + otpURL: secret.otpauth_url |
| 23 | + }; |
| 24 | + |
| 25 | + res.json({ |
| 26 | + message: 'Verify OTP', |
| 27 | + tempSecret: secret.base32, |
| 28 | + dataUrl, |
| 29 | + otpURL: secret.otpauth_url |
| 30 | + }); |
| 31 | + } catch (err) { |
| 32 | + next(err); |
| 33 | + } |
| 34 | +}; |
| 35 | + |
| 36 | +// get 2fa details |
| 37 | +const get2Auth = (req, res) => { |
| 38 | + res.json(user.twofactor); |
| 39 | +}; |
| 40 | + |
| 41 | +// disable 2fa |
| 42 | +const remove2Auth = (req, res) => { |
| 43 | + delete user.twofactor; |
| 44 | + |
| 45 | + res.send('Successfully remove user 2Auth'); |
| 46 | +}; |
| 47 | + |
| 48 | +const verifyAuth = (req, res) => { |
| 49 | + const { token } = req.body; |
| 50 | + |
| 51 | + const verified = speakeasy.totp.verify({ |
| 52 | + secret: user.twofactor.tempSecret, // TODO: Receive secret from user |
| 53 | + encoding: 'base32', |
| 54 | + token |
| 55 | + }); |
| 56 | + |
| 57 | + if (verified) { |
| 58 | + user.twofactor.secret = user.twofactor.tempSecret; // set secret, confirm 2fa |
| 59 | + return res.send('Two-factor auth enabled'); |
| 60 | + } |
| 61 | + |
| 62 | + return res.status(400).send('Invalid token, verification failed'); |
| 63 | +}; |
| 64 | + |
| 65 | +const login = (req, res) => { |
| 66 | + if (!user.twofactor || !user.twofactor.secret) { // two factor is not enabled by the user |
| 67 | + // check credentials |
| 68 | + if (req.body.email === user.email && req.body.password === user.password) { |
| 69 | + return res.send('Successfully authenticated without OTP'); // authenticate user |
| 70 | + } |
| 71 | + return res.status(400).send('Invald email or password'); |
| 72 | + } |
| 73 | + // two factor enabled |
| 74 | + if (req.body.email !== user.email || req.body.password !== user.password) { |
| 75 | + return res.status(400).send('Invald email or password'); |
| 76 | + } |
| 77 | + |
| 78 | + // check if otp is passed, if not then ask for OTP |
| 79 | + if (!req.headers['x-otp']) { |
| 80 | + return res.status(206).send('Please enter otp to continue'); |
| 81 | + } |
| 82 | + |
| 83 | + // validate otp |
| 84 | + const verified = speakeasy.totp.verify({ |
| 85 | + secret: user.twofactor.secret, |
| 86 | + encoding: 'base32', |
| 87 | + token: req.headers['x-otp'] |
| 88 | + }); |
| 89 | + |
| 90 | + return verified |
| 91 | + ? res.send('Successfully authenticated with OTP') |
| 92 | + : res.status(400).send('Invalid OTP'); |
| 93 | +}; |
| 94 | + |
| 95 | +module.exports = { |
| 96 | + setup2Auth, |
| 97 | + get2Auth, |
| 98 | + remove2Auth, |
| 99 | + verifyAuth, |
| 100 | + login |
| 101 | +}; |
0 commit comments