-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
64 lines (53 loc) · 1.66 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
53
54
55
56
57
58
59
60
61
62
63
64
var express = require('express');
var app = express();
var speakeasy = require("speakeasy");
var sdk = require('messagemedia-messages-sdk');
var controller = sdk.MessagesController;
const ejs = require('ejs');
var bodyParser = require("body-parser");
app.set('views', __dirname + '/views');
app.set('view engine', 'html');
app.engine('html', ejs.renderFile);
app.use(express.static(__dirname + '/public'));
var jsonParser = bodyParser.json()
var urlencodedParser = bodyParser.urlencoded({ extended: false })
var secret = speakeasy.generateSecret({length: 20});
app.get('/', function(req, res){
res.render('index');
});
app.post("/register", urlencodedParser, function (req, res) {
var token = speakeasy.totp({
secret: secret.base32,
encoding: 'base32'
});
sdk.Configuration.basicAuthUserName = process.env.KEY;
sdk.Configuration.basicAuthPassword = process.env.SECRET;
var body = new sdk.SendMessagesRequest({
"messages":[
{
"content":"Your MessageMedia security code is: "+token,
"destination_number":req.body.mobile
}
]
});
controller.createSendMessages(body, function(error, response, context) {
if (!error){
res.render("verify");
} else {
res.send(error);
}
});
});
app.post("/verify", urlencodedParser, function (req, res) {
var userCode = req.body.code;
var tokenValidates = speakeasy.totp.verify({
secret: secret.base32,
encoding: 'base32',
token: userCode,
window: 6
});
res.send(tokenValidates);
});
var listener = app.listen(process.env.PORT, function() {
console.log('Your app is listening on port ' + listener.address().port);
});