Node JS LDAP Auth User

后端 未结 3 1342

I am creating a login authentication page, where a user would input there active directory username and password and using NodeJS I would check to see if it\'s valid, but I

3条回答
  •  盖世英雄少女心
    2020-12-23 15:42

    @Sukh Thank you for posting your UPDATE solution; however, there is a problem with the code you posted in your UPDATE. While it works for simple cases, with larger queries, you will find you are unbinding before the results have been output. The solution for me was to move your unbinds into the search.on functions.

    Here is an edit of your UPDATE:

    var ldap = require('ldapjs');
    ldap.Attribute.settings.guid_format = ldap.GUID_FORMAT_B;
    var client = ldap.createClient({
          url: 'ldap://batman.com/cn='+username+', ou=users, ou=compton, dc=batman, dc=com',
          timeout: 5000,
          connectTimeout: 10000
    });
    var opts = {
      filter: '(&(objectclass=user)(samaccountname='+username+'))',
      scope: 'sub',
      //attributes: ['objectGUID']
      // This attribute list is what broke your solution
      attributes: ['objectGUID','sAMAccountName','cn','mail','manager','memberOf']
    };
    
    console.log('--- going to try to connect user ---');
    
    try {
        client.bind(username, password, function (error) {
            if(error){
                console.log(error.message);
                client.unbind(function(error) {if(error){console.log(error.message);} else{console.log('client disconnected');}});
            } else {
                console.log('connected');
                client.search('ou=users, ou=compton, dc=batman, dc=com', opts, function(error, search) {
                    console.log('Searching.....');
    
                    search.on('searchEntry', function(entry) {
                        if(entry.object){
                            console.log('entry: %j ' + JSON.stringify(entry.object));
                        }
                        client.unbind(function(error) {if(error){console.log(error.message);} else{console.log('client disconnected');}});
                    });
    
                    search.on('error', function(error) {
                        console.error('error: ' + error.message);
                        client.unbind(function(error) {if(error){console.log(error.message);} else{console.log('client disconnected');}});
                    });
    
                    // don't do this here
                    //client.unbind(function(error) {if(error){console.log(error.message);} else{console.log('client disconnected');}});
                });
            }
        });
    } catch(error){
        console.log(error);
        client.unbind(function(error) {if(error){console.log(error.message);} else{console.log('client disconnected');}});
    }
    

    At least this is what I discovered when using your solution with Active Directory searches. memberOf returns A LOT of entries in my use case and the unbinds were being done prematurely, so I was getting the following error:

    error: 1__ldap://my.domain.com/,OU=Employees,OU=Accounts,DC=my,DC=domain,DC=com closed
    client disconnected
    

提交回复
热议问题