-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_router.js
More file actions
112 lines (96 loc) · 4.33 KB
/
Copy pathhttp_router.js
File metadata and controls
112 lines (96 loc) · 4.33 KB
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
var express = require("express");
var app = express();
var cookieParser = require('cookie-parser');
app.use(cookieParser());
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var cw_router_lib = require("./cipherwallet/api_router.js");
var cw_router = express.Router();
app.use('/cipherwallet', cw_router);
cw_router_lib.define(cw_router);
/////////////// regular (static and template) files processing
app.use("/css", express.static(__dirname + "/css"));
app.use("/img", express.static(__dirname + "/img"));
app.use("/js", express.static(__dirname + "/js"));
app.get("/your.db", function(rq, rp) {
rp.status(401).send("Unauthorized");
});
app.get("/", function(rq, rp) {
rp.redirect("/cw_index.html");
});
app.get("/cw_index.html", function(rq, rp) {
rp.sendFile(__dirname + "/cw_index.html");
});
/////////////// ajax API
app.post("/login", function(rq, rp) {
// This web service is nothing else than a sample, classic, username-and-password
// login procedure. You probably already have something like this on your website,
// and you are not required to modify it in any way for cipherwallet
var email = rq.body.email.substr(0, 16);
var password = rq.body.password.substr(0, 16);
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database(__dirname + '/your.db');
db.get(
"SELECT firstname, email, password FROM users WHERE email = ?;",
email,
function(err, u) {
if (err) {
console.log(err);
rp.status(404).end(); return;
} else {
if (require("bcrypt-nodejs").compareSync(password, u.password))
rp.send({ 'firstname': u.firstname, 'email': u.email });
else {
rp.status(401).end(); return;
}
}
}
);
});
app.post("/user/:user_id", function(rq, rp) {
// This sample web service is created to look similar to what is called with a POST method
// by your signup web page, when the user presses the "create user" submit button. Form
// data is POSTed from the signup page.
// If data signup page data was loaded from the mobile app (QR code scanning), we also
// register the user to use cipherwallet (QR code scanning) for the logins
// This should mostly be *your* procedure to create an user record, and should work regardless
// of whether cipherwallet is active or not
var firstname = rq.body.firstname.substr(0, 16);
var email = rq.body.email.substr(0, 64);
var password = require("bcrypt-nodejs").hashSync(rq.body.password1.substr(0, 16));
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database(__dirname + '/your.db');
// if the user already exists, delete it (...obviously, you wouldn't do that on your
// real website, right?)
db.run("DELETE FROM users WHERE email = ?;", rq.param('user_id'));
// now add the user in your standard users table
db.run(
"INSERT INTO users(firstname, email, password, created_on) VALUES(?, ?, ?, ?);",
firstname, email, password, parseInt(Date.now() / 1000),
function(err) {
if (err) {
console.log(err);
rp.status(500).end(); return;
} else {
// looks like we successfully created the user record
// =========================== IMPORTANT =================================
// ====== YOU MAY CALL THE set_qr_login_data() FUNCTION DIRECTLY HERE ======
// === just make sure to pass in a tag= parameter in your POST request, ===
// === having the same value as the qrContainerID property; like in our ===
// === example, this would be tag=signup_form_qr, and we would call ===
// === rq.body.user_id = rq.body.email; ===
// === cw_router_lib.set_qr_login_data(rq, rp); ===
// =========================================================================
// for now, we rely on a secondary AJAX request made by the client-side
// javascript with the registerForQRLogin() member function; so we just
// return something nice for the browser
rp.send({ 'firstname': firstname, 'email': email });
}
}
);
});
// definitely find some better server than this...
var HTTP_PORT = 8050
app.listen(HTTP_PORT, "127.0.0.1");
console.log("server started on :" + HTTP_PORT)