Skip to content
This repository has been archived by the owner on May 14, 2024. It is now read-only.

simple example to search for username #428

Closed
ORESoftware opened this issue Apr 5, 2017 · 11 comments
Closed

simple example to search for username #428

ORESoftware opened this issue Apr 5, 2017 · 11 comments
Labels

Comments

@ORESoftware
Copy link

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

@tastypackets
Copy link

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 = "test@test.com"; // 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);
});

@AlistairHardy
Copy link

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.

@tastypackets
Copy link

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

@jsumners
Copy link
Member

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

@evertonpavan
Copy link

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

@tastypackets
Copy link

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.

@evertonpavan
Copy link

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

@amineharbaoui
Copy link

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

@niclasek
Copy link

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

@Gil-Porsansky
Copy link

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 !")
}

@jsumners
Copy link
Member

⚠️ This issue has been locked due to age. If you have encountered a recent
problem that seems to be covered by this issue, please open a new issue.

Please include a minimal reproducible example
when opening a new issue.

@ldapjs ldapjs locked as resolved and limited conversation to collaborators Mar 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

8 participants