question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

search.on('end') fires before the entry is found

See original GitHub issue

I’m using ldapjs client libraries to connect to an Active Directory server, everything works if the login succeeds and the user is found, but only if there’s no search.on(‘end’) specified. If I ad the ‘end’ listener, then it gets called BEFORE the user is found, so when I try to return the response, it crashes the app.

THIS DOESN’T WORK

            client.search('ou=users, dc=company, dc=com', opts, function(error, search) {
                    console.log('Searching.....');

                    search.on('searchEntry', function(entry) {
                        if (entry.object) {
                            userClient = ldap.createClient({url: config.ldap.url, timeout: 5000, connectTimeout: 10000});
                            userClient.bind(entry.object.dn, password, function(err) {
                                if (err == null) {
                                    res.send({status: "OK", sessionID: sessionID});
                                } else {
                                    res.send("FAIL - BAD LOGIN");
                                }
                            });
                        }
                    });

                    search.on('end', function(){
                        res.send("FAIL - not found");
                    });

THIS WORKS, BUT ONLY IF THE USER IS FOUND, IF NOT, APP CRASHES.

            client.search('ou=users, dc=company, dc=com', opts, function(error, search) {
                    console.log('Searching.....');

                    search.on('searchEntry', function(entry) {
                        if (entry.object) {
                            userClient = ldap.createClient({url: config.ldap.url, timeout: 5000, connectTimeout: 10000});
                            userClient.bind(entry.object.dn, password, function(err) {
                                if (err == null) {
                                    res.send({status: "OK", sessionID: sessionID});
                                } else {
                                    res.send("FAIL - BAD LOGIN");
                                }
                            });
                        }
                    });

All I want to do is return a failed login if the user does not exist on the server. And also determine if the login attempt failed by the password.

Any help would be greatly appreciated.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:3
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
maziyarpanahicommented, Jan 22, 2017

here is how I did it. I have a login process as follow: search the LDAP and bind it. So either way I need the userObject or a null value to decide whether bind/search has failed:

let userObject = {};
    client.on('connect', function (e) {
        console.log('LDAP connected:');
        client.search(baseDN, opts, function(err, res) {
            res.on('searchEntry', function(entry) {               
                userObject = {
                    username: entry.object.uid,
                    firstname: entry.object.givenName,
                    lastname: entry.object.sn,
                    name: entry.object.cn,
                    email: entry.object.mail,
                    description: entry.object.description,
                    uidNumber: entry.object.uidNumber
                };
                client.bind(entry.object.dn, password, function(err, res) {
                    if(err){
                        // Bind failed, return null
                        callback(null);
                    }else{
                        // Bind was successful, return userObject                      
                        callback(userObject);
                    }
                });
            });
            res.on('searchReference', function(referral) {
                console.log('referral: ' + referral.uris.join());
            });
            res.on('error', function(err) {
                // client/TCP errors, return null
                callback(null);
            });
            res.on('end', function(result) {
                if(userObject.hasOwnProperty("username")){
                   // search was successful, will return null/objectUser in  client.bind
                  // no action needed here
                    console.log("finished success");
                }else{
                    // search failed, return null
                    callback(null);
                }
            });
        });
    });

I am using the latest version and res.end always returns null if it can’t find anything. I don’t really care if the search was successful as you can see. But the failed part always happens when the search fails. It’s unfortunate that in “search” the error is not the same as other functions:

Note that the error event will only be for client/TCP errors, not LDAP error codes like the other APIs

Hope this helps.

1reaction
victorbellocommented, Jun 23, 2016

I thought the same thing, but end fires before searchEntry even when there is a record found. I haven’t figured out a way to retrieve an login error with this library.

Read more comments on GitHub >

github_iconTop Results From Across the Web

A firefighter's guide to fireground search and rescue – Part 2
The mission of a search and rescue team in a fire situation is to find victims ... bypass before entry eliminates them from...
Read more >
How do you detect the clearing of a "search" HTML5 input?
I found this answer on another post and it works perfect for me and only fires when the user clears the search box....
Read more >
Michigan v. Clifford :: 464 U.S. 287 (1984)
On interlocutory appeal, the Michigan Court of Appeals found that no exigent ... (a) Where a warrant is necessary to search fire-damaged premises, ......
Read more >
search warrant | Wex | US Law | LII / Legal Information Institute
A search warrant is a warrant signed by a judge or magistrate authorizing a law ... basement to find the cause of fire,...
Read more >
Specify time modifiers in your search - Splunk Documentation
latest=now, Specify that the search starts or ends at the current time. ... 24 hours before the search is run, up to midnight;...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found