Use ldapjs with promise

2019-09-10 12:41发布

问题:

I want to convert the following code to use promise. It is working and output a user's attributes within the active directory.

var client = ldap.createClient({
  url: ldap_url
});

client.bind(ldap_username, ldap_password, function (err) {
    client.search(ldap_dn_search, opts, function (err, search) {
        search.on('searchEntry', function (entry) {
          var user = entry.object;
          // It is working!!!. It outputs all user attributes.
          console.log(user);
        });

    });
}); 

The following is my attempt, butit doesn't output anything.

var Promise = require('promise');
var client_bind = Promise.denodeify(client.bind);
var client_search = Promise.denodeify(client.search);

client_bind(ldap_username, ldap_password)
.then(function(err){
  client_search(ldap_dn_search, opts)
    .then(function(search){
      var search_on = Promise.denodeify(search.on);
      search_on('searchEntry')
        .then(function(entry){
          var user = entry.object;

          // It doesn't output anything !!!
          console.log(user);
        });
      });

    });

回答1:

I had the same problem. Search emits events, so we need something that processes them and passes further along the chain. Here is piece of code, that works for me:

var ldap = require('ldapjs');
var promise = require('bluebird');

var client = ldap.createClient({url: app.settings['ldap']['server']});
var uid;

promise.promisifyAll(client);

function searchPromise(res, notfoundtext) {
  return new Promise(function(resolve, reject) {
    var found = false;
    res.on('searchEntry', function(entry) {
      found = true;
      resolve(entry);
    });
    res.on('error', function(e) {
      reject(e.message);
    });
    res.on('end', function() {
      if (!found) {
        reject(notfoundtext);
      }
    });
  });
}

client.searchAsync(app.settings['ldap']['baseDn'], {filter: '(mail='+credentials.email+')', scope: 'sub'})
  .then(function(res) {
    return searchPromise(res, 'User isn\'t exists.');
  })
  .then(function (entry) {
    uid = entry.object.uid;
    return client.bindAsync(entry.object.dn, credentials.password);
  })
  .then(function() {
    return client.searchAsync('cn='+app.settings['ldap']['group']+',cn=groups,'+app.settings['ldap']['baseDn'], {scope: 'sub', filter: '(memberUid='+uid+')'});
  })
  .then(function(res) {
    return searchPromise(res, 'User is not in group ' + app.settings['ldap']['group']);
  })
  .then(function() {
    console.log('All is ok');
  })
  .catch(function(message) {
    console.log('Error:' + message);
  });

Immediately after the search I add one more step that catches the events, processes them, and passes it further along the chain. This makes the function searchPromise.

Good luck coding )



回答2:

Most likely those methods do require to be called on client as a context, so you will need to bind() them before passing them to Promise.denodeify:

var client_bind = Promise.denodeify(client.bind.bind(client));
var client_search = Promise.denodeify(client.search.bind(client));

Also, a proper use of promises would look like this:

client_bind(ldap_username, ldap_password).then(function() {
    return client_search(ldap_dn_search, opts);
//  ^^^^^^ always return something from the callback
}).then(function(search) { // flatten your chain
    return Promise.denodeify(search.on).call(search, 'searchEntry');
//                                    ^^^^^^ an alternative to `bind`
}).then(function(entry){
    var user = entry.object;
    console.log(user);
}).catch(function(err) { // always catch errors!
    console.error(err);
});


回答3:

Using Bluebird Promises, the easy way to do this is to create your client normally, and then run the promisifyAll() on the client.

var ldap = require('ldapjs');
var Promise = require('bluebird');

var client = ldap.createClient({
  url: 'ldap://my-server:1234',
});

Promise.promisifyAll(client);

Now you can call client.addAsync() and client.searchAsync() and such.

client.bindAsync(secUserDn, secUserPassword)
  .then(doSearch) // if it works, call doSearch
  .catch(function (err) { // if bind fails, handle it
    console.error('Error on bind', err)
  });

function doSearch(data) {
  client.searchAsync('CN=A Test,OU=Users,DC=website,DC=com', options)
    .then(function (data) { // Handle the search result processing
      console.log('I got a result');
    })  
    .catch(function (err) { // Catch potential errors and handle them
      console.error('Error on search', err);
    });
}