* User stuff converted to use Async.js. More to come

This commit is contained in:
NuSkooler 2014-10-25 21:35:42 -06:00
parent 668fdd9166
commit 46875ccddd
3 changed files with 223 additions and 57 deletions

View File

@ -29,6 +29,8 @@ EditTextView.prototype.onKeyPress = function(key, isSpecial) {
assert(1 === key.length); assert(1 === key.length);
console.log('key: ' + key);
if(this.text.length < this.options.maxLength) { if(this.text.length < this.options.maxLength) {
key = strUtil.stylizeString(key, this.textStyle); key = strUtil.stylizeString(key, this.textStyle);

View File

@ -4,12 +4,13 @@
var userDb = require('./database.js').dbs.user; var userDb = require('./database.js').dbs.user;
var crypto = require('crypto'); var crypto = require('crypto');
var assert = require('assert'); var assert = require('assert');
var async = require('async');
exports.User = User; exports.User = User;
exports.getUserId = getUserId; exports.getUserId = getUserId;
exports.createNew = createNew; exports.createNew = createNew;
exports.generatePasswordDerivedKey = generatePasswordDerivedKey;
exports.persistAll = persistAll; exports.persistAll = persistAll;
exports.authenticate = authenticate;
function User() { function User() {
var self = this; var self = this;
@ -47,6 +48,10 @@ User.PBKDF2 = {
saltLen : 32, saltLen : 32,
}; };
User.StandardPropertyGroups = {
password : [ 'pw_pbkdf2_salt', 'pw_pbkdf2_dk' ],
};
function getUserId(userName, cb) { function getUserId(userName, cb) {
userDb.get( userDb.get(
'SELECT id ' + 'SELECT id ' +
@ -54,7 +59,15 @@ function getUserId(userName, cb) {
'WHERE user_name LIKE ?;', 'WHERE user_name LIKE ?;',
[ userName ], [ userName ],
function onResults(err, row) { function onResults(err, row) {
cb(err, row.id); if(err) {
cb(err);
} else {
if(row) {
cb(null, row.id);
} else {
cb(new Error('No matching user name'));
}
}
} }
); );
} }
@ -62,59 +75,108 @@ function getUserId(userName, cb) {
function createNew(user, cb) { function createNew(user, cb) {
assert(user.userName && user.userName.length > 1, 'Invalid userName'); assert(user.userName && user.userName.length > 1, 'Invalid userName');
async.series(
[
function beginTransaction(callback) {
userDb.run('BEGIN;', function onBegin(err) {
callback(err);
});
},
function createUserRec(callback) {
userDb.run( userDb.run(
'INSERT INTO user (user_name) ' + 'INSERT INTO user (user_name) ' +
'VALUES (?);', 'VALUES (?);',
[ user.userName ], [ user.userName ],
function onUserInsert(err) { function onUserInsert(err) {
if(err) { if(err) {
cb(err); callback(err);
} else { } else {
user.id = this.lastID; user.id = this.lastID;
callback(null);
// }
// Allow converting user.password -> Salt/DK }
// );
},
function genPasswordDkAndSaltIfRequired(callback) {
if(user.password && user.password.length > 0) { if(user.password && user.password.length > 0) {
generatePasswordDerivedKey(user.password, function onDkGenerated(err, dk) { generatePasswordDerivedKeyAndSalt(user.password, function onDkAndSalt(err, info) {
user.properties = user.properties || { if(err) {
pw_pbkdf2_salt : dk.salt, callback(err);
pw_pbkdf2_dk : dk.dk, } else {
}; user.properties = user.properties || {};
user.properties.pw_pbkdf2_salt = info.salt;
persistAll(user, function onUserPersisted() { user.properties.pw_pbkdf2_dk = info.dk;
cb(null, user.id); callback(null);
}); }
}); });
} else { } else {
persistAll(user, function onUserPersisted() { callback(null);
cb(null, user.id); }
},
function saveAll(callback) {
persistAll(user, false, function onPersisted(err) {
callback(err);
}); });
} }
],
function onComplete(err) {
if(err) {
userDb.run('ROLLBACK;', function onRollback(err) {
cb(err);
});
} else {
userDb.run('COMMIT;', function onCommit(err) {
if(err) {
cb(err);
} else {
cb(null, user.id);
}
});
} }
} }
); );
} }
function generatePasswordDerivedKey(password, cb) { function generatePasswordDerivedKeyAndSalt(password, cb) {
crypto.randomBytes(User.PBKDF2.saltLen, function onRandomSalt(err, salt) { async.waterfall(
if(err) { [
cb(err); function getSalt(callback) {
return; generatePasswordDerivedKeySalt(function onSalt(err, salt) {
callback(err, salt);
});
},
function getDk(salt, callback) {
generatePasswordDerivedKey(password, salt, function onDk(err, dk) {
callback(err, salt, dk);
});
}
],
function onComplete(err, salt, dk) {
cb(err, { salt : salt, dk : dk });
}
);
} }
salt = salt.toString('hex'); function generatePasswordDerivedKeySalt(cb) {
crypto.randomBytes(User.PBKDF2.saltLen, function onRandSalt(err, salt) {
if(err) {
cb(err);
} else {
cb(null, salt.toString('hex'));
}
});
}
function generatePasswordDerivedKey(password, salt, cb) {
password = new Buffer(password).toString('hex'); password = new Buffer(password).toString('hex');
crypto.pbkdf2(password, salt, User.PBKDF2.iterations, User.PBKDF2.keyLen, function onDerivedKey(err, dk) { crypto.pbkdf2(password, salt, User.PBKDF2.iterations, User.PBKDF2.keyLen, function onDerivedKey(err, dk) {
if(err) { if(err) {
cb(err); cb(err);
} else { } else {
cb(null, { dk : dk.toString('hex'), salt : salt } ); cb(null, dk.toString('hex'));
} }
}); });
});
} }
function persistProperties(user, cb) { function persistProperties(user, cb) {
@ -124,27 +186,125 @@ function persistProperties(user, cb) {
'REPLACE INTO user_property (user_id, prop_name, prop_value) ' + 'REPLACE INTO user_property (user_id, prop_name, prop_value) ' +
'VALUES (?, ?, ?);'); 'VALUES (?, ?, ?);');
Object.keys(user.properties).forEach(function onProp(name) { async.each(Object.keys(user.properties), function onProp(propName, callback) {
stmt.run(user.id, name, user.properties[name]); stmt.run(user.id, propName, user.properties[propName], function onRun(err) {
callback(err);
}); });
}, function onComplete(err) {
if(err) {
cb(err);
} else {
stmt.finalize(function onFinalized() { stmt.finalize(function onFinalized() {
if(cb) { cb(null);
cb(); });
} }
}); });
} }
function persistAll(user, cb) { function getProperties(userId, propNames, cb) {
var properties = {};
async.each(propNames, function onPropName(propName, next) {
userDb.get(
'SELECT prop_value ' +
'FROM user_property ' +
'WHERE user_id = ? AND prop_name = ?;',
[ userId, propName ],
function onRow(err, row) {
if(err) {
next(err);
} else {
if(row) {
properties[propName] = row.prop_value;
next();
} else {
next(new Error('No property "' + propName + '" for user ' + userId));
}
}
}
);
}, function onCompleteOrError(err) {
if(err) {
cb(err);
} else {
cb(null, properties);
}
});
}
function persistAll(user, useTransaction, cb) {
assert(user.id > 0); assert(user.id > 0);
userDb.serialize(function onSerialized() { async.series(
userDb.run('BEGIN;'); [
function beginTransaction(callback) {
persistProperties(user); if(useTransaction) {
userDb.run('BEGIN;', function onBegin(err) {
userDb.run('COMMIT;'); callback(err);
}); });
} else {
cb(); callback(null);
}
},
function saveProps(callback) {
persistProperties(user, function onPropPersist(err) {
callback(err);
});
}
],
function onComplete(err) {
if(err) {
if(useTransaction) {
userDb.run('ROLLBACK;', function onRollback(err) {
cb(err);
});
} else {
cb(err);
}
} else {
if(useTransaction) {
userDb.run('COMMIT;', function onCommit(err) {
cb(err);
});
} else {
cb(null);
}
}
}
);
}
function authenticate(userName, password, client, cb) {
assert(client);
async.waterfall(
[
function fetchUserId(callback) {
// get user ID
getUserId(userName, function onUserId(err, userId) {
callback(err, userId);
});
},
function getRequiredAuthProperties(userId, callback) {
// fetch properties required for authentication
getProperties(userId, User.StandardPropertyGroups.password, function onProps(err, props) {
callback(err, props);
});
},
function getDkWithSalt(props, callback) {
// get DK from stored salt and password provided
generatePasswordDerivedKey(password, props.pw_pbkdf2_salt, function onDk(err, dk) {
callback(err, dk, props.pw_pbkdf2_dk);
});
}
],
function validateAuth(err, passDk, propsDk) {
if(err) {
cb(false);
} else {
cb(passDk === propsDk);
}
}
);
} }

View File

@ -60,8 +60,12 @@ function entryPoint(client) {
etv.redraw();*/ etv.redraw();*/
user.authenticate('NuSkooler', 'password', client, function onAuth(isValid) {
console.log(isValid);
});
user.createNew({ user.createNew({
userName : 'Scooby', userName : 'NuSkooler',
password : 'password', password : 'password',
//properties : { //properties : {
// pw_pbkdf2_salt : '81b45dc699c716ac1913039138b64e3057844128cf1f9291c6475d26dab3d4a5', // pw_pbkdf2_salt : '81b45dc699c716ac1913039138b64e3057844128cf1f9291c6475d26dab3d4a5',