@@ -87,6 +87,7 @@ function LdapAuth(opts) {
87
87
this . opts . bindProperty || ( this . opts . bindProperty = 'dn' ) ;
88
88
this . opts . groupSearchScope || ( this . opts . groupSearchScope = 'sub' ) ;
89
89
this . opts . groupDnProperty || ( this . opts . groupDnProperty = 'dn' ) ;
90
+ this . opts . tlsStarted = false ;
90
91
91
92
EventEmitter . call ( this ) ;
92
93
@@ -126,21 +127,7 @@ function LdapAuth(opts) {
126
127
this . _userClient . on ( 'error' , this . _handleError . bind ( this ) ) ;
127
128
128
129
var self = this ;
129
- if ( this . opts . starttls ) {
130
- // When starttls is enabled, this callback supplants the 'connect' callback
131
- this . _adminClient . starttls ( this . opts . tlsOptions , this . _adminClient . controls , function ( err ) {
132
- if ( err ) {
133
- self . _handleError ( err ) ;
134
- } else {
135
- self . _onConnectAdmin ( ) ;
136
- }
137
- } ) ;
138
- this . _userClient . starttls ( this . opts . tlsOptions , this . _userClient . controls , function ( err ) {
139
- if ( err ) {
140
- self . _handleError ( err ) ;
141
- }
142
- } ) ;
143
- } else if ( opts . reconnect ) {
130
+ if ( opts . reconnect && ! this . opts . starttls ) {
144
131
this . once ( '_installReconnectListener' , function ( ) {
145
132
self . log && self . log . trace ( 'install reconnect listener' ) ;
146
133
self . _adminClient . on ( 'connect' , function ( ) {
@@ -401,6 +388,37 @@ LdapAuth.prototype._findGroups = function (user, callback) {
401
388
*/
402
389
LdapAuth . prototype . authenticate = function ( username , password , callback ) {
403
390
var self = this ;
391
+ if ( this . opts . starttls && ! this . opts . tlsStarted ) {
392
+ // When starttls is enabled, this callback supplants the 'connect' callback
393
+ this . _adminClient . starttls ( this . opts . tlsOptions , this . _adminClient . controls , function ( err ) {
394
+ if ( err ) {
395
+ self . _handleError ( err ) ;
396
+ } else {
397
+ self . _onConnectAdmin ( function ( ) { self . _handleAuthenticate ( username , password , callback ) ; } ) ;
398
+ }
399
+ } ) ;
400
+ this . _userClient . starttls ( this . opts . tlsOptions , this . _userClient . controls , function ( err ) {
401
+ if ( err ) {
402
+ self . _handleError ( err ) ;
403
+ }
404
+ } ) ;
405
+ } else {
406
+ self . _handleAuthenticate ( username , password , callback ) ;
407
+ }
408
+ } ;
409
+
410
+ /**
411
+ * Authenticate given credentials against LDAP server
412
+ *
413
+ * @private
414
+ * @param {string } username - The username to authenticate
415
+ * @param {string } password - The password to verify
416
+ * @param {resultCallback } callback - Result handling callback
417
+ * @returns {undefined }
418
+ */
419
+ LdapAuth . prototype . _handleAuthenticate = function ( username , password , callback ) {
420
+ this . opts . tlsStarted = true ;
421
+ var self = this ;
404
422
405
423
if ( typeof password === 'undefined' || password === null || password === '' ) {
406
424
return callback ( new Error ( 'no password given' ) ) ;
0 commit comments