diff --git a/lib/activedirectory.js b/lib/activedirectory.js index a54b294..67f29a1 100755 --- a/lib/activedirectory.js +++ b/lib/activedirectory.js @@ -913,6 +913,10 @@ ActiveDirectory.prototype.authenticate = function authenticate (username, passwo } const client = utils.createClient(null, null, this) + client.on('connectTimeout', (err) => { + this.emit('error', err) + return callback(err, false) + }) client.on('error', (err) => { // only used on socket connection failure since it doesn't invoke bind cb this.emit('error', err) diff --git a/lib/components/search.js b/lib/components/search.js index ae23632..74ef08b 100644 --- a/lib/components/search.js +++ b/lib/components/search.js @@ -58,6 +58,10 @@ function Searcher (opts) { this.rangeProcessing = false this.client = utils.createClient(ad.url || ad.opts.url, this.ldapOpts, ad) + this.client.on('connectTimeout', (err) => { + // to handle connection errors + this.callback(err) + }) this.client.on('error', (err) => { // to handle connection errors this.callback(err) diff --git a/test/find.js b/test/find.js index 8f88204..27bb52b 100644 --- a/test/find.js +++ b/test/find.js @@ -3,6 +3,7 @@ /* eslint-disable no-unused-expressions */ const expect = require('chai').expect +const ldapjs = require('ldapjs') const ActiveDirectory = require('../index') const config = require('./config') @@ -215,5 +216,16 @@ describe('find Method', function () { done() }) }) + + it('should return err (ConnectionError) when connection timeouts', function (done) { + new ActiveDirectory({ + url: 'ldap://example.com', + connectTimeout: 100 + }).find({}, function (err, result) { + expect(err).to.be.an.instanceOf(ldapjs.ConnectionError) + expect(result).to.be.undefined + done() + }) + }) }) }) diff --git a/test/network.js b/test/network.js index de57ca1..8a0d5fe 100644 --- a/test/network.js +++ b/test/network.js @@ -3,6 +3,7 @@ /* eslint-disable no-unused-expressions */ const expect = require('chai').expect +const ldapjs = require('ldapjs') const ActiveDirectory = require('../index') describe('Network Connections', function () { @@ -33,5 +34,17 @@ describe('Network Connections', function () { done() }) }) + + it('should return err (ConnectionError) when connection timeouts', function (done) { + const ad = new ActiveDirectory({ + url: 'ldap://example.com', + connectTimeout: 1 + }) + ad.authenticate(username, password, function (err, auth) { + expect(err).to.be.an.instanceOf(ldapjs.ConnectionError) + expect(auth).to.be.false + done() + }) + }) }) })