This commit is contained in:
Andris Reinman 2017-08-31 15:52:13 +03:00
parent ea67b25bb5
commit 5862950741
2 changed files with 82 additions and 4 deletions

View file

@ -27,6 +27,75 @@ class UserHandler {
this.authlogExpireDays = options.authlogExpireDays;
}
/**
* Reolve user by username/address
*
* @param {String} username Either username or email address
* @param {Object} [extraFields] Optional projection fields object
*/
get(username, extraFields, callback) {
if (!callback && typeof extraFields === 'function') {
callback = extraFields;
extraFields = false;
}
let fields = {
_id: true,
quota: true,
storageUsed: true,
disabled: true
};
Object.keys(extraFields || {}).forEach(field => {
fields[field] = true;
});
let checkAddress = next => {
if (username.indexOf('@') < 0) {
// assume regular username
return next(null, {
unameview: username.replace(/\./g, '')
});
}
// try to find existing email address
let address = tools.normalizeAddress(username);
this.users.collection('addresses').findOne({
addrview: address.substr(0, address.indexOf('@')).replace(/\./g, '') + address.substr(address.indexOf('@'))
}, {
fields: {
user: true
}
}, (err, addressData) => {
if (err) {
return callback(err);
}
if (!addressData) {
return callback(null, false);
}
next(null, { _id: addressData.user });
});
};
checkAddress((err, query) => {
if (err) {
return callback(err);
}
this.users.collection('users').findOne(query, {
fields
}, (err, userData) => {
if (err) {
return callback(err);
}
return callback(null, userData);
});
});
}
/**
* Authenticate user
*
@ -182,7 +251,10 @@ class UserHandler {
return this.logAuthEvent(userData._id, meta, () => authFail(null, false));
}
let prefix = crypto.createHash('md5').update(password.substr(0, 4)).digest('hex');
let prefix = crypto
.createHash('md5')
.update(password.substr(0, 4))
.digest('hex');
this.users
.collection('asps')
@ -270,7 +342,10 @@ class UserHandler {
// We need a quick hash key that can be used to identify the password.
// Otherwise, when authenticating, we'd need to check the password against all stored bcrypt
// hashes which would make forever if the user has a longer list of application specific passwords
let prefix = crypto.createHash('md5').update(password.substr(0, 4)).digest('hex');
let prefix = crypto
.createHash('md5')
.update(password.substr(0, 4))
.digest('hex');
let allowedScopes = ['imap', 'pop3', 'smtp'];
let hasAllScopes = false;
@ -1090,7 +1165,10 @@ class UserHandler {
}
entry.count = 1;
entry.groupKey = crypto.createHash('sha1').update(entry.groupKey + ':' + Math.floor(Date.now() / (3600 * 1000))).digest('base64');
entry.groupKey = crypto
.createHash('sha1')
.update(entry.groupKey + ':' + Math.floor(Date.now() / (3600 * 1000)))
.digest('base64');
entry.updated = entry.created;
this.users.collection('authlog').findOneAndUpdate({
user,

View file

@ -1,6 +1,6 @@
{
"name": "wildduck",
"version": "1.0.74",
"version": "1.0.75",
"description": "IMAP server built with Node.js and MongoDB",
"main": "server.js",
"scripts": {