I have an Ldap Server running on Docker + ldapjs. This server is adding a set of records that I am trying to search for with the client. A sample user object looks like below:
{
user: 'cn=first.last,ou=user_group,o=main',
info: {
cn: 'first.last',
email: 'first.last@mail.com'
}
}
The options would look like this:
let opts = {
scope: 'base',
attributes: ['dn', 'sn', 'cn', 'user', 'info']
};
I'm using this code in a class, so I bind in the constructor, after initializing the client:
constructor(url) {
client = ldap.createClient({
url: url
});
client.on('error', (err) => {
log.error(`${err}`);
});
client.bind(username, password, function (err) {
if (err) {
log.error(`${err}`);
}
});
log.info('Client Initialized.');
};
And my search code:
return new Promise((resolve, reject) => {
var record = {};
client.search(username, opts, function (err, res) {
res.on('searchEntry', function (entry) {
log.info(`Record Retrieved: ${JSON.stringify(entry.object)}`);
record = entry.object;
});
res.on('error', function (err) {
log.error(`Error: ${err.message}`);
});
res.on('end', function (result) {
if (err) {
reject(err);
}
else {
log.info(`Status: ${result.status}`);
resolve(record);
}
});
});
});
The issue I'm experiencing is that the code will always resolve on end when I make a search request from the client, which means that I never get a match, although it's definitely there.
I've tried:
I connect to the server ok, bind is ok as well, so I think I'm either doing the search wrong, or the way I have structured the users in the server is not proper.
Added screenshot showing server logs: The user added in the entry looks like it has a different name, but I changed it to match in the data.
I've found the issue, which was related to the structure I was using in my records, I've solved it using an ldapts client instead, but the same logic can be used in an ldapjs client:
Specifically: This is a record in my ldapjs Server:
{
name: 'John Doe',
uid: 'john.doe',
dn: 'uid=john.doe, ou=users, o=server',
email: 'john.doe@email.com',
userprincipalname: 'cgi-doej',
}
This is how I search for it:
let attributes = ['cn'], filter = `(email=${email})`
const { searchEntries, searchReferences } = await this.client.search(searchDN, {
scope: 'base',
filter: filter,
attributes: attributes
});
This has solved my issues.