Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add connect callback for adminClient to rebind on reconnect #68

Merged
merged 2 commits into from
Dec 3, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 41 additions & 16 deletions lib/ldapauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ function LdapAuth(opts) {

this._adminClient.on('error', this._handleError.bind(this));
this._userClient.on('error', this._handleError.bind(this));

var self = this;
this._adminClient.on('connect', function() {
self._onConnectAdmin();
});

this._adminClient.on('connectTimeout', this._handleError.bind(this));
this._userClient.on('connectTimeout', this._handleError.bind(this));

Expand Down Expand Up @@ -159,33 +165,52 @@ LdapAuth.prototype._handleError = function(err) {
};

/**
* Ensure that `this._adminClient` is bound.
* Bind adminClient to the admin user on connect
*
* @private
* @param {voidCallback} callback - Callback that checks possible error
* @param {voidCallback} callback - Callback that checks possible error, optional
* @returns {undefined}
*/
LdapAuth.prototype._adminBind = function(callback) {
LdapAuth.prototype._onConnectAdmin = function(callback) {
var self = this;

// Anonymous binding
if (typeof this.bindDN === 'undefined' || this.bindDN === null) {
return callback();
}
if (this._adminBound) {
return callback();
if (typeof self.bindDN === 'undefined' || self.bindDN === null) {
self._adminBound = true;
return callback ? callback() : null;
}
var self = this;
this._adminClient.bind(
this.bindDN,
this.bindCredentials,

self.log && self.log.trace('ldap authenticate: bind: %s', self.bindDN);
self._adminClient.bind(
self.bindDN,
self.bindCredentials,
function(err) {
if (err) {
self.log && self.log.trace('ldap authenticate: bind error: %s', err);
return callback(err);
self._adminBound = false;
return callback ? callback(err) : null;
}

self.log && self.log.trace('ldap authenticate: bind ok');
self._adminBound = true;
return callback();
}
);
return callback ? callback() : null;
});
}

/**
* Ensure that `this._adminClient` is bound.
*
* @private
* @param {voidCallback} callback - Callback that checks possible error
* @returns {undefined}
*/
LdapAuth.prototype._adminBind = function(callback) {
if (this._adminBound) {
return callback();
}

// Call the connect handler with a callback
return this._onConnectAdmin(callback);
};

/**
Expand Down