Search code examples
javascriptangularjsnode.jsldapldap-query

LDAP JS with Node JS Finding users passing in patterns of string


I am currently using LDAP JS for Authentication in Angular JS app and everything works perfectly fine.

I am now building a new view and the requirement I have is this:

  • I have text box in which admin will write may be a few letters of a user id present in LDAP.
  • I want to show app matching ID present in LDAP on a typeahead/suggestions. I know how typeahead works so that's not an issue. The issue is how can I pass a rejex or pattern matching kind of a thing for uid in search() method.

My sample code is here:

function GetAllLDAPUser(dnFilter, res) {

    client.search('uid=**WHAT-PATTERN-CAN-I-PASS-HERE**' + dnFilter, opts, function(err, result) {

            result.on('searchEntry', function(entry) {

                // I usually read entry.object or entry.raw here , that works

            });
            result.on('end', function(result) {

                .......
            });
        }
    }
}

So the question is what should I pass in place of WHAT-PATTERN-CAN-I-PASS-HERE

Results : Suppose I type an. The typeahead will show all user id starting with an like ana, anamon, analisa etc.


Solution

  • I have written the final solution and closed the issue on the project's repository

    For pattern matching, we need to play with the 'filter' field in option object which we pass to the search method. So I ended up doing something like below:

    var dnFilter = 'ou=People,o=Intra,dc=YOURCOMPANY,dc=com'; //depends on your LDAP settings.
    var query;
    var matchedUsers = [];
    query.LDAPName = "dummy"; //some name which resides in LDAP 
    //You can even have one simple variable rather than having this query object.
    opts = {
        scope: 'sub',
        filter: (shcDisplayName = '+ query.LDAPName + ')
        '
    };
    
    //Do not use 'shcDisplayName' , this will be any variable stored in your LDAP object. You need get 
    //the structure of LDAP end point you are working on. For me, I had one variable 'shcDisplayName'
    //on which I wanted to play so I am using this variable in my filter.
    client.search(dnFilter, opts, function(err, result) {
    
            result.on('searchEntry', function(entry) {
                    matchedUsers.push({
                        'Name': entry.object.shcDisplayName,
                        'Id': entry.object.uid
                    });
                }
    
                result.on('end', function(result) {
    
                        if (matchedUsers.length) { //if any match was found.
    
                            //send the json result back
                            res.json(matchedUsers);
    
                            //if you want to send json back, do not use res.send() otherwise you will end up getting
                            //circular reference error. 
                        }
                    }
    
                    result.on('error', function(ex) {
                        //Handle errors here if any
                    });
    
                });
        }
    }