-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
52 lines (40 loc) · 1.25 KB
/
server.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
var speakeasy = require('speakeasy')
var qr = require('qr-image');
var express = require('express');
var app = express();
app.set('views', './views')
app.set('view engine', 'jade')
app.get('/', function (req, res) {
var locals = {}
// Generate a secret
locals.secret = speakeasy.generateSecret({length: 20})
// Get QR code URL
locals.qrPath = '/qrcode?qrurl=' + encodeURIComponent(locals.secret.otpauth_url);
// Get initial token
locals.token = getToken(locals.secret.base32)
res.render('index', locals)
});
app.get('/token', function (req, res) {
res.send(getToken(req.query.secret));
});
app.get('/verify', function (req, res) {
if (verifyToken(req.query.secret, req.query.token)) {
res.send('Verified');
} else {
res.send('Failed to verify');
}
});
app.get('/qrcode', function(req, res) {
var code = qr.image(req.query.qrurl, { type: 'png' });
res.type('png');
code.pipe(res);
});
function getToken(secret) {
return speakeasy.time({secret: secret, encoding: 'base32'})
}
function verifyToken(secret, token) {
return speakeasy.time.verify({secret: secret, encoding: 'base32', token: token})
}
var port = process.env.OPENSHIFT_NODEJS_PORT || 8081
, ip = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
app.listen(port, ip);