2015-10-19 23:21:47 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
|
|
|
const setClientTheme = require('./theme.js').setClientTheme;
|
|
|
|
const clientConnections = require('./client_connections.js').clientConnections;
|
|
|
|
const StatLog = require('./stat_log.js');
|
|
|
|
const logger = require('./logger.js');
|
|
|
|
const Events = require('./events.js');
|
2018-11-11 08:55:38 +00:00
|
|
|
const Config = require('./config.js').get;
|
2018-11-22 02:43:50 +00:00
|
|
|
const {
|
|
|
|
Errors,
|
|
|
|
ErrorReasons
|
|
|
|
} = require('./enig_error.js');
|
2018-11-24 00:41:16 +00:00
|
|
|
const UserProps = require('./user_property.js');
|
2018-11-26 02:05:16 +00:00
|
|
|
const SysProps = require('./system_property.js');
|
|
|
|
const SystemLogKeys = require('./system_log.js');
|
2015-10-19 23:21:47 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// deps
|
|
|
|
const async = require('async');
|
2018-11-23 06:07:37 +00:00
|
|
|
const _ = require('lodash');
|
2015-10-19 23:21:47 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
exports.userLogin = userLogin;
|
2015-10-19 23:21:47 +00:00
|
|
|
|
|
|
|
function userLogin(client, username, password, cb) {
|
2018-12-24 22:32:38 +00:00
|
|
|
const config = Config();
|
|
|
|
|
|
|
|
if(config.users.badUserNames.includes(username.toLowerCase())) {
|
|
|
|
client.log.info( { username : username }, 'Attempt to login with banned username');
|
|
|
|
return cb(Errors.BadLogin(ErrorReasons.NotAllowed));
|
|
|
|
}
|
2018-11-23 06:07:37 +00:00
|
|
|
|
2018-12-24 22:32:38 +00:00
|
|
|
client.user.authenticate(username, password, err => {
|
2018-06-22 05:15:04 +00:00
|
|
|
if(err) {
|
2018-11-23 06:07:37 +00:00
|
|
|
client.user.sessionFailedLoginAttempts = _.get(client.user, 'sessionFailedLoginAttempts', 0) + 1;
|
|
|
|
const disconnect = config.users.failedLogin.disconnect;
|
|
|
|
if(disconnect > 0 && client.user.sessionFailedLoginAttempts >= disconnect) {
|
2018-11-23 18:44:46 +00:00
|
|
|
err = Errors.BadLogin('To many failed login attempts', ErrorReasons.TooMany);
|
2018-11-23 06:07:37 +00:00
|
|
|
}
|
2015-10-19 23:21:47 +00:00
|
|
|
|
2018-11-23 18:44:46 +00:00
|
|
|
client.log.info( { username : username, error : err.message }, 'Failed login attempt');
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
2018-11-23 06:07:37 +00:00
|
|
|
|
|
|
|
const user = client.user;
|
|
|
|
|
|
|
|
// Good login; reset any failed attempts
|
|
|
|
delete user.sessionFailedLoginAttempts;
|
2015-10-19 23:21:47 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Ensure this user is not already logged in.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-11-22 02:50:03 +00:00
|
|
|
const existingClientConnection = clientConnections.find(cc => {
|
|
|
|
return user !== cc.user && // not current connection
|
|
|
|
user.userId === cc.user.userId; // ...but same user
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
2015-10-19 23:21:47 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if(existingClientConnection) {
|
|
|
|
client.log.info(
|
|
|
|
{
|
2018-06-23 03:26:46 +00:00
|
|
|
existingClientId : existingClientConnection.session.id,
|
|
|
|
username : user.username,
|
|
|
|
userId : user.userId
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
'Already logged in'
|
|
|
|
);
|
2015-10-19 23:21:47 +00:00
|
|
|
|
2018-11-22 02:43:50 +00:00
|
|
|
return cb(Errors.BadLogin(
|
|
|
|
`User ${user.username} already logged in.`,
|
|
|
|
ErrorReasons.AlreadyLoggedIn
|
|
|
|
));
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2015-10-19 23:21:47 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// update client logger with addition of username
|
2018-06-22 05:15:04 +00:00
|
|
|
client.log = logger.log.child(
|
|
|
|
{
|
2018-06-23 03:26:46 +00:00
|
|
|
clientId : client.log.fields.clientId,
|
|
|
|
sessionId : client.log.fields.sessionId,
|
|
|
|
username : user.username,
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
client.log.info('Successful login');
|
2015-10-19 23:21:47 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// User's unique session identifier is the same as the connection itself
|
2018-11-24 00:41:16 +00:00
|
|
|
user.sessionId = client.session.uniqueId; // convenience
|
2018-06-04 01:58:31 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
Events.emit(Events.getSystemEvents().UserLogin, { user } );
|
2018-06-03 23:59:16 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
async.parallel(
|
|
|
|
[
|
|
|
|
function setTheme(callback) {
|
2018-11-24 00:41:16 +00:00
|
|
|
setClientTheme(client, user.properties[UserProps.ThemeId]);
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null);
|
|
|
|
},
|
|
|
|
function updateSystemLoginCount(callback) {
|
2018-11-26 03:13:48 +00:00
|
|
|
StatLog.incrementNonPersistentSystemStat(SysProps.LoginsToday, 1);
|
2018-11-26 02:05:16 +00:00
|
|
|
return StatLog.incrementSystemStat(SysProps.LoginCount, 1, callback);
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
function recordLastLogin(callback) {
|
2018-11-24 00:41:16 +00:00
|
|
|
return StatLog.setUserStat(user, UserProps.LastLoginTs, StatLog.now, callback);
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
function updateUserLoginCount(callback) {
|
2018-11-24 00:41:16 +00:00
|
|
|
return StatLog.incrementUserStat(user, UserProps.LoginCount, 1, callback);
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
function recordLoginHistory(callback) {
|
2018-11-11 08:55:38 +00:00
|
|
|
const loginHistoryMax = Config().statLog.systemEvents.loginHistoryMax;
|
2018-07-21 20:32:06 +00:00
|
|
|
const historyItem = JSON.stringify({
|
|
|
|
userId : user.userId,
|
|
|
|
sessionId : user.sessionId,
|
|
|
|
});
|
2018-11-11 08:58:49 +00:00
|
|
|
|
|
|
|
return StatLog.appendSystemLogEntry(
|
2018-11-26 02:05:16 +00:00
|
|
|
SystemLogKeys.UserLoginHistory,
|
2018-11-11 08:58:49 +00:00
|
|
|
historyItem,
|
|
|
|
loginHistoryMax,
|
|
|
|
StatLog.KeepType.Max,
|
|
|
|
callback
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2015-10-19 23:21:47 +00:00
|
|
|
}
|