1

Been playing around with NodeJS in the past few days and run stuck with LDAP connection using ldapjs module. Background story, the Active-Directory server, that I am trying to connect to, supports LDAPv3.

Below is the code I used to connect to the LDAP server:

var ldapClient = ldap.createClient({url: 'ldap://ldap.myhost.com.au:389'}, function (err){
  if (err) {
    console.log('Failed');
    process.exit(1);
  }
  else
    console.log('Through');
});

In the given example, it doesn't include the last callback function, I tried it as the common pattern mentioned that most operations include callback so I thought I could have a bit of luck with it, I didn't.

I have also tried with and without the callback, still didn't work.

The ldapClient container will always return true even when I change the url into an invalid one.

-- Up to this point it has been explained by James Thomas' answer below --

EDIT:

I tried binding the user onto LDAP and do a search query as the snippet below:

ldapClient.bind('cn=drupal,ou=SystemAccount,dc=myhost,dc=com,dc=au', '', function (err) {
  if (err) {
    console.log('LDAP binding failed... disconnecting');
    process.exit(1);
  }
});

var opts = {
  scope: 'sub'
};

var result = ldapClient.search('ou=Users,dc=myhost,dc=com,dc=au', opts, function(err, res) {
  assert.ifError(err);

  res.on('searchEntry', function(entry) {
    console.log('entry: ' + JSON.stringify(entry));
  });
  res.on('searchReference', function(referral) {
    console.log('referral: ' + referral.uris.join());
  });
  res.on('error', function(err) {
    console.error('error: ' + err.message);
  });
  res.on('end', function(result) {
    console.log('status: ' + result.status);
  });
});

This code prints nothing to the screen and result variable that is used to hold the search method return value returned 'true'.

Anyone could share how it's done?

Community
  • 1
  • 1
Chris Wijaya
  • 1,276
  • 3
  • 16
  • 34

1 Answers1

3

The ldapjs.createClient() call doesn't connect to the server when you execute the function, rather sets up the credentials to authentication that you use when calling the methods on the returned "client" object.

For example, you need to do the following:

var client = ldap.createClient({url: 'ldap://ldap.myhost.com.au:389'})
var entry = {
  cn: 'foo',
  sn: 'bar',
  email: ['foo@bar.com', 'foo1@bar.com'],
  objectclass: 'fooPerson'
};
client.add('cn=foo, o=example', entry, function(err) {
  assert.ifError(err);
});
James Thomas
  • 4,303
  • 1
  • 20
  • 26
  • That explained something. I tried the search method after initializing the client, the code is quite straight forward, I'll add them on my question above. But it returns 'true' instead of the object. – Chris Wijaya Apr 15 '15 at 22:48