2013-09-13 23:11:00 +02:00
|
|
|
var async = require("async");
|
|
|
|
var cradle = require("cradle");
|
2013-09-13 18:49:44 +02:00
|
|
|
var express = require("express");
|
2013-09-13 23:11:00 +02:00
|
|
|
var fs = require("fs");
|
2013-09-13 18:49:44 +02:00
|
|
|
var http = require("http");
|
|
|
|
var https = require("https");
|
2013-09-13 23:11:00 +02:00
|
|
|
var log4js = require("log4js");
|
2013-09-14 01:18:43 +02:00
|
|
|
var scrypt = require("scrypt");
|
2013-09-13 23:11:00 +02:00
|
|
|
|
2013-09-15 18:30:51 +02:00
|
|
|
//load own tools (tiny functions that help a little ;-)
|
|
|
|
var tools = require("./src/Tools.js");
|
|
|
|
|
2013-09-13 23:11:00 +02:00
|
|
|
//load settings
|
|
|
|
var settings = require("./src/Settings.js");
|
|
|
|
|
|
|
|
//initialize couch connector
|
|
|
|
cradle.setup(settings.couchdb);
|
|
|
|
var couch = new(cradle.Connection);
|
|
|
|
var db = couch.database(settings.couchdb.database);
|
|
|
|
|
|
|
|
db.exists(function(err, exists) {
|
|
|
|
if(err) {
|
2013-09-14 23:31:17 +02:00
|
|
|
console.log("An error occured - could not talk to couchdb:");
|
2013-09-13 23:11:00 +02:00
|
|
|
console.log(err);
|
|
|
|
process.exit(1);
|
|
|
|
} else if(exists == true) {
|
|
|
|
console.log("Database exists :-)");
|
|
|
|
} else {
|
2013-09-14 23:31:17 +02:00
|
|
|
console.log("Database does not exist :-(");
|
2013-09-13 23:11:00 +02:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
});
|
2013-09-13 18:49:44 +02:00
|
|
|
|
2013-09-13 23:11:00 +02:00
|
|
|
//begin setting up the dashboard app
|
2013-09-13 18:49:44 +02:00
|
|
|
var app = express();
|
2013-09-14 23:29:45 +02:00
|
|
|
app.use(express.cookieParser());
|
|
|
|
app.use(express.session({
|
|
|
|
"secret": settings.general.sessionsecret
|
|
|
|
}));
|
2013-09-15 18:30:51 +02:00
|
|
|
app.use(express.bodyParser());
|
2013-09-14 23:29:45 +02:00
|
|
|
|
|
|
|
//some logging for debugging
|
2013-09-13 18:49:44 +02:00
|
|
|
app.use(function(req, res, next) {
|
|
|
|
console.log("%s %s", req.method, req.url);
|
2013-09-14 23:29:45 +02:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
//initialize fresh session
|
|
|
|
app.use(function(req, res, next) {
|
|
|
|
if(req.session.initialized != true) {
|
|
|
|
req.session.initialized = true;
|
2013-09-15 18:30:51 +02:00
|
|
|
req.session.data = {
|
|
|
|
"user": null,
|
|
|
|
"profile": {},
|
|
|
|
"login": false,
|
|
|
|
"lastActivity": new Date()
|
|
|
|
};
|
2013-09-14 23:29:45 +02:00
|
|
|
}
|
2013-09-15 18:30:51 +02:00
|
|
|
console.log(["Session Data", req.session.data]);
|
2013-09-13 18:49:44 +02:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
//deliver static files by default
|
2013-09-14 23:32:59 +02:00
|
|
|
app.use("/", express.static(__dirname + '/static'));
|
2013-09-13 18:49:44 +02:00
|
|
|
|
2013-09-14 23:29:45 +02:00
|
|
|
//API: /session
|
|
|
|
app.use("/session", function(req, res) {
|
|
|
|
res.setHeader("Content-Type", "application/json");
|
|
|
|
|
2013-09-16 18:21:52 +02:00
|
|
|
//refresh session and return login status
|
2013-09-14 23:29:45 +02:00
|
|
|
if(req.method == "GET") {
|
2013-09-15 18:30:51 +02:00
|
|
|
if(req.session.data.login == true) {
|
|
|
|
if(new Date() - req.session.data.lastActivity < 5 * 60 * 1000) {
|
|
|
|
req.session.data.lastActivity = new Date();
|
2013-09-14 23:29:45 +02:00
|
|
|
} else {
|
2013-09-15 18:30:51 +02:00
|
|
|
req.session.data.login = false;
|
2013-09-14 23:29:45 +02:00
|
|
|
}
|
2013-09-14 23:40:48 +02:00
|
|
|
}
|
2013-09-14 23:29:45 +02:00
|
|
|
res.send(200, JSON.stringify({
|
2013-09-16 18:21:52 +02:00
|
|
|
"success": true,
|
2013-09-15 18:30:51 +02:00
|
|
|
"login": req.session.data.login
|
2013-09-14 23:29:45 +02:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
//check user credentials, update session data
|
|
|
|
if(req.method == "PUT") {
|
2013-09-16 18:21:52 +02:00
|
|
|
//already logged in?
|
2013-09-15 18:30:51 +02:00
|
|
|
if(req.session.data.login == true) {
|
|
|
|
res.send(200, JSON.stringify({
|
2013-09-16 18:21:52 +02:00
|
|
|
"success": false,
|
|
|
|
"error": "You are already logged in!"
|
2013-09-15 18:30:51 +02:00
|
|
|
}));
|
2013-09-16 18:21:52 +02:00
|
|
|
return;
|
2013-09-15 18:30:51 +02:00
|
|
|
}
|
2013-09-16 18:21:52 +02:00
|
|
|
|
2013-09-15 18:30:51 +02:00
|
|
|
var params = req.body;
|
2013-09-16 18:21:52 +02:00
|
|
|
//username or password missing?
|
|
|
|
if(tools.reqParamsGiven(["username", "password"], params) == false) {
|
2013-09-15 18:30:51 +02:00
|
|
|
res.send(200, JSON.stringify({
|
2013-09-16 18:21:52 +02:00
|
|
|
"success": false,
|
|
|
|
"error": "Insufficient parameters given! Need: username, password"
|
2013-09-15 18:30:51 +02:00
|
|
|
}));
|
|
|
|
return;
|
|
|
|
}
|
2013-09-16 18:21:52 +02:00
|
|
|
//check if user exists
|
|
|
|
db.get(params.username, function (err, doc) {
|
|
|
|
if(!err && doc.type == "user") {
|
|
|
|
//user exists, verify password
|
|
|
|
scrypt.verifyHash(user.auth, params.password, function(err, match) {
|
|
|
|
if(err || match == false) {
|
|
|
|
res.send(200, JSON.stringify({
|
|
|
|
"success": false,
|
|
|
|
"error": "Invalid login credentials!"
|
|
|
|
}));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(!err && match == true) {
|
|
|
|
req.session.data.login = true;
|
|
|
|
req.session.data.lastActivity = new Date();
|
|
|
|
res.send(200, JSON.stringify({
|
|
|
|
"success": true
|
|
|
|
}));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
//user does not exist.
|
|
|
|
res.send(200, JSON.stringify({
|
|
|
|
"success": false,
|
|
|
|
"error": "Invalid login credentials!"
|
|
|
|
}));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
2013-09-14 23:29:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//destroy the session
|
|
|
|
if(req.method == "DELETE") {
|
2013-09-16 18:21:52 +02:00
|
|
|
//only do logout if login exists
|
|
|
|
if(req.session.data.login == false) {
|
|
|
|
res.send(200, JSON.stringify({
|
|
|
|
"success": false,
|
|
|
|
"error": "Cannot log you out, you are not logged in!"
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
req.session.data.login = false;
|
|
|
|
res.send(200, JSON.stringify({
|
|
|
|
"success": true
|
|
|
|
}));
|
|
|
|
}
|
2013-09-14 23:29:45 +02:00
|
|
|
}
|
2013-09-13 18:49:44 +02:00
|
|
|
});
|
|
|
|
|
2013-09-15 18:30:51 +02:00
|
|
|
//API: /user
|
|
|
|
app.use("/user", function(req, res) {
|
|
|
|
res.setHeader("Content-Type", "application/json");
|
|
|
|
if(req.method == "PUT") {
|
|
|
|
var params = req.body;
|
|
|
|
if(tools.reqParamsGiven(["username", "password", "email"], params) == false) {
|
|
|
|
res.send(500, JSON.stringify({
|
|
|
|
"success": false,
|
|
|
|
"err": "This method needs username, password and email!"
|
|
|
|
}));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//check if user already exists
|
|
|
|
db.get(params.username, function (err, doc) {
|
|
|
|
if(!err || err.error != "not_found" || err.reason != "missing") {
|
|
|
|
res.send(200, JSON.stringify({
|
|
|
|
"success": false,
|
|
|
|
"err": "Username already taken!"
|
|
|
|
}));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//get: {"0":{"error":"not_found","reason":"missing"}}
|
|
|
|
scrypt.passwordHash(params.password, 10, function(err, pwHash) {
|
|
|
|
var userDoc = {
|
|
|
|
"_id": params.username,
|
|
|
|
"auth": pwHash,
|
|
|
|
"email": params.email,
|
|
|
|
"type": "user"
|
|
|
|
};
|
|
|
|
db.save(userDoc._id, userDoc, function(err, result) {
|
|
|
|
if(err) {
|
|
|
|
res.send(200, JSON.stringify({
|
|
|
|
"success": false,
|
|
|
|
"err": err
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
res.send(200, JSON.stringify({
|
|
|
|
"success": true
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if(req.method == "GET") {
|
|
|
|
res.send(200, JSON.stringify(req.session.data.user));
|
|
|
|
}
|
|
|
|
if(req.method == "POST") {
|
|
|
|
console.log(req);
|
|
|
|
}
|
|
|
|
if(req.method == "DELETE") {
|
|
|
|
//verify credentials before erasing all data
|
|
|
|
console.log(req);
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-09-14 23:31:17 +02:00
|
|
|
//define 404 for everything else or 500 on error (ugly but i think it's useful)
|
|
|
|
app.use(function(err, req, res, next) {
|
|
|
|
if(err) {
|
|
|
|
console.log(err.stack);
|
|
|
|
res.send(500, "Oops, an error occured.");
|
|
|
|
} else {
|
|
|
|
res.send(404, "Sorry, nothing here.");
|
|
|
|
}
|
|
|
|
res.end();
|
2013-09-13 18:49:44 +02:00
|
|
|
});
|
|
|
|
|
2013-09-14 23:32:59 +02:00
|
|
|
//fire it up as https (or http - NOT recommended(!)) server
|
2013-09-13 23:11:00 +02:00
|
|
|
if(settings.general.https == true) {
|
|
|
|
var httpsOptions = {
|
|
|
|
"cert": fs.readFileSync(settings.https.cert),
|
|
|
|
"key": fs.readFileSync(settings.https.key)
|
|
|
|
};
|
2013-09-14 14:32:08 +02:00
|
|
|
https.createServer(httpsOptions, app).listen(settings.general.listen.port, settings.general.listen.host);
|
2013-09-13 23:11:00 +02:00
|
|
|
} else {
|
2013-09-14 14:32:08 +02:00
|
|
|
http.createServer(app).listen(settings.general.listen.port, settings.general.listen.host);
|
2013-09-13 23:11:00 +02:00
|
|
|
}
|