2014-10-20 05:30:44 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-11-23 06:07:37 +00:00
|
|
|
// ENiGMA½
|
2022-06-05 20:04:25 +00:00
|
|
|
const userDb = require('./database.js').dbs.user;
|
|
|
|
const Config = require('./config.js').get;
|
|
|
|
const userGroup = require('./user_group.js');
|
|
|
|
const { Errors, ErrorReasons } = require('./enig_error.js');
|
|
|
|
const Events = require('./events.js');
|
|
|
|
const UserProps = require('./user_property.js');
|
|
|
|
const Log = require('./logger.js').log;
|
|
|
|
const StatLog = require('./stat_log.js');
|
2018-06-23 03:26:46 +00:00
|
|
|
|
|
|
|
// deps
|
2022-06-12 20:12:03 +00:00
|
|
|
const crypto = require('crypto');
|
|
|
|
const assert = require('assert');
|
|
|
|
const async = require('async');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const moment = require('moment');
|
|
|
|
const sanatizeFilename = require('sanitize-filename');
|
|
|
|
const ssh2 = require('ssh2');
|
2014-10-21 04:47:13 +00:00
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
module.exports = class User {
|
2018-06-22 05:15:04 +00:00
|
|
|
constructor() {
|
2022-06-12 20:12:03 +00:00
|
|
|
this.userId = 0;
|
|
|
|
this.username = '';
|
|
|
|
this.properties = {}; // name:value
|
|
|
|
this.groups = []; // group membership(s)
|
|
|
|
this.authFactor = User.AuthFactors.None;
|
|
|
|
this.statusFlags = User.StatusFlags.None;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// static property accessors
|
2018-06-22 05:15:04 +00:00
|
|
|
static get RootUserID() {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-04-10 02:07:19 +00:00
|
|
|
static get AuthFactors() {
|
|
|
|
return {
|
2022-06-05 20:04:25 +00:00
|
|
|
None: 0, // Not yet authenticated in any way
|
|
|
|
Factor1: 1, // username + password/pubkey/etc. checked out
|
|
|
|
Factor2: 2, // validated with 2FA of some sort such as OTP
|
2019-04-10 02:07:19 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
static get PBKDF2() {
|
|
|
|
return {
|
2022-06-05 20:04:25 +00:00
|
|
|
iterations: 1000,
|
|
|
|
keyLen: 128,
|
|
|
|
saltLen: 32,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static get StandardPropertyGroups() {
|
|
|
|
return {
|
2022-06-05 20:04:25 +00:00
|
|
|
auth: [
|
|
|
|
UserProps.PassPbkdf2Salt,
|
|
|
|
UserProps.PassPbkdf2Dk,
|
2019-04-10 02:07:19 +00:00
|
|
|
UserProps.AuthPubKey,
|
2019-02-21 06:55:09 +00:00
|
|
|
],
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static get AccountStatus() {
|
|
|
|
return {
|
2022-06-05 20:04:25 +00:00
|
|
|
disabled: 0, // +op disabled
|
|
|
|
inactive: 1, // inactive, aka requires +op approval/activation
|
|
|
|
active: 2, // standard, active
|
|
|
|
locked: 3, // locked out (too many bad login attempts, etc.)
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-05-09 04:15:57 +00:00
|
|
|
static get StatusFlags() {
|
|
|
|
return {
|
2022-06-12 20:12:03 +00:00
|
|
|
None: 0x00000000,
|
|
|
|
NotAvailable: 0x00000001, // Not currently available for chat, message, page, etc.
|
|
|
|
NotVisible: 0x00000002, // Invisible -- does not show online, last callers, etc.
|
|
|
|
};
|
2022-05-09 04:15:57 +00:00
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
isAuthenticated() {
|
|
|
|
return true === this.authenticated;
|
|
|
|
}
|
|
|
|
|
|
|
|
isValid() {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.userId <= 0 || this.username.length < Config().users.usernameMin) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-23 06:07:37 +00:00
|
|
|
return this.hasValidPasswordProperties();
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-23 06:07:37 +00:00
|
|
|
hasValidPasswordProperties() {
|
2022-06-05 20:04:25 +00:00
|
|
|
const salt = this.getProperty(UserProps.PassPbkdf2Salt);
|
|
|
|
const dk = this.getProperty(UserProps.PassPbkdf2Dk);
|
|
|
|
|
|
|
|
if (
|
|
|
|
!salt ||
|
|
|
|
!dk ||
|
|
|
|
salt.length !== User.PBKDF2.saltLen * 2 ||
|
|
|
|
dk.length !== User.PBKDF2.keyLen * 2
|
|
|
|
) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-23 06:07:37 +00:00
|
|
|
return true;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isRoot() {
|
|
|
|
return User.isRootUserId(this.userId);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
isSysOp() {
|
|
|
|
// alias to isRoot()
|
2018-06-22 05:15:04 +00:00
|
|
|
return this.isRoot();
|
|
|
|
}
|
|
|
|
|
|
|
|
isGroupMember(groupNames) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isString(groupNames)) {
|
|
|
|
groupNames = [groupNames];
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const isMember = groupNames.some(gn => -1 !== this.groups.indexOf(gn));
|
2018-06-22 05:15:04 +00:00
|
|
|
return isMember;
|
|
|
|
}
|
|
|
|
|
2023-08-24 15:13:29 +00:00
|
|
|
realName(withUsernameFallback = true) {
|
|
|
|
const realName = this.getProperty(UserProps.RealName);
|
|
|
|
if (realName) {
|
|
|
|
return realName;
|
|
|
|
}
|
|
|
|
if (withUsernameFallback) {
|
|
|
|
return this.username;
|
|
|
|
}
|
2023-07-24 18:17:47 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
getSanitizedName(type = 'username') {
|
2023-08-24 15:13:29 +00:00
|
|
|
const name = 'real' === type ? this.realName(true) : this.username;
|
2018-12-09 08:01:55 +00:00
|
|
|
return sanatizeFilename(name) || `user${this.userId.toString()}`;
|
|
|
|
}
|
|
|
|
|
2023-08-24 15:13:29 +00:00
|
|
|
emailAddress() {
|
|
|
|
const email = this.getProperty(UserProps.EmailAddress);
|
|
|
|
if (email) {
|
|
|
|
const realName = this.realName(false);
|
|
|
|
return realName ? `${realName} <${email}>` : email;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-09 04:15:57 +00:00
|
|
|
isAvailable() {
|
|
|
|
return (this.statusFlags & User.StatusFlags.NotAvailable) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
isVisible() {
|
|
|
|
return (this.statusFlags & User.StatusFlags.NotVisible) == 0;
|
|
|
|
}
|
|
|
|
|
2022-05-31 18:28:26 +00:00
|
|
|
setAvailability(available) {
|
|
|
|
if (available) {
|
|
|
|
this.statusFlags &= ~User.StatusFlags.NotAvailable;
|
|
|
|
} else {
|
|
|
|
this.statusFlags |= User.StatusFlags.NotAvailable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-09 04:15:57 +00:00
|
|
|
setVisibility(visible) {
|
|
|
|
if (visible) {
|
|
|
|
this.statusFlags &= ~User.StatusFlags.NotVisible;
|
|
|
|
} else {
|
|
|
|
this.statusFlags |= User.StatusFlags.NotVisible;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
getLegacySecurityLevel() {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.isRoot() || this.isGroupMember('sysops')) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return 100;
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.isGroupMember('users')) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return 30;
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
return 10; // :TODO: Is this what we want?
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-23 06:07:37 +00:00
|
|
|
processFailedLogin(userId, cb) {
|
|
|
|
async.waterfall(
|
|
|
|
[
|
2022-06-05 20:04:25 +00:00
|
|
|
callback => {
|
2018-11-23 06:07:37 +00:00
|
|
|
return User.getUser(userId, callback);
|
|
|
|
},
|
|
|
|
(tempUser, callback) => {
|
|
|
|
return StatLog.incrementUserStat(
|
|
|
|
tempUser,
|
|
|
|
UserProps.FailedLoginAttempts,
|
|
|
|
1,
|
|
|
|
(err, failedAttempts) => {
|
|
|
|
return callback(null, tempUser, failedAttempts);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
(tempUser, failedAttempts, callback) => {
|
|
|
|
const lockAccount = _.get(Config(), 'users.failedLogin.lockAccount');
|
2022-06-05 20:04:25 +00:00
|
|
|
if (lockAccount > 0 && failedAttempts >= lockAccount) {
|
2018-11-23 06:07:37 +00:00
|
|
|
const props = {
|
2022-06-05 20:04:25 +00:00
|
|
|
[UserProps.AccountStatus]: User.AccountStatus.locked,
|
|
|
|
[UserProps.AccountLockedTs]: StatLog.now,
|
2018-11-23 06:07:37 +00:00
|
|
|
};
|
2022-06-05 20:04:25 +00:00
|
|
|
if (
|
|
|
|
!_.has(tempUser.properties, UserProps.AccountLockedPrevStatus)
|
|
|
|
) {
|
|
|
|
props[UserProps.AccountLockedPrevStatus] =
|
|
|
|
tempUser.getProperty(UserProps.AccountStatus);
|
2018-11-23 06:07:37 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
Log.info(
|
|
|
|
{ userId, failedAttempts },
|
|
|
|
'(Re)setting account to locked due to failed logins'
|
|
|
|
);
|
2018-11-23 06:07:37 +00:00
|
|
|
return tempUser.persistProperties(props, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cb(null);
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-11-23 06:07:37 +00:00
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
unlockAccount(cb) {
|
|
|
|
const prevStatus = this.getProperty(UserProps.AccountLockedPrevStatus);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!prevStatus) {
|
|
|
|
return cb(null); // nothing to do
|
2018-11-23 06:07:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.persistProperty(UserProps.AccountStatus, prevStatus, err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-11-23 06:07:37 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
return this.removeProperties(
|
|
|
|
[UserProps.AccountLockedPrevStatus, UserProps.AccountLockedTs],
|
|
|
|
cb
|
|
|
|
);
|
2018-11-23 06:07:37 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-23 05:51:12 +00:00
|
|
|
static get AuthFactor1Types() {
|
|
|
|
return {
|
2022-06-05 20:04:25 +00:00
|
|
|
SSHPubKey: 'sshPubKey',
|
|
|
|
Password: 'password',
|
|
|
|
TLSClient: 'tlsClientAuth',
|
2019-02-23 05:51:12 +00:00
|
|
|
};
|
|
|
|
}
|
2019-02-21 06:55:09 +00:00
|
|
|
|
2019-02-23 05:51:12 +00:00
|
|
|
authenticateFactor1(authInfo, cb) {
|
|
|
|
const username = authInfo.username;
|
2018-06-22 05:15:04 +00:00
|
|
|
const self = this;
|
2018-11-23 06:07:37 +00:00
|
|
|
const tempAuthInfo = {};
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2019-02-21 06:55:09 +00:00
|
|
|
const validatePassword = (props, callback) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
User.generatePasswordDerivedKey(
|
|
|
|
authInfo.password,
|
|
|
|
props[UserProps.PassPbkdf2Salt],
|
|
|
|
(err, dk) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2019-02-21 06:55:09 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
//
|
|
|
|
// Use constant time comparison here for security feel-goods
|
|
|
|
//
|
|
|
|
const passDkBuf = Buffer.from(dk, 'hex');
|
|
|
|
const propsDkBuf = Buffer.from(props[UserProps.PassPbkdf2Dk], 'hex');
|
2019-02-21 06:55:09 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
return callback(
|
|
|
|
crypto.timingSafeEqual(passDkBuf, propsDkBuf)
|
|
|
|
? null
|
|
|
|
: Errors.AccessDenied('Invalid password')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2019-02-21 06:55:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const validatePubKey = (props, callback) => {
|
2019-04-10 02:07:19 +00:00
|
|
|
const pubKeyActual = ssh2.utils.parseKey(props[UserProps.AuthPubKey]);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!pubKeyActual) {
|
2019-02-21 06:55:09 +00:00
|
|
|
return callback(Errors.AccessDenied('Invalid public key'));
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (
|
|
|
|
authInfo.pubKey.key.algo != pubKeyActual.type ||
|
|
|
|
!crypto.timingSafeEqual(
|
|
|
|
authInfo.pubKey.key.data,
|
|
|
|
pubKeyActual.getPublicSSH()
|
|
|
|
)
|
|
|
|
) {
|
2019-02-21 06:55:09 +00:00
|
|
|
return callback(Errors.AccessDenied('Invalid public key'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null);
|
|
|
|
};
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function fetchUserId(callback) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// get user ID
|
2018-06-22 05:15:04 +00:00
|
|
|
User.getUserIdAndName(username, (err, uid, un) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
tempAuthInfo.userId = uid;
|
2018-11-23 06:07:37 +00:00
|
|
|
tempAuthInfo.username = un;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function getRequiredAuthProperties(callback) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// fetch properties required for authentication
|
2022-06-05 20:04:25 +00:00
|
|
|
User.loadProperties(
|
|
|
|
tempAuthInfo.userId,
|
|
|
|
{ names: User.StandardPropertyGroups.auth },
|
|
|
|
(err, props) => {
|
|
|
|
return callback(err, props);
|
|
|
|
}
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
2019-02-21 06:55:09 +00:00
|
|
|
function validatePassOrPubKey(props, callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (User.AuthFactor1Types.SSHPubKey === authInfo.type) {
|
2019-02-21 06:55:09 +00:00
|
|
|
return validatePubKey(props, callback);
|
|
|
|
}
|
|
|
|
return validatePassword(props, callback);
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
function initProps(callback) {
|
2018-11-23 06:07:37 +00:00
|
|
|
User.loadProperties(tempAuthInfo.userId, (err, allProps) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!err) {
|
2018-11-23 06:07:37 +00:00
|
|
|
tempAuthInfo.properties = allProps;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
},
|
2018-11-23 06:07:37 +00:00
|
|
|
function checkAccountStatus(callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
const accountStatus = parseInt(
|
|
|
|
tempAuthInfo.properties[UserProps.AccountStatus],
|
|
|
|
10
|
|
|
|
);
|
|
|
|
if (User.AccountStatus.disabled === accountStatus) {
|
|
|
|
return callback(
|
|
|
|
Errors.AccessDenied('Account disabled', ErrorReasons.Disabled)
|
|
|
|
);
|
2018-11-23 06:07:37 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
if (User.AccountStatus.inactive === accountStatus) {
|
|
|
|
return callback(
|
|
|
|
Errors.AccessDenied('Account inactive', ErrorReasons.Inactive)
|
|
|
|
);
|
2018-11-23 06:07:37 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (User.AccountStatus.locked === accountStatus) {
|
|
|
|
const autoUnlockMinutes = _.get(
|
|
|
|
Config(),
|
|
|
|
'users.failedLogin.autoUnlockMinutes'
|
|
|
|
);
|
|
|
|
const lockedTs = moment(
|
|
|
|
tempAuthInfo.properties[UserProps.AccountLockedTs]
|
|
|
|
);
|
|
|
|
if (autoUnlockMinutes && lockedTs.isValid()) {
|
2018-11-23 06:07:37 +00:00
|
|
|
const minutesSinceLocked = moment().diff(lockedTs, 'minutes');
|
2022-06-05 20:04:25 +00:00
|
|
|
if (minutesSinceLocked >= autoUnlockMinutes) {
|
2018-11-23 06:07:37 +00:00
|
|
|
// allow the login - we will clear any lock there
|
2018-11-23 18:44:46 +00:00
|
|
|
Log.info(
|
2022-06-05 20:04:25 +00:00
|
|
|
{
|
|
|
|
username,
|
|
|
|
userId: tempAuthInfo.userId,
|
|
|
|
lockedAt: lockedTs.format(),
|
|
|
|
},
|
2018-11-23 18:44:46 +00:00
|
|
|
'Locked account will now be unlocked due to auto-unlock minutes policy'
|
|
|
|
);
|
2018-11-23 06:07:37 +00:00
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
return callback(
|
|
|
|
Errors.AccessDenied('Account is locked', ErrorReasons.Locked)
|
|
|
|
);
|
2018-11-23 06:07:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// anything else besides active is still not allowed
|
2022-06-05 20:04:25 +00:00
|
|
|
if (User.AccountStatus.active !== accountStatus) {
|
2018-11-23 06:07:37 +00:00
|
|
|
return callback(Errors.AccessDenied('Account is not active'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null);
|
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
function initGroups(callback) {
|
2018-11-23 06:07:37 +00:00
|
|
|
userGroup.getGroupsForUser(tempAuthInfo.userId, (err, groups) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!err) {
|
2018-11-23 06:07:37 +00:00
|
|
|
tempAuthInfo.groups = groups;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return callback(err);
|
|
|
|
});
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-11-23 06:07:37 +00:00
|
|
|
//
|
|
|
|
// If we failed login due to something besides an inactive or disabled account,
|
|
|
|
// we need to update failure status and possibly lock the account.
|
|
|
|
//
|
|
|
|
// If locked already, update the lock timestamp -- ie, extend the lockout period.
|
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (
|
|
|
|
![ErrorReasons.Disabled, ErrorReasons.Inactive].includes(
|
|
|
|
err.reasonCode
|
|
|
|
) &&
|
|
|
|
tempAuthInfo.userId
|
|
|
|
) {
|
2018-11-23 06:07:37 +00:00
|
|
|
self.processFailedLogin(tempAuthInfo.userId, persistErr => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (persistErr) {
|
|
|
|
Log.warn(
|
|
|
|
{ error: persistErr.message },
|
|
|
|
'Failed to persist failed login information'
|
|
|
|
);
|
2018-11-23 06:07:37 +00:00
|
|
|
}
|
|
|
|
return cb(err); // pass along original error
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// everything checks out - load up info
|
2022-06-05 20:04:25 +00:00
|
|
|
self.userId = tempAuthInfo.userId;
|
|
|
|
self.username = tempAuthInfo.username;
|
|
|
|
self.properties = tempAuthInfo.properties;
|
|
|
|
self.groups = tempAuthInfo.groups;
|
|
|
|
self.authFactor = User.AuthFactors.Factor1;
|
2019-04-10 02:07:19 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// If 2FA/OTP is required, this user is not quite authenticated yet.
|
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
self.authenticated = !(self.getProperty(UserProps.AuthFactor2OTP)
|
|
|
|
? true
|
|
|
|
: false);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-11-23 06:07:37 +00:00
|
|
|
self.removeProperty(UserProps.FailedLoginAttempts);
|
|
|
|
|
|
|
|
//
|
|
|
|
// We need to *revert* any locked status back to
|
|
|
|
// the user's previous status & clean up props.
|
|
|
|
//
|
|
|
|
self.unlockAccount(unlockErr => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (unlockErr) {
|
|
|
|
Log.warn(
|
|
|
|
{ error: unlockErr.message },
|
|
|
|
'Failed to unlock account'
|
|
|
|
);
|
2018-11-23 06:07:37 +00:00
|
|
|
}
|
|
|
|
return cb(null);
|
|
|
|
});
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
create(createUserInfo, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
assert(0 === this.userId);
|
|
|
|
const config = Config();
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (
|
|
|
|
this.username.length < config.users.usernameMin ||
|
|
|
|
this.username.length > config.users.usernameMax
|
|
|
|
) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(Errors.Invalid('Invalid username length'));
|
|
|
|
}
|
|
|
|
|
|
|
|
const self = this;
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: set various defaults, e.g. default activation status, etc.
|
2022-06-05 20:04:25 +00:00
|
|
|
self.properties[UserProps.AccountStatus] = config.users.requireActivation
|
|
|
|
? User.AccountStatus.inactive
|
|
|
|
: User.AccountStatus.active;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function beginTransaction(callback) {
|
|
|
|
return userDb.beginTransaction(callback);
|
|
|
|
},
|
|
|
|
function createUserRec(trans, callback) {
|
|
|
|
trans.run(
|
|
|
|
`INSERT INTO user (user_name)
|
2018-06-23 03:26:46 +00:00
|
|
|
VALUES (?);`,
|
2022-06-05 20:04:25 +00:00
|
|
|
[self.username],
|
|
|
|
function inserted(err) {
|
|
|
|
// use classic function for |this|
|
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.userId = this.lastID;
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// Do not require activation for userId 1 (root/admin)
|
2022-06-05 20:04:25 +00:00
|
|
|
if (User.RootUserID === self.userId) {
|
|
|
|
self.properties[UserProps.AccountStatus] =
|
|
|
|
User.AccountStatus.active;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null, trans);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function genAuthCredentials(trans, callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
User.generatePasswordDerivedKeyAndSalt(
|
|
|
|
createUserInfo.password,
|
|
|
|
(err, info) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
self.properties[UserProps.PassPbkdf2Salt] = info.salt;
|
|
|
|
self.properties[UserProps.PassPbkdf2Dk] = info.dk;
|
|
|
|
return callback(null, trans);
|
|
|
|
}
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
function setInitialGroupMembership(trans, callback) {
|
2020-02-18 16:35:23 +00:00
|
|
|
// Assign initial groups. Must perform a clone: #235 - All users are sysops (and I can't un-sysop them)
|
|
|
|
self.groups = [...config.users.defaultGroups];
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (User.RootUserID === self.userId) {
|
|
|
|
// root/SysOp?
|
2018-06-22 05:15:04 +00:00
|
|
|
self.groups.push('sysops');
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null, trans);
|
|
|
|
},
|
|
|
|
function saveAll(trans, callback) {
|
|
|
|
self.persistWithTransaction(trans, err => {
|
|
|
|
return callback(err, trans);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function sendEvent(trans, callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
Events.emit(Events.getSystemEvents().NewUser, {
|
|
|
|
user: Object.assign({}, self, {
|
|
|
|
sessionId: createUserInfo.sessionId,
|
|
|
|
}),
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null, trans);
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
(err, trans) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (trans) {
|
2018-06-22 05:15:04 +00:00
|
|
|
trans[err ? 'rollback' : 'commit'](transErr => {
|
|
|
|
return cb(err ? err : transErr);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
persistWithTransaction(trans, cb) {
|
|
|
|
assert(this.userId > 0);
|
|
|
|
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function saveProps(callback) {
|
|
|
|
self.persistProperties(self.properties, trans, err => {
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function saveGroups(callback) {
|
|
|
|
userGroup.addUserToGroups(self.userId, self.groups, trans, err => {
|
|
|
|
return callback(err);
|
|
|
|
});
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-11-23 06:07:37 +00:00
|
|
|
static persistPropertyByUserId(userId, propName, propValue, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
userDb.run(
|
|
|
|
`REPLACE INTO user_property (user_id, prop_name, prop_value)
|
2018-06-23 03:26:46 +00:00
|
|
|
VALUES (?, ?, ?);`,
|
2022-06-05 20:04:25 +00:00
|
|
|
[userId, propName, propValue],
|
2018-06-22 05:15:04 +00:00
|
|
|
err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (cb) {
|
2018-11-23 06:07:37 +00:00
|
|
|
return cb(err, propValue);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-11 03:34:52 +00:00
|
|
|
setProperty(propName, propValue) {
|
|
|
|
this.properties[propName] = propValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
incrementProperty(propName, incrementBy) {
|
|
|
|
incrementBy = incrementBy || 1;
|
|
|
|
let newValue = parseInt(this.getProperty(propName));
|
2022-06-05 20:04:25 +00:00
|
|
|
if (newValue) {
|
2019-01-11 03:34:52 +00:00
|
|
|
newValue += incrementBy;
|
|
|
|
} else {
|
|
|
|
newValue = incrementBy;
|
|
|
|
}
|
|
|
|
this.setProperty(propName, newValue);
|
|
|
|
return newValue;
|
|
|
|
}
|
|
|
|
|
2018-11-23 06:07:37 +00:00
|
|
|
getProperty(propName) {
|
|
|
|
return this.properties[propName];
|
|
|
|
}
|
|
|
|
|
|
|
|
getPropertyAsNumber(propName) {
|
|
|
|
return parseInt(this.getProperty(propName), 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
persistProperty(propName, propValue, cb) {
|
|
|
|
// update live props
|
|
|
|
this.properties[propName] = propValue;
|
|
|
|
|
|
|
|
return User.persistPropertyByUserId(this.userId, propName, propValue, cb);
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
removeProperty(propName, cb) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// update live
|
2018-06-22 05:15:04 +00:00
|
|
|
delete this.properties[propName];
|
|
|
|
|
|
|
|
userDb.run(
|
|
|
|
`DELETE FROM user_property
|
2018-06-23 03:26:46 +00:00
|
|
|
WHERE user_id = ? AND prop_name = ?;`,
|
2022-06-05 20:04:25 +00:00
|
|
|
[this.userId, propName],
|
2018-06-22 05:15:04 +00:00
|
|
|
err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-11-23 06:07:37 +00:00
|
|
|
removeProperties(propNames, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
async.each(
|
|
|
|
propNames,
|
|
|
|
(name, next) => {
|
|
|
|
return this.removeProperty(name, next);
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
if (cb) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2018-11-28 05:01:14 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
);
|
2018-11-23 06:07:37 +00:00
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
persistProperties(properties, transOrDb, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!_.isFunction(cb) && _.isFunction(transOrDb)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
cb = transOrDb;
|
|
|
|
transOrDb = userDb;
|
|
|
|
}
|
|
|
|
|
|
|
|
const self = this;
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// update live props
|
2018-06-22 05:15:04 +00:00
|
|
|
_.merge(this.properties, properties);
|
|
|
|
|
|
|
|
const stmt = transOrDb.prepare(
|
|
|
|
`REPLACE INTO user_property (user_id, prop_name, prop_value)
|
2018-06-23 03:26:46 +00:00
|
|
|
VALUES (?, ?, ?);`
|
2018-06-22 05:15:04 +00:00
|
|
|
);
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
async.each(
|
|
|
|
Object.keys(properties),
|
|
|
|
(propName, nextProp) => {
|
|
|
|
stmt.run(self.userId, propName, properties[propName], err => {
|
|
|
|
return nextProp(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
stmt.finalize(() => {
|
|
|
|
return cb(null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setNewAuthCredentials(password, cb) {
|
|
|
|
User.generatePasswordDerivedKeyAndSalt(password, (err, info) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const newProperties = {
|
2022-06-05 20:04:25 +00:00
|
|
|
[UserProps.PassPbkdf2Salt]: info.salt,
|
|
|
|
[UserProps.PassPbkdf2Dk]: info.dk,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.persistProperties(newProperties, err => {
|
|
|
|
return cb(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getAge() {
|
2018-11-23 06:07:37 +00:00
|
|
|
const birthdate = this.getProperty(UserProps.Birthdate);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (birthdate) {
|
2018-11-23 06:07:37 +00:00
|
|
|
return moment().diff(birthdate, 'years');
|
2018-06-22 05:15:04 +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);
|
|
|
|
});
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
(err, userName, properties, groups) => {
|
|
|
|
const user = new User();
|
2022-06-05 20:04:25 +00:00
|
|
|
user.userId = userId;
|
|
|
|
user.username = userName;
|
|
|
|
user.properties = properties;
|
|
|
|
user.groups = groups;
|
2019-04-10 02:07:19 +00:00
|
|
|
|
|
|
|
// explicitly NOT an authenticated user!
|
2022-06-05 20:04:25 +00:00
|
|
|
user.authenticated = false;
|
|
|
|
user.authFactor = User.AuthFactors.None;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
return cb(err, user);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-11-27 02:51:00 +00:00
|
|
|
static getUserInfo(userId, propsList, cb) {
|
|
|
|
if (!cb && _.isFunction(propsList)) {
|
|
|
|
cb = propsList;
|
|
|
|
propsList = [
|
2022-06-12 20:12:03 +00:00
|
|
|
UserProps.RealName,
|
|
|
|
UserProps.Sex,
|
|
|
|
UserProps.EmailAddress,
|
|
|
|
UserProps.Location,
|
|
|
|
UserProps.Affiliations,
|
2020-11-27 02:51:00 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
2022-06-12 20:12:03 +00:00
|
|
|
callback => {
|
2020-11-27 02:51:00 +00:00
|
|
|
return User.getUserName(userId, callback);
|
|
|
|
},
|
|
|
|
(userName, callback) => {
|
2022-06-12 20:12:03 +00:00
|
|
|
User.loadProperties(userId, { names: propsList }, (err, props) => {
|
|
|
|
return callback(
|
|
|
|
err,
|
|
|
|
Object.assign({}, props, { user_name: userName })
|
|
|
|
);
|
2020-11-27 02:51:00 +00:00
|
|
|
});
|
2022-06-12 20:12:03 +00:00
|
|
|
},
|
2020-11-27 02:51:00 +00:00
|
|
|
],
|
|
|
|
(err, userProps) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const userInfo = {};
|
|
|
|
Object.keys(userProps).forEach(key => {
|
|
|
|
userInfo[_.camelCase(key)] = userProps[key] || 'N/A';
|
|
|
|
});
|
|
|
|
|
|
|
|
return cb(null, userInfo);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
static isRootUserId(userId) {
|
2022-06-05 20:04:25 +00:00
|
|
|
return User.RootUserID === userId;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static getUserIdAndName(username, cb) {
|
|
|
|
userDb.get(
|
|
|
|
`SELECT id, user_name
|
2018-06-23 03:26:46 +00:00
|
|
|
FROM user
|
|
|
|
WHERE user_name LIKE ?;`,
|
2022-06-05 20:04:25 +00:00
|
|
|
[username],
|
2018-06-22 05:15:04 +00:00
|
|
|
(err, row) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (row) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(null, row.id, row.user_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cb(Errors.DoesNotExist('No matching username'));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static getUserIdAndNameByRealName(realName, cb) {
|
|
|
|
userDb.get(
|
|
|
|
`SELECT id, user_name
|
2018-06-23 03:26:46 +00:00
|
|
|
FROM user
|
|
|
|
WHERE id = (
|
|
|
|
SELECT user_id
|
|
|
|
FROM user_property
|
2018-11-24 05:18:15 +00:00
|
|
|
WHERE prop_name='${UserProps.RealName}' AND prop_value LIKE ?
|
2018-06-23 03:26:46 +00:00
|
|
|
);`,
|
2022-06-05 20:04:25 +00:00
|
|
|
[realName],
|
2018-06-22 05:15:04 +00:00
|
|
|
(err, row) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (row) {
|
2018-06-22 05:15:04 +00:00
|
|
|
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) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
User.getUserIdAndNameByRealName(lookup, (err, userId, userName) => {
|
|
|
|
return cb(err, userId, userName);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return cb(null, userId, userName);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static getUserName(userId, cb) {
|
|
|
|
userDb.get(
|
|
|
|
`SELECT user_name
|
2018-06-23 03:26:46 +00:00
|
|
|
FROM user
|
|
|
|
WHERE id = ?;`,
|
2022-06-05 20:04:25 +00:00
|
|
|
[userId],
|
2018-06-22 05:15:04 +00:00
|
|
|
(err, row) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (row) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(null, row.user_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cb(Errors.DoesNotExist('No matching user ID'));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static loadProperties(userId, options, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!cb && _.isFunction(options)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
let sql = `SELECT prop_name, prop_value
|
2018-06-23 03:26:46 +00:00
|
|
|
FROM user_property
|
|
|
|
WHERE user_id = ?`;
|
2017-02-18 20:21:18 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (options.names) {
|
2018-06-22 05:15:04 +00:00
|
|
|
sql += ` AND prop_name IN("${options.names.join('","')}");`;
|
|
|
|
} else {
|
|
|
|
sql += ';';
|
|
|
|
}
|
|
|
|
|
|
|
|
let properties = {};
|
2022-06-05 20:04:25 +00:00
|
|
|
userDb.each(
|
|
|
|
sql,
|
|
|
|
[userId],
|
|
|
|
(err, row) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
properties[row.prop_name] = row.prop_value;
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return cb(err, err ? null : properties);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: make this much more flexible - propValue should allow for case-insensitive compare, etc.
|
2018-06-22 05:15:04 +00:00
|
|
|
static getUserIdsWithProperty(propName, propValue, cb) {
|
|
|
|
let userIds = [];
|
|
|
|
|
|
|
|
userDb.each(
|
|
|
|
`SELECT user_id
|
2018-06-23 03:26:46 +00:00
|
|
|
FROM user_property
|
|
|
|
WHERE prop_name = ? AND prop_value = ?;`,
|
2022-06-05 20:04:25 +00:00
|
|
|
[propName, propValue],
|
2018-06-22 05:15:04 +00:00
|
|
|
(err, row) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (row) {
|
2018-06-22 05:15:04 +00:00
|
|
|
userIds.push(row.user_id);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
return cb(null, userIds);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-05-02 01:58:00 +00:00
|
|
|
static getUserCount(cb) {
|
|
|
|
userDb.get(
|
|
|
|
`SELECT count() AS user_count
|
|
|
|
FROM user;`,
|
|
|
|
(err, row) => {
|
2022-06-12 20:12:03 +00:00
|
|
|
if (err) {
|
2022-05-02 01:58:00 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
return cb(null, row.user_count);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
static getUserList(options, cb) {
|
2018-07-22 18:56:56 +00:00
|
|
|
const userList = [];
|
2020-07-14 04:40:49 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
options.properties = options.properties || [UserProps.RealName];
|
2020-07-14 04:40:49 +00:00
|
|
|
|
|
|
|
const asList = [];
|
|
|
|
const joinList = [];
|
|
|
|
for (let i = 0; i < options.properties.length; ++i) {
|
|
|
|
const dbProp = options.properties[i];
|
|
|
|
const propName = options.propsCamelCase ? _.camelCase(dbProp) : dbProp;
|
|
|
|
asList.push(`p${i}.prop_value AS ${propName}`);
|
2022-06-05 20:04:25 +00:00
|
|
|
joinList.push(
|
|
|
|
`LEFT OUTER JOIN user_property p${i} ON p${i}.user_id = u.id AND p${i}.prop_name = '${dbProp}'`
|
|
|
|
);
|
2020-07-14 04:40:49 +00:00
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
userDb.each(
|
2020-07-14 04:40:49 +00:00
|
|
|
`SELECT u.id as userId, u.user_name as userName, ${asList.join(', ')}
|
|
|
|
FROM user u ${joinList.join(' ')}
|
|
|
|
ORDER BY u.user_name;`,
|
2018-06-22 05:15:04 +00:00
|
|
|
(err, row) => {
|
2020-07-14 04:40:49 +00:00
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2020-07-14 04:40:49 +00:00
|
|
|
userList.push(row);
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
2020-07-14 04:40:49 +00:00
|
|
|
err => {
|
|
|
|
return cb(err, userList);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static generatePasswordDerivedKeyAndSalt(password, cb) {
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function getSalt(callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
User.generatePasswordDerivedKeySalt((err, salt) => {
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(err, salt);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function getDk(salt, callback) {
|
|
|
|
User.generatePasswordDerivedKey(password, salt, (err, dk) => {
|
|
|
|
return callback(err, salt, dk);
|
|
|
|
});
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
(err, salt, dk) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
return cb(err, { salt: salt, dk: dk });
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static generatePasswordDerivedKeySalt(cb) {
|
|
|
|
crypto.randomBytes(User.PBKDF2.saltLen, (err, salt) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
return cb(null, salt.toString('hex'));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static generatePasswordDerivedKey(password, salt, cb) {
|
|
|
|
password = Buffer.from(password).toString('hex');
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
crypto.pbkdf2(
|
|
|
|
password,
|
|
|
|
salt,
|
|
|
|
User.PBKDF2.iterations,
|
|
|
|
User.PBKDF2.keyLen,
|
|
|
|
'sha1',
|
|
|
|
(err, dk) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
return cb(null, dk.toString('hex'));
|
|
|
|
}
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2017-02-18 20:21:18 +00:00
|
|
|
};
|