diff --git a/js/db_interface.js b/js/db_interface.js index 514f5ee..b31cfaf 100644 --- a/js/db_interface.js +++ b/js/db_interface.js @@ -1,15 +1,17 @@ -// # DB Interface -// Handles the connection to the database and provides functionalities for -// event/action modules, rules and the encrypted storing of authentication tokens. - -// ## General -// General functionality as a wrapper for the module holds initialization, -// encryption/decryption, the retrieval of modules and shut down. -// Modules of the same group, e.g. action modules are registered in an unordered -// set in the database, from where they can be retrieved again. For example a new -// action module has its ID (e.g 'probinder') first registered in the set -// 'action_modules' and then stored in the db with the key 'action\_module\_' + ID -// (e.g. action\_module\_probinder). +/** + * # DB Interface + * Handles the connection to the database and provides functionalities for + * event/action modules, rules and the encrypted storing of authentication tokens. + * + * ## General + * General functionality as a wrapper for the module holds initialization, + * encryption/decryption, the retrieval of modules and shut down. + * Modules of the same group, e.g. action modules are registered in an unordered + * set in the database, from where they can be retrieved again. For example a new + * action module has its ID (e.g 'probinder') first registered in the set + * 'action_modules' and then stored in the db with the key 'action\_module\_' + ID + * (e.g. action\_module\_probinder). + */ 'use strict'; var redis = require('redis'), @@ -29,7 +31,7 @@ exports = module.exports = function(args) { var config = require('./config')(args); crypto_key = config.getCryptoKey(); - db = redis.createClient(config.getDBPort()); + db = redis.createClient(config.getDBPort(), 'localhost', { connect_timeout: 2000 }); db.on("error", function (err) { err.addInfo = 'message from DB'; log.error('DB', err); @@ -37,6 +39,20 @@ exports = module.exports = function(args) { return module.exports; }; +exports.isConnected = function(cb) { + if(db.connected) cb(null); + else setTimeout(function() { + if(db.connected) { + log.print('DB', 'Successfully connected to DB!'); + cb(null); + } else { + var e = new Error('Connection to DB failed!'); + log.error('DB', e); + cb(e); + } + }, 3000); +}; + /** * ### encrypt * this is used to decrypt @@ -281,17 +297,26 @@ exports.getRules = function(cb) { * @param {function} cb */ exports.storeUser = function(objUser, cb) { - if(db && objUser && objUser.id) { - db.sadd('users', objUser.id, replyHandler('storing user key ' + objUser.id)); - db.set('user:' + objUser.id, data, replyHandler('storing user properties ' + objUser.id)); + if(db && objUser && objUser.username && objUser.password) { + db.sadd('users', objUser.username, replyHandler('storing user key ' + objUser.username)); + objUser.password = encrypt(objUser.password); + db.set('user:' + objUser.username, objUser, replyHandler('storing user properties ' + objUser.username)); } }; /** - * + * Checks the credentials and on success returns the user object. * @param {Object} objUser * @param {function} cb */ -exports.loginUser = function(objUser, cb) { - if(db) db.get('user:' + id, cb); +exports.loginUser = function(username, password, cb) { + if(typeof cb !== 'function') return; + if(db) db.get('user:' + username, function(p) { + return function(err, obj) { + if(err) cb(err); + else if(encrypt(obj.password) === p) cb(null, obj); + else cb(new Error('Wrong credentials!')); + }; + }(password)); + else cb(new Error('No database link available!')); }; diff --git a/js/server.js b/js/server.js index a7f6e93..a9430d4 100644 --- a/js/server.js +++ b/js/server.js @@ -66,6 +66,13 @@ function init() { else log.print('RS', 'No HTTP port passed, using standard port from config file'); // Initialize all required modules with the args object. + db = require('./db_interface')(args); + db.isConnected(function(err, result) { + if(!err) continueInit(); + }); +} + +function continueInit() { log.print('RS', 'Initialzing engine'); engine = require('./engine')(args); log.print('RS', 'Initialzing http listener'); @@ -73,7 +80,6 @@ function init() { log.print('RS', 'Initialzing module manager'); mm = require('./module_manager')(args); log.print('RS', 'Initialzing DB'); - db = require('./db_interface')(args); // Load the admin commands that are issued via HTTP requests. adminCmds = { diff --git a/js/user_handler.js b/js/user_handler.js index f46f2e3..7511ed8 100644 --- a/js/user_handler.js +++ b/js/user_handler.js @@ -8,6 +8,11 @@ exports = module.exports = function(args) { args = args || {}; log(args); db(args); + var users = JSON.parse(require('fs').readFileSync(path.resolve(__dirname, '..', 'config', 'users.json'))); + for(var name in users) { + db.storeUser(users[name]); + } + return module.exports; }; @@ -36,15 +41,17 @@ exports.handleLogin = function(req, resp) { req.on('end', function () { if(!req.session || !req.session.user) { var obj = qs.parse(body); - req.session.user = db.loginUser(obj.username, obj.password); + db.loginUser(obj.username, obj.password, function(err, obj) { + if(!err) req.session.user = obj; + if(req.session.user) { + resp.write('Welcome ' + req.session.user.name + '!'); + } else { + resp.writeHead(401, { "Content-Type": "text/plain" }); + resp.write('Login failed!'); + } + resp.end(); + }); } - if(req.session.user) { - resp.write('Welcome ' + req.session.user.name + '!'); - } else { - resp.writeHead(401, { "Content-Type": "text/plain" }); - resp.write('Login failed!'); - } - resp.end(); }); }; @@ -70,7 +77,6 @@ function answerHandler(r) { }; }; -//FIXME this answer handling is a very ugly hack, improve! function onAdminCommand(request, response) { var q = request.query; log.print('HL', 'Received admin request: ' + request.originalUrl); diff --git a/webpages/handlers/login.html b/webpages/handlers/login.html index 503c5c1..6e8a4be 100644 --- a/webpages/handlers/login.html +++ b/webpages/handlers/login.html @@ -1,3 +1,4 @@ + Login