jeremycx / node-LDAP

LDAP binding for node.js
MIT License
221 stars 43 forks source link

Close connection causes crash #103

Closed jaredmeakin closed 7 years ago

jaredmeakin commented 7 years ago

Just wondering if there is a proper way to close a connection?

What I'm "trying" to do is follow the pattern in the documentation. Start with a bind() using an admin account, and then findandbind() to authenticate the user. After that I'd like to close both connections.

When I close() inside of my findandbind() callback I get the below error from OpenLDAP (osixia/openldap). Testing against Microsoft AD yields no such error. Looking at the OpenLDAP logs it appears that after every search the initial/admin connection sends ABANDON, is that expected behavior? Does the library automatically close connections when done?

.../node_modules/ldap-client/index.js:255
        this.ld.abandon(msgid);
               ^

TypeError: Cannot read property 'abandon' of undefined
    at LDAP.searchTimeout (.../node_modules/ldap-client/index.js:255:16)
    at ontimeout (timers.js:469:11)
    at tryOnTimeout (timers.js:304:5)
    at Timer.listOnTimeout (timers.js:264:5)

Here's the code:

const express = require('express');
const router = express.Router();
const ldapClient = require('ldap-client');
const ldapUserSearch = ldapClient.escapefn('filter', '(&(objectClass=*)(name=%s))');

...

router.post('/', function(req, res, next) {
  const ldap = new ldapClient({
    uri: 'ldap://localhost',
    base: 'dc=example,dc=com',
    attrs: 'displayName'
  });

  ldap.bind({
    binddn: 'cn=admin,dc=example,dc=com',
    password: 'adminpassword'
  }, function(err) {
    if (err) {
      return res.render('index', { error: err.message });
    }

    ldap.findandbind({
      filter: ldapUserSearch(req.body.username),
      password: req.body.password,
    }, function(err, data) {
      if (err) {
        return res.render('index', { error: 'Invalid username and/or password.' });
      } else {
        ldap.close();
        return res.render('servers', { user: data.displayName });
      }
    })
  });
});