-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
138 lines (111 loc) · 3.21 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const express = require('express')
const sqlite3 = require('sqlite3')
const session = require('express-session')
const { authenticator } = require('otplib')
const QRCode = require('qrcode')
const jwt = require('jsonwebtoken')
const expressJWT = require('express-jwt')
const bodyParser = require('body-parser')
const app = express()
const port = 3000
app.set('view engine', 'ejs')
app.use(session({
secret: 'supersecret',
}))
app.use(bodyParser.urlencoded({ extended: false }))
app.get('/', (req, res) => {
res.render('signup.ejs')
})
app.post('/sign-up', (req, res) => {
const email = req.body.email,
secret = authenticator.generateSecret()
const db = new sqlite3.Database('db.sqlite')
db.serialize(() => {
db.run('INSERT INTO `users`(`email`, `secret`) VALUES (?, ?)',
[email, secret],
(err) => {
if (err) {
throw err
}
//generate qr and put it in session
QRCode.toDataURL(authenticator.keyuri(email, '2FA Node App', secret), (err, url) => {
if (err) {
throw err
}
req.session.qr = url
req.session.email = email
res.redirect('/sign-up-2fa')
})
})
})
})
app.get('/sign-up-2fa', (req, res) => {
if (!req.session.qr) {
return res.redirect('/')
}
return res.render('signup-2fa.ejs', { qr: req.session.qr })
})
app.post('/sign-up-2fa', (req, res) => {
if (!req.session.email) {
return res.redirect('/')
}
const email = req.session.email,
code = req.body.code
return verifyLogin(email, code, req, res, '/sign-up-2fa')
})
const jwtMiddleware = expressJWT({
secret: 'supersecret',
algorithms: ['HS256'],
getToken: (req) => {
return req.session.token
}
})
app.get('/login', (req, res) => {
return res.render('login.ejs')
})
app.post('/login', (req, res) => {
//verify login
const email = req.body.email,
code = req.body.code
return verifyLogin(email, code, req, res, '/login')
})
app.get('/private', jwtMiddleware, (req, res) => {
return res.render('private.ejs', {email: req.user})
})
app.get('/logout', jwtMiddleware, (req, res) => {
req.session.destroy()
return res.redirect('/')
})
function verifyLogin (email, code, req, res, failUrl) {
//load user by email
const db = new sqlite3.Database('db.sqlite')
db.serialize(() => {
db.get('SELECT secret FROM users WHERE email = ?', [email], (err, row) => {
if (err) {
throw err
}
if (!row) {
return res.redirect('/')
}
if (!authenticator.check(code, row.secret)) {
//redirect back
return res.redirect(failUrl)
}
//correct, add jwt to session
req.session.qr = null
req.session.email = null
req.session.token = jwt.sign(email, 'supersecret')
//redirect to "private" page
return res.redirect('/private')
})
})
}
//create database with tables if it doesn't exist
const db = new sqlite3.Database('db.sqlite')
db.serialize(() => {
db.run('CREATE TABLE IF NOT EXISTS `users` (`user_id` INTEGER PRIMARY KEY AUTOINCREMENT, `email` VARCHAR(255) NOT NULL, `secret` varchar(255) NOT NULL)')
})
db.close()
app.listen(port, () => {
console.log(`2FA Node app listening at http://localhost:${port}`)
})