2014-10-20 05:30:44 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
const userDb = require('./database.js').dbs.user;
|
|
|
|
const Config = require('./config.js').config;
|
|
|
|
const userGroup = require('./user_group.js');
|
|
|
|
const Errors = require('./enig_error.js').Errors;
|
2018-06-04 01:58:05 +00:00
|
|
|
const Events = require('./events.js');
|
2017-02-18 20:21:18 +00:00
|
|
|
|
|
|
|
// deps
|
|
|
|
const crypto = require('crypto');
|
|
|
|
const assert = require('assert');
|
|
|
|
const async = require('async');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const moment = require('moment');
|
2014-10-21 04:47:13 +00:00
|
|
|
|
2016-08-30 03:03:48 +00:00
|
|
|
exports.isRootUserId = function(id) { return 1 === id; };
|
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
module.exports = class User {
|
|
|
|
constructor() {
|
|
|
|
this.userId = 0;
|
|
|
|
this.username = '';
|
|
|
|
this.properties = {}; // name:value
|
|
|
|
this.groups = []; // group membership(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// static property accessors
|
|
|
|
static get RootUserID() {
|
|
|
|
return 1;
|
|
|
|
}
|
2014-10-20 05:30:44 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
static get PBKDF2() {
|
|
|
|
return {
|
|
|
|
iterations : 1000,
|
|
|
|
keyLen : 128,
|
|
|
|
saltLen : 32,
|
|
|
|
};
|
|
|
|
}
|
2014-10-21 04:47:13 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
static get StandardPropertyGroups() {
|
|
|
|
return {
|
|
|
|
password : [ 'pw_pbkdf2_salt', 'pw_pbkdf2_dk' ],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static get AccountStatus() {
|
|
|
|
return {
|
|
|
|
disabled : 0,
|
|
|
|
inactive : 1,
|
|
|
|
active : 2,
|
|
|
|
};
|
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
isAuthenticated() {
|
|
|
|
return true === this.authenticated;
|
|
|
|
}
|
2016-01-30 22:26:19 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
isValid() {
|
|
|
|
if(this.userId <= 0 || this.username.length < Config.users.usernameMin) {
|
2014-10-24 04:18:38 +00:00
|
|
|
return false;
|
2014-10-21 04:47:13 +00:00
|
|
|
}
|
|
|
|
|
2014-10-24 04:18:38 +00:00
|
|
|
return this.hasValidPassword();
|
2017-02-18 20:21:18 +00:00
|
|
|
}
|
2014-10-21 04:47:13 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
hasValidPassword() {
|
2014-10-24 04:18:38 +00:00
|
|
|
if(!this.properties || !this.properties.pw_pbkdf2_salt || !this.properties.pw_pbkdf2_dk) {
|
|
|
|
return false;
|
2014-10-21 04:47:13 +00:00
|
|
|
}
|
2014-10-20 05:30:44 +00:00
|
|
|
|
2018-06-03 23:59:16 +00:00
|
|
|
return ((this.properties.pw_pbkdf2_salt.length === User.PBKDF2.saltLen * 2) &&
|
|
|
|
(this.properties.pw_pbkdf2_dk.length === User.PBKDF2.keyLen * 2));
|
2017-02-18 20:21:18 +00:00
|
|
|
}
|
2014-10-20 05:30:44 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
isRoot() {
|
|
|
|
return User.isRootUserId(this.userId);
|
|
|
|
}
|
2014-10-21 04:47:13 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
isSysOp() { // alias to isRoot()
|
|
|
|
return this.isRoot();
|
|
|
|
}
|
2015-04-06 06:18:08 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
isGroupMember(groupNames) {
|
2015-11-05 06:04:55 +00:00
|
|
|
if(_.isString(groupNames)) {
|
|
|
|
groupNames = [ groupNames ];
|
|
|
|
}
|
|
|
|
|
2018-01-15 19:22:11 +00:00
|
|
|
const isMember = groupNames.some(gn => (-1 !== this.groups.indexOf(gn)));
|
2016-01-30 22:26:19 +00:00
|
|
|
return isMember;
|
2017-02-18 20:21:18 +00:00
|
|
|
}
|
2015-05-12 22:34:11 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
getLegacySecurityLevel() {
|
|
|
|
if(this.isRoot() || this.isGroupMember('sysops')) {
|
2015-08-05 04:35:59 +00:00
|
|
|
return 100;
|
2016-09-01 04:06:49 +00:00
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
if(this.isGroupMember('users')) {
|
2015-08-05 04:35:59 +00:00
|
|
|
return 30;
|
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2016-09-01 04:06:49 +00:00
|
|
|
return 10; // :TODO: Is this what we want?
|
2017-02-18 20:21:18 +00:00
|
|
|
}
|
2014-10-26 03:35:42 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
authenticate(username, password, cb) {
|
|
|
|
const self = this;
|
|
|
|
const cachedInfo = {};
|
2015-04-16 04:46:45 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function fetchUserId(callback) {
|
|
|
|
// get user ID
|
|
|
|
User.getUserIdAndName(username, (err, uid, un) => {
|
|
|
|
cachedInfo.userId = uid;
|
|
|
|
cachedInfo.username = un;
|
2015-08-03 00:27:05 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function getRequiredAuthProperties(callback) {
|
|
|
|
// fetch properties required for authentication
|
|
|
|
User.loadProperties(cachedInfo.userId, { names : User.StandardPropertyGroups.password }, (err, props) => {
|
|
|
|
return callback(err, props);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function getDkWithSalt(props, callback) {
|
|
|
|
// get DK from stored salt and password provided
|
|
|
|
User.generatePasswordDerivedKey(password, props.pw_pbkdf2_salt, (err, dk) => {
|
|
|
|
return callback(err, dk, props.pw_pbkdf2_dk);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function validateAuth(passDk, propsDk, callback) {
|
|
|
|
//
|
|
|
|
// Use constant time comparison here for security feel-goods
|
|
|
|
//
|
2018-04-28 12:59:07 +00:00
|
|
|
const passDkBuf = Buffer.from(passDk, 'hex');
|
|
|
|
const propsDkBuf = Buffer.from(propsDk, 'hex');
|
2017-02-18 20:21:18 +00:00
|
|
|
|
|
|
|
if(passDkBuf.length !== propsDkBuf.length) {
|
|
|
|
return callback(Errors.AccessDenied('Invalid password'));
|
|
|
|
}
|
2015-04-06 06:18:08 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
let c = 0;
|
|
|
|
for(let i = 0; i < passDkBuf.length; i++) {
|
|
|
|
c |= passDkBuf[i] ^ propsDkBuf[i];
|
|
|
|
}
|
2015-04-06 06:18:08 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
return callback(0 === c ? null : Errors.AccessDenied('Invalid password'));
|
|
|
|
},
|
|
|
|
function initProps(callback) {
|
|
|
|
User.loadProperties(cachedInfo.userId, (err, allProps) => {
|
|
|
|
if(!err) {
|
|
|
|
cachedInfo.properties = allProps;
|
|
|
|
}
|
2015-04-06 06:18:08 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function initGroups(callback) {
|
|
|
|
userGroup.getGroupsForUser(cachedInfo.userId, (err, groups) => {
|
|
|
|
if(!err) {
|
|
|
|
cachedInfo.groups = groups;
|
|
|
|
}
|
2015-04-06 06:18:08 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
return callback(err);
|
|
|
|
});
|
2015-04-06 06:18:08 +00:00
|
|
|
}
|
2017-02-18 20:21:18 +00:00
|
|
|
],
|
|
|
|
err => {
|
|
|
|
if(!err) {
|
|
|
|
self.userId = cachedInfo.userId;
|
|
|
|
self.username = cachedInfo.username;
|
|
|
|
self.properties = cachedInfo.properties;
|
|
|
|
self.groups = cachedInfo.groups;
|
|
|
|
self.authenticated = true;
|
2015-04-06 06:18:08 +00:00
|
|
|
}
|
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
return cb(err);
|
2015-04-06 06:18:08 +00:00
|
|
|
}
|
2017-02-18 20:21:18 +00:00
|
|
|
);
|
|
|
|
}
|
2015-04-06 06:18:08 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
create(password, cb) {
|
|
|
|
assert(0 === this.userId);
|
2015-04-06 06:18:08 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
if(this.username.length < Config.users.usernameMin || this.username.length > Config.users.usernameMax) {
|
|
|
|
return cb(Errors.Invalid('Invalid username length'));
|
|
|
|
}
|
2015-04-10 04:49:56 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
const self = this;
|
2015-04-10 04:49:56 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
// :TODO: set various defaults, e.g. default activation status, etc.
|
|
|
|
self.properties.account_status = Config.users.requireActivation ? User.AccountStatus.inactive : User.AccountStatus.active;
|
2015-04-16 04:46:45 +00:00
|
|
|
|
2017-09-16 23:13:11 +00:00
|
|
|
async.waterfall(
|
2017-02-18 20:21:18 +00:00
|
|
|
[
|
|
|
|
function beginTransaction(callback) {
|
2017-09-16 23:13:11 +00:00
|
|
|
return userDb.beginTransaction(callback);
|
2017-02-18 20:21:18 +00:00
|
|
|
},
|
2017-09-16 23:13:11 +00:00
|
|
|
function createUserRec(trans, callback) {
|
|
|
|
trans.run(
|
2017-02-18 20:21:18 +00:00
|
|
|
`INSERT INTO user (user_name)
|
|
|
|
VALUES (?);`,
|
|
|
|
[ self.username ],
|
|
|
|
function inserted(err) { // use classic function for |this|
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2015-04-10 04:49:56 +00:00
|
|
|
self.userId = this.lastID;
|
2015-04-16 04:46:45 +00:00
|
|
|
|
2015-05-12 22:34:11 +00:00
|
|
|
// Do not require activation for userId 1 (root/admin)
|
2017-02-18 20:21:18 +00:00
|
|
|
if(User.RootUserID === self.userId) {
|
2015-04-16 04:46:45 +00:00
|
|
|
self.properties.account_status = User.AccountStatus.active;
|
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2017-09-16 23:13:11 +00:00
|
|
|
return callback(null, trans);
|
2015-04-10 04:49:56 +00:00
|
|
|
}
|
2017-02-18 20:21:18 +00:00
|
|
|
);
|
|
|
|
},
|
2017-09-16 23:13:11 +00:00
|
|
|
function genAuthCredentials(trans, callback) {
|
2017-02-18 20:21:18 +00:00
|
|
|
User.generatePasswordDerivedKeyAndSalt(password, (err, info) => {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2015-04-10 04:49:56 +00:00
|
|
|
self.properties.pw_pbkdf2_salt = info.salt;
|
|
|
|
self.properties.pw_pbkdf2_dk = info.dk;
|
2017-09-16 23:13:11 +00:00
|
|
|
return callback(null, trans);
|
2017-02-18 20:21:18 +00:00
|
|
|
});
|
|
|
|
},
|
2017-09-16 23:13:11 +00:00
|
|
|
function setInitialGroupMembership(trans, callback) {
|
2017-02-18 20:21:18 +00:00
|
|
|
self.groups = Config.users.defaultGroups;
|
|
|
|
|
|
|
|
if(User.RootUserID === self.userId) { // root/SysOp?
|
|
|
|
self.groups.push('sysops');
|
2015-04-10 04:49:56 +00:00
|
|
|
}
|
2015-08-21 04:29:16 +00:00
|
|
|
|
2017-09-16 23:13:11 +00:00
|
|
|
return callback(null, trans);
|
2017-02-18 20:21:18 +00:00
|
|
|
},
|
2017-09-16 23:13:11 +00:00
|
|
|
function saveAll(trans, callback) {
|
|
|
|
self.persistWithTransaction(trans, err => {
|
|
|
|
return callback(err, trans);
|
2017-02-18 20:21:18 +00:00
|
|
|
});
|
2018-06-04 01:58:05 +00:00
|
|
|
},
|
|
|
|
function sendEvent(trans, callback) {
|
|
|
|
Events.emit(Events.getSystemEvents().NewUser, { user : self });
|
|
|
|
return callback(null, trans);
|
2017-02-18 20:21:18 +00:00
|
|
|
}
|
|
|
|
],
|
2017-09-16 23:13:11 +00:00
|
|
|
(err, trans) => {
|
|
|
|
if(trans) {
|
|
|
|
trans[err ? 'rollback' : 'commit'](transErr => {
|
|
|
|
return cb(err ? err : transErr);
|
2017-02-18 20:21:18 +00:00
|
|
|
});
|
|
|
|
} else {
|
2017-09-16 23:13:11 +00:00
|
|
|
return cb(err);
|
2015-05-12 22:34:11 +00:00
|
|
|
}
|
2015-04-10 04:49:56 +00:00
|
|
|
}
|
2017-02-18 20:21:18 +00:00
|
|
|
);
|
|
|
|
}
|
2015-04-10 04:49:56 +00:00
|
|
|
|
2017-09-16 23:13:11 +00:00
|
|
|
persistWithTransaction(trans, cb) {
|
2017-02-18 20:21:18 +00:00
|
|
|
assert(this.userId > 0);
|
2015-04-10 04:49:56 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
const self = this;
|
2015-04-15 04:27:07 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function saveProps(callback) {
|
2017-09-16 23:13:11 +00:00
|
|
|
self.persistProperties(self.properties, trans, err => {
|
2017-02-18 20:21:18 +00:00
|
|
|
return callback(err);
|
2015-04-10 04:49:56 +00:00
|
|
|
});
|
2017-02-18 20:21:18 +00:00
|
|
|
},
|
|
|
|
function saveGroups(callback) {
|
2017-09-16 23:13:11 +00:00
|
|
|
userGroup.addUserToGroups(self.userId, self.groups, trans, err => {
|
2017-02-18 20:21:18 +00:00
|
|
|
return callback(err);
|
2015-04-10 04:49:56 +00:00
|
|
|
});
|
|
|
|
}
|
2017-02-18 20:21:18 +00:00
|
|
|
],
|
|
|
|
err => {
|
2017-09-16 23:13:11 +00:00
|
|
|
return cb(err);
|
2015-04-10 04:49:56 +00:00
|
|
|
}
|
2017-02-18 20:21:18 +00:00
|
|
|
);
|
|
|
|
}
|
2015-04-10 04:49:56 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
persistProperty(propName, propValue, cb) {
|
|
|
|
// update live props
|
|
|
|
this.properties[propName] = propValue;
|
|
|
|
|
|
|
|
userDb.run(
|
|
|
|
`REPLACE INTO user_property (user_id, prop_name, prop_value)
|
2018-01-15 19:22:11 +00:00
|
|
|
VALUES (?, ?, ?);`,
|
|
|
|
[ this.userId, propName, propValue ],
|
2017-02-18 20:21:18 +00:00
|
|
|
err => {
|
|
|
|
if(cb) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2015-08-18 03:45:11 +00:00
|
|
|
}
|
2017-02-18 20:21:18 +00:00
|
|
|
);
|
|
|
|
}
|
2016-12-07 01:58:56 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
removeProperty(propName, cb) {
|
|
|
|
// update live
|
|
|
|
delete this.properties[propName];
|
|
|
|
|
|
|
|
userDb.run(
|
|
|
|
`DELETE FROM user_property
|
|
|
|
WHERE user_id = ? AND prop_name = ?;`,
|
|
|
|
[ this.userId, propName ],
|
|
|
|
err => {
|
|
|
|
if(cb) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2016-12-07 01:58:56 +00:00
|
|
|
}
|
2017-02-18 20:21:18 +00:00
|
|
|
);
|
|
|
|
}
|
2015-07-26 06:20:07 +00:00
|
|
|
|
2017-09-16 23:13:11 +00:00
|
|
|
persistProperties(properties, transOrDb, cb) {
|
|
|
|
if(!_.isFunction(cb) && _.isFunction(transOrDb)) {
|
|
|
|
cb = transOrDb;
|
|
|
|
transOrDb = userDb;
|
|
|
|
}
|
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
const self = this;
|
2015-08-17 03:47:33 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
// update live props
|
|
|
|
_.merge(this.properties, properties);
|
2015-08-17 03:47:33 +00:00
|
|
|
|
2017-09-16 23:13:11 +00:00
|
|
|
const stmt = transOrDb.prepare(
|
2017-02-18 20:21:18 +00:00
|
|
|
`REPLACE INTO user_property (user_id, prop_name, prop_value)
|
|
|
|
VALUES (?, ?, ?);`
|
|
|
|
);
|
2015-08-17 03:47:33 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
async.each(Object.keys(properties), (propName, nextProp) => {
|
|
|
|
stmt.run(self.userId, propName, properties[propName], err => {
|
|
|
|
return nextProp(err);
|
2015-08-17 03:47:33 +00:00
|
|
|
});
|
2017-02-18 20:21:18 +00:00
|
|
|
},
|
|
|
|
err => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
stmt.finalize( () => {
|
|
|
|
return cb(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2015-12-24 18:54:55 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
setNewAuthCredentials(password, cb) {
|
|
|
|
User.generatePasswordDerivedKeyAndSalt(password, (err, info) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
const newProperties = {
|
2015-12-24 18:54:55 +00:00
|
|
|
pw_pbkdf2_salt : info.salt,
|
|
|
|
pw_pbkdf2_dk : info.dk,
|
|
|
|
};
|
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
this.persistProperties(newProperties, err => {
|
|
|
|
return cb(err);
|
2015-12-24 18:54:55 +00:00
|
|
|
});
|
2017-02-18 20:21:18 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getAge() {
|
|
|
|
if(_.has(this.properties, 'birthdate')) {
|
|
|
|
return moment().diff(this.properties.birthdate, 'years');
|
2015-12-24 18:54:55 +00:00
|
|
|
}
|
2017-02-18 20:21:18 +00:00
|
|
|
}
|
2015-12-24 18:54:55 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
static getUser(userId, cb) {
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function fetchUserId(callback) {
|
|
|
|
User.getUserName(userId, (err, userName) => {
|
|
|
|
return callback(null, userName);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function initProps(userName, callback) {
|
|
|
|
User.loadProperties(userId, (err, properties) => {
|
|
|
|
return callback(err, userName, properties);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function initGroups(userName, properties, callback) {
|
|
|
|
userGroup.getGroupsForUser(userId, (err, groups) => {
|
|
|
|
return callback(null, userName, properties, groups);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
],
|
|
|
|
(err, userName, properties, groups) => {
|
|
|
|
const user = new User();
|
|
|
|
user.userId = userId;
|
|
|
|
user.username = userName;
|
|
|
|
user.properties = properties;
|
|
|
|
user.groups = groups;
|
|
|
|
user.authenticated = false; // this is NOT an authenticated user!
|
|
|
|
|
|
|
|
return cb(err, user);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
static isRootUserId(userId) {
|
|
|
|
return (User.RootUserID === userId);
|
2015-07-22 22:37:11 +00:00
|
|
|
}
|
2015-07-22 05:52:20 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
static getUserIdAndName(username, cb) {
|
|
|
|
userDb.get(
|
|
|
|
`SELECT id, user_name
|
|
|
|
FROM user
|
|
|
|
WHERE user_name LIKE ?;`,
|
|
|
|
[ username ],
|
|
|
|
(err, row) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2015-05-12 22:34:11 +00:00
|
|
|
if(row) {
|
2017-02-18 20:21:18 +00:00
|
|
|
return cb(null, row.id, row.user_name);
|
2015-05-12 22:34:11 +00:00
|
|
|
}
|
2018-01-02 01:10:38 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
return cb(Errors.DoesNotExist('No matching username'));
|
2014-10-21 04:47:13 +00:00
|
|
|
}
|
2017-02-18 20:21:18 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-01-02 01:10:38 +00:00
|
|
|
static getUserIdAndNameByRealName(realName, cb) {
|
|
|
|
userDb.get(
|
|
|
|
`SELECT id, user_name
|
|
|
|
FROM user
|
|
|
|
WHERE id = (
|
|
|
|
SELECT user_id
|
|
|
|
FROM user_property
|
2018-01-02 01:43:05 +00:00
|
|
|
WHERE prop_name='real_name' AND prop_value LIKE ?
|
2018-01-02 01:10:38 +00:00
|
|
|
);`,
|
|
|
|
[ realName ],
|
|
|
|
(err, row) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(row) {
|
|
|
|
return cb(null, row.id, row.user_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cb(Errors.DoesNotExist('No matching real name'));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static getUserIdAndNameByLookup(lookup, cb) {
|
|
|
|
User.getUserIdAndName(lookup, (err, userId, userName) => {
|
|
|
|
if(err) {
|
|
|
|
User.getUserIdAndNameByRealName(lookup, (err, userId, userName) => {
|
|
|
|
return cb(err, userId, userName);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return cb(null, userId, userName);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
static getUserName(userId, cb) {
|
|
|
|
userDb.get(
|
|
|
|
`SELECT user_name
|
|
|
|
FROM user
|
|
|
|
WHERE id = ?;`,
|
|
|
|
[ userId ],
|
|
|
|
(err, row) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2015-08-03 00:27:05 +00:00
|
|
|
if(row) {
|
2017-02-18 20:21:18 +00:00
|
|
|
return cb(null, row.user_name);
|
2015-08-03 00:27:05 +00:00
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
return cb(Errors.DoesNotExist('No matching user ID'));
|
2015-08-03 00:27:05 +00:00
|
|
|
}
|
2017-02-18 20:21:18 +00:00
|
|
|
);
|
|
|
|
}
|
2014-10-26 03:35:42 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
static loadProperties(userId, options, cb) {
|
|
|
|
if(!cb && _.isFunction(options)) {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
2014-10-21 04:47:13 +00:00
|
|
|
}
|
2017-02-18 20:21:18 +00:00
|
|
|
|
|
|
|
let sql =
|
|
|
|
`SELECT prop_name, prop_value
|
|
|
|
FROM user_property
|
|
|
|
WHERE user_id = ?`;
|
|
|
|
|
|
|
|
if(options.names) {
|
|
|
|
sql += ` AND prop_name IN("${options.names.join('","')}");`;
|
2014-10-26 03:35:42 +00:00
|
|
|
} else {
|
2017-02-18 20:21:18 +00:00
|
|
|
sql += ';';
|
2014-10-26 03:35:42 +00:00
|
|
|
}
|
2014-10-20 05:30:44 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
let properties = {};
|
|
|
|
userDb.each(sql, [ userId ], (err, row) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
properties[row.prop_name] = row.prop_value;
|
2017-03-03 01:50:58 +00:00
|
|
|
}, (err) => {
|
|
|
|
return cb(err, err ? null : properties);
|
2017-02-18 20:21:18 +00:00
|
|
|
});
|
|
|
|
}
|
2015-04-06 06:18:08 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
// :TODO: make this much more flexible - propValue should allow for case-insensitive compare, etc.
|
|
|
|
static getUserIdsWithProperty(propName, propValue, cb) {
|
|
|
|
let userIds = [];
|
2015-04-06 06:18:08 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
userDb.each(
|
|
|
|
`SELECT user_id
|
|
|
|
FROM user_property
|
|
|
|
WHERE prop_name = ? AND prop_value = ?;`,
|
2018-01-15 19:22:11 +00:00
|
|
|
[ propName, propValue ],
|
2017-02-18 20:21:18 +00:00
|
|
|
(err, row) => {
|
|
|
|
if(row) {
|
|
|
|
userIds.push(row.user_id);
|
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
},
|
2017-02-18 20:21:18 +00:00
|
|
|
() => {
|
|
|
|
return cb(null, userIds);
|
|
|
|
}
|
|
|
|
);
|
2015-04-06 06:18:08 +00:00
|
|
|
}
|
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
static getUserList(options, cb) {
|
|
|
|
let userList = [];
|
|
|
|
let orderClause = 'ORDER BY ' + (options.order || 'user_name');
|
2015-04-06 06:18:08 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
userDb.each(
|
|
|
|
`SELECT id, user_name
|
|
|
|
FROM user
|
|
|
|
${orderClause};`,
|
|
|
|
(err, row) => {
|
|
|
|
if(row) {
|
|
|
|
userList.push({
|
|
|
|
userId : row.id,
|
|
|
|
userName : row.user_name,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
options.properties = options.properties || [];
|
|
|
|
async.map(userList, (user, nextUser) => {
|
|
|
|
userDb.each(
|
|
|
|
`SELECT prop_name, prop_value
|
|
|
|
FROM user_property
|
|
|
|
WHERE user_id = ? AND prop_name IN ("${options.properties.join('","')}");`,
|
|
|
|
[ user.userId ],
|
|
|
|
(err, row) => {
|
|
|
|
if(row) {
|
|
|
|
user[row.prop_name] = row.prop_value;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return nextUser(err, user);
|
|
|
|
}
|
|
|
|
);
|
2018-01-15 19:22:11 +00:00
|
|
|
},
|
2017-02-18 20:21:18 +00:00
|
|
|
(err, transformed) => {
|
|
|
|
return cb(err, transformed);
|
|
|
|
});
|
2015-11-28 05:26:00 +00:00
|
|
|
}
|
2017-02-18 20:21:18 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static generatePasswordDerivedKeyAndSalt(password, cb) {
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function getSalt(callback) {
|
|
|
|
User.generatePasswordDerivedKeySalt( (err, salt) => {
|
|
|
|
return callback(err, salt);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function getDk(salt, callback) {
|
|
|
|
User.generatePasswordDerivedKey(password, salt, (err, dk) => {
|
|
|
|
return callback(err, salt, dk);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
],
|
|
|
|
(err, salt, dk) => {
|
|
|
|
return cb(err, { salt : salt, dk : dk } );
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static generatePasswordDerivedKeySalt(cb) {
|
|
|
|
crypto.randomBytes(User.PBKDF2.saltLen, (err, salt) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
return cb(null, salt.toString('hex'));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-15 19:22:11 +00:00
|
|
|
static generatePasswordDerivedKey(password, salt, cb) {
|
2018-04-28 12:59:07 +00:00
|
|
|
password = Buffer.from(password).toString('hex');
|
2017-02-18 20:21:18 +00:00
|
|
|
|
|
|
|
crypto.pbkdf2(password, salt, User.PBKDF2.iterations, User.PBKDF2.keyLen, 'sha1', (err, dk) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
return cb(null, dk.toString('hex'));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|