Skip to content

Commit

Permalink
feat: add a convenience callback to do ldapSearch in getUserInfo config
Browse files Browse the repository at this point in the history
  • Loading branch information
stropitek committed Apr 27, 2022
1 parent 5de6cda commit 9e41fd6
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/couch/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,11 +437,11 @@ async function syncOneLdapGroup(ctx, group, user) {
let user = entry.object;
// Custom email extraction
if (user) {
if (ctx._getPublicUserInfo) {
if (ctx._config.getPublicUserInfo) {
try {
const userInfo = ctx._getPublicUserInfo(user);
const userInfo = ctx._config.getPublicUserInfo(user);
if (userInfo !== null) {
info.push(ctx._getPublicUserInfo(user));
info.push(ctx._config.getPublicUserInfo(user));
}
} catch {
// Do not add anything to info
Expand Down
4 changes: 1 addition & 3 deletions src/couch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class Couch {
};

this._logLevel = log.getLevel(config.logLevel);
this._config = config;

this._customDesign = config.customDesign || {};
this._viewsWithOwner = new Set();
Expand All @@ -61,9 +62,6 @@ class Couch {
}
}

this._getUserInfo = config.getUserInfo;
this._getPublicUserInfo = config.getPublicUserInfo;

this._defaultEntry = config.defaultEntry || getDefaultEntry;
this._rights = Object.assign({}, basicRights, config.rights);
this._administrators = config.administrators || [];
Expand Down
42 changes: 41 additions & 1 deletion src/couch/user.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const CouchError = require('../util/CouchError');
const { search } = require('../util/LDAP');
const debug = require('../util/debug')('main:user');
const simpleMerge = require('../util/simpleMerge');

Expand Down Expand Up @@ -36,7 +37,46 @@ const methods = {

getUserInfo(user) {
debug('getUserInfo (%s)', user);
return this._getUserInfo(user);
// Callback which allows to do a custom ldap search to get user data
// If it exists, it uses the auth ldap config for default values
const ldapServer = this._config.auth.ldap?.server;

function ldapSearch(ldapOptions, searchOptions) {
debug('getUserInfo ldapSearch callback');
const defaultLdapOptions = ldapServer
? {
url: ldapServer.url,
bindDN: ldapServer.bindDN,
bindPassword: ldapServer.bindCredentials,
}
: {};

const defaultSearchOptions = ldapServer
? {
DN: ldapServer.searchBase,
}
: {};
if (searchOptions === undefined) {
searchOptions = ldapOptions;
ldapOptions = {};
}
const finalLdapOptions = Object.assign(
{},
defaultLdapOptions,
ldapOptions,
);
const finalSearchOptions = Object.assign(
{},
defaultSearchOptions,
searchOptions,
);
return search(finalLdapOptions, finalSearchOptions);
}

if (!this._config.getUserInfo) {
throw new CouchError('getUserInfo is not configured', 'bad request');
}
return this._config.getUserInfo(user, ldapSearch);
},

async getUserGroups(user) {
Expand Down
22 changes: 17 additions & 5 deletions test/homeDirectories/main/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,23 @@ module.exports = {
},
ldap: ldapAuthConfig,
},
getUserInfo(email) {
return Promise.resolve({
email,
value: 42,
});
async getUserInfo(email, searchLdap) {
if(email.endsWith('zakodium.com')) {
const uid = email.slice(0, email.indexOf('@'));
const data = await searchLdap({
filter: `uid=${uid}`,
attributes: ['mail', 'displayName'],
});
return {
email: data[0].object.mail,
displayName: data[0].object.displayName,
}
} else {
return {
email,
value: 42,
};
}
},
getPublicUserInfo(user) {
return {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/rest-api/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe('LDAP user, developer@zakodium.com', () => {
.then((res) => {
expect(res.body).toBeDefined();
expect(res.body).toStrictEqual({
value: 42,
displayName: 'Developer User',
email: 'developer@zakodium.com',
});
});
Expand Down

0 comments on commit 9e41fd6

Please sign in to comment.