Node-ldapjs: simple example to search for username

Created on 5 Apr 2017  路  10Comments  路  Source: ldapjs/node-ldapjs

Sorry for the newb question - I see how to create an ldap client, that is all well and good, but I am looking for an example of how to search for a particular user's information in LDAP.

I know that the language of LDAP is pretty universal, but I am looking for a good example of how to do this with LDAPjs.

thanks

question

Most helpful comment

Here is a Nodejs example searching Active Directory by userPrincipalName for a user and it console logs their name attribute.

const ldap = require('ldapjs');
const assert = require('assert');

// LDAP Connection Settings
const server = "dns or ip here"; // 192.168.1.1
const userPrincipalName = "[email protected]"; // Username
const password = "myPassword"; // User password
const adSuffix = "dc=test,dc=com"; // test.com

// Create client and bind to AD
const client = ldap.createClient({
    url: `ldap://${server}`
});

client.bind(userPrincipalName,password,err => {
    assert.ifError(err);
});

// Search AD for user
const searchOptions = {
    scope: "sub",
    filter: `(userPrincipalName=${userPrincipalName})`
};

client.search(adSuffix,searchOptions,(err,res) => {
    assert.ifError(err);

    res.on('searchEntry', entry => {
        console.log(entry.object.name);
    });
    res.on('searchReference', referral => {
        console.log('referral: ' + referral.uris.join());
    });
    res.on('error', err => {
        console.error('error: ' + err.message);
    });
    res.on('end', result => {
        console.log(result);
    });
});

// Wrap up
client.unbind( err => {
    assert.ifError(err);
});

All 10 comments

Here is a Nodejs example searching Active Directory by userPrincipalName for a user and it console logs their name attribute.

const ldap = require('ldapjs');
const assert = require('assert');

// LDAP Connection Settings
const server = "dns or ip here"; // 192.168.1.1
const userPrincipalName = "[email protected]"; // Username
const password = "myPassword"; // User password
const adSuffix = "dc=test,dc=com"; // test.com

// Create client and bind to AD
const client = ldap.createClient({
    url: `ldap://${server}`
});

client.bind(userPrincipalName,password,err => {
    assert.ifError(err);
});

// Search AD for user
const searchOptions = {
    scope: "sub",
    filter: `(userPrincipalName=${userPrincipalName})`
};

client.search(adSuffix,searchOptions,(err,res) => {
    assert.ifError(err);

    res.on('searchEntry', entry => {
        console.log(entry.object.name);
    });
    res.on('searchReference', referral => {
        console.log('referral: ' + referral.uris.join());
    });
    res.on('error', err => {
        console.error('error: ' + err.message);
    });
    res.on('end', result => {
        console.log(result);
    });
});

// Wrap up
client.unbind( err => {
    assert.ifError(err);
});

Sorry to revive a rather old post, but can someone adjust this to get a list of groups the user is a member of?
The above is the simplest I've seen so far and I'm hoping it doesn't get more complicated.

@AlistairHardy It's in the entry.object on the searchEntry event. I believe the groups were in value memberOf inside the entry object.

You can see everything if you console.log(entry.object)

I believe this has been sufficiently answered. If you are still having issues, feel free to re-open.

If the search can't find a user, no error returns to me

If the search can't find a user, no error returns to me

If no data is found it won't throw an error, since the absence of data is a valid response for LDAP. If you want it to throw an error when there is no user found you'll need to check the response and throw manually when there is no data.

Think of it like searching a database.

@tastypackets How can i verify if the response not found data?

@AlistairHardy It's in the entry.object on the searchEntry event. I believe the groups were in value memberOf inside the entry object.

You can see everything if you console.log(entry.object)

it's not called the searchEntry event, do you have any idea why please ? (I used your code above)

Isn't there a problem if the search is performed before the bind has returned? Bind being asynchronous...

hey , if you want to verify if user is found or not
let users = [];
response.on('searchEntry', function(entry) {
users.push(entry.object);
});
response.on('error', function(err) {
console.error('error: ' + err.message);
});
/// check if user is found or not by checking the length of users array
response.on('end', function() {
if(users.length < 1){
console.log("no users found")
const error = new Error("some error")
next(error)
}else{
// user is in active
console.log("user found !")
}

Was this page helpful?
0 / 5 - 0 ratings

Related issues

moehlone picture moehlone  路  3Comments

djholly123 picture djholly123  路  3Comments

UziTech picture UziTech  路  4Comments

sorccu picture sorccu  路  5Comments

abarik1981 picture abarik1981  路  4Comments