user handling improved

This commit is contained in:
Dominic Bosch 2013-11-19 13:51:30 +01:00
parent 33318f4cba
commit e3fb282dd3
4 changed files with 67 additions and 29 deletions

View file

@ -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!'));
};

View file

@ -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 = {

View file

@ -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);

View file

@ -1,3 +1,4 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Login</title>