Skip to content

Commit

Permalink
Log client ID after authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
andris9 committed Aug 10, 2023
1 parent 103534e commit a3ebf05
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
6 changes: 3 additions & 3 deletions imap-core/lib/commands/authenticate-plain.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ module.exports = {
};

function authenticate(connection, token, requireClientToken, callback) {
let data = Buffer.from(token, 'base64')
.toString()
.split('\x00');
let data = Buffer.from(token, 'base64').toString().split('\x00');

if ((!requireClientToken && data.length !== 3) || (requireClientToken && data.length !== 4)) {
return callback(null, {
Expand Down Expand Up @@ -134,7 +132,9 @@ function authenticate(connection, token, requireClientToken, callback) {
connection.setUser(response.user);
connection.state = 'Authenticated';
connection.setupNotificationListener();

imapTools.sendCapabilityResponse(connection);
imapTools.logClientId(connection);

callback(null, {
response: 'OK',
Expand Down
17 changes: 4 additions & 13 deletions imap-core/lib/commands/id.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const packageInfo = require('../../../package');
const imapHandler = require('../handler/imap-handler');
const imapTools = require('../imap-tools');

const allowedKeys = ['name', 'version', 'os', 'os-version', 'vendor', 'support-url', 'address', 'date', 'command', 'arguments', 'environment'];

Expand Down Expand Up @@ -33,10 +34,7 @@ module.exports = {
if (Array.isArray(command.attributes[0])) {
command.attributes[0].forEach(val => {
if (key === false) {
key = (val.value || '')
.toString()
.toLowerCase()
.trim();
key = (val.value || '').toString().toLowerCase().trim();
} else {
if (allowedKeys.indexOf(key) >= 0) {
clientId[key] = (val.value || '').toString();
Expand All @@ -55,17 +53,9 @@ module.exports = {
this.id
);

let logdata = {
short_message: '[CLIENT ID]',
_mail_action: 'client_id',
_user: this.session && this.session.user && this.session.user.id && this.session.user.id.toString(),
_sess: this.id
};

Object.keys(clientId)
.sort((a, b) => allowedKeys.indexOf(a) - allowedKeys.indexOf(b))
.forEach(key => {
logdata[`_client_id_${key}`] = clientId[key];
this._server.logger.info(
{
tnx: 'id',
Expand All @@ -79,7 +69,8 @@ module.exports = {
);
});

this._server.loggelf(logdata);
this.session.clientId = clientId;
imapTools.logClientId(this);
}

// Create response ID serverIdList
Expand Down
1 change: 1 addition & 0 deletions imap-core/lib/commands/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ module.exports = {
this.state = 'Authenticated';
this.setupNotificationListener();
imapTools.sendCapabilityResponse(this);
imapTools.logClientId(this);

callback(null, {
response: 'OK',
Expand Down
20 changes: 20 additions & 0 deletions imap-core/lib/imap-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -783,3 +783,23 @@ module.exports.validateSearchDate = internaldate => {
}
return /^\d{1,2}-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\d{4}$/i.test(internaldate);
};

module.exports.logClientId = connection => {
if (!connection.session.clientId) {
return false;
}

let logdata = {
short_message: '[CLIENT ID]',
_mail_action: 'client_id',
_authenticated: !!connection.session && connection.session.user && connection.session.user.id ? 'yes' : 'no',
_user: connection.session && connection.session.user && connection.session.user.id && connection.session.user.id.toString(),
_sess: connection.id
};

Object.keys(connection.session.clientId || {}).forEach(key => {
logdata[`_client_id_${key}`] = connection.session.clientId[key];
});

connection._server.loggelf(logdata);
};

0 comments on commit a3ebf05

Please sign in to comment.