From 1cb811576b4b7f6d36d87edd9421800dee6e1571 Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Sun, 3 Jun 2018 19:58:31 -0600 Subject: [PATCH] + Add unique session ID to client sessions * Aliased to user for convienence * Added to logs for easy tracing * Can be used from events/etc. for grouping --- core/client_connections.js | 6 +++++- core/user_login.js | 12 ++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/core/client_connections.js b/core/client_connections.js index 121ea0fa..3e7378f9 100644 --- a/core/client_connections.js +++ b/core/client_connections.js @@ -8,6 +8,7 @@ const Events = require('./events.js'); // deps const _ = require('lodash'); const moment = require('moment'); +const hashids = require('hashids'); exports.getActiveConnections = getActiveConnections; exports.getActiveNodeList = getActiveNodeList; @@ -60,9 +61,12 @@ function addNewClient(client, clientSock) { const id = client.session.id = clientConnections.push(client) - 1; const remoteAddress = client.remoteAddress = clientSock.remoteAddress; + // create a uniqe identifier one-time ID for this session + client.session.uniqueId = new hashids('ENiGMA½ClientSession').encode([ id, moment().valueOf() ]); + // Create a client specific logger // Note that this will be updated @ login with additional information - client.log = logger.log.child( { clientId : id } ); + client.log = logger.log.child( { clientId : id, sessionId : client.session.uniqueId } ); const connInfo = { remoteAddress : remoteAddress, diff --git a/core/user_login.js b/core/user_login.js index 8e0e7404..80d832e0 100644 --- a/core/user_login.js +++ b/core/user_login.js @@ -55,11 +55,19 @@ function userLogin(client, username, password, cb) { return cb(existingConnError); } - // update client logger with addition of username - client.log = logger.log.child( { clientId : client.log.fields.clientId, username : user.username }); + client.log = logger.log.child( + { + clientId : client.log.fields.clientId, + sessionId : client.log.fields.sessionId, + username : user.username, + } + ); client.log.info('Successful login'); + // User's unique session identifier is the same as the connection itself + user.sessionId = client.session.uniqueId; // convienence + Events.emit(Events.getSystemEvents().UserLogin, { user } ); async.parallel(