vesse / node-ldapauth-fork

Simple node.js module to authenticate against an LDAP server
Other
126 stars 79 forks source link

Option to not connect automatically #37

Closed grimurd closed 7 years ago

grimurd commented 8 years ago

Is it possible to get an option to not connect to the LDAP service automatically when the class is created?

I'm using dependency injection and creating the ldapauth class in the constructor of my service but it isn't always used and in those cases the connection doesn't close properly and it crashes my application.

Example use case:

const LdapAuth = require('ldapauth-fork');

class LDAPService {
    constructor() {
        this.ldap = new LdapAuth({
            autoConnect: false // Default to true for backwards compatibility
        });
    }

    authenticateLdap(username, password, callback) {
        // This connect() method would need to be implemented
        this.ldap.connect(function(err) {
            if(err) callback(err);

            this.ldap.authenticate(username,password, function(err, user) {
                if(err) callback(err);

                this.ldap.close(function(err) {
                    if(err) callback(err);

                    callback(null, user);
                });
            });
        });
    }
}
vesse commented 7 years ago

Hi,

Don't know if this problem is still timely, but technically sure - would need to check if the client is created, and if not create it. However I don't know how long it takes for the client to connect. If that would be just before the admin client is being bound, the binding probably would fail because of not being connected (ldapjs source)