Log client ID after authentication

This commit is contained in:
Andris Reinman 2023-08-10 13:57:58 +03:00
parent 103534ef2a
commit a3ebf05f47
4 changed files with 28 additions and 16 deletions

View file

@ -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, {
@ -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',

View file

@ -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'];
@ -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();
@ -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',
@ -79,7 +69,8 @@ module.exports = {
);
});
this._server.loggelf(logdata);
this.session.clientId = clientId;
imapTools.logClientId(this);
}
// Create response ID serverIdList

View file

@ -124,6 +124,7 @@ module.exports = {
this.state = 'Authenticated';
this.setupNotificationListener();
imapTools.sendCapabilityResponse(this);
imapTools.logClientId(this);
callback(null, {
response: 'OK',

View file

@ -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);
};