@@ -71,13 +71,13 @@ var PlainClient = function(username, password) {
7171 this . password = password ;
7272} ;
7373
74- PlainClient . prototype . start = function ( ) {
74+ PlainClient . prototype . start = function ( callback ) {
7575 var response = new Buffer ( 1 + this . username . length + 1 + this . password . length ) ;
7676 response . writeUInt8 ( 0 , 0 ) ;
7777 response . write ( this . username , 1 ) ;
7878 response . writeUInt8 ( 0 , 1 + this . username . length ) ;
7979 response . write ( this . password , 1 + this . username . length + 1 ) ;
80- return response ;
80+ callback ( undefined , response ) ;
8181} ;
8282
8383var AnonymousServer = function ( ) {
@@ -94,11 +94,11 @@ var AnonymousClient = function(name) {
9494 this . username = name ? name : 'anonymous' ;
9595} ;
9696
97- AnonymousClient . prototype . start = function ( ) {
97+ AnonymousClient . prototype . start = function ( callback ) {
9898 var response = new Buffer ( 1 + this . username . length ) ;
9999 response . writeUInt8 ( 0 , 0 ) ;
100100 response . write ( this . username , 1 ) ;
101- return response ;
101+ callback ( undefined , response ) ;
102102} ;
103103
104104var ExternalServer = function ( ) {
@@ -114,20 +114,20 @@ var ExternalClient = function() {
114114 this . username = undefined ;
115115} ;
116116
117- ExternalClient . prototype . start = function ( ) {
118- return '' ;
117+ ExternalClient . prototype . start = function ( callback ) {
118+ callback ( undefined , '' ) ;
119119} ;
120120
121- ExternalClient . prototype . step = function ( ) {
122- return '' ;
121+ ExternalClient . prototype . step = function ( callback ) {
122+ callback ( undefined , '' ) ;
123123} ;
124124
125125var XOAuth2Client = function ( username , token ) {
126126 this . username = username ;
127127 this . token = token ;
128128} ;
129129
130- XOAuth2Client . prototype . start = function ( ) {
130+ XOAuth2Client . prototype . start = function ( callback ) {
131131 var response = new Buffer ( this . username . length + this . token . length + 5 + 12 + 3 ) ;
132132 var count = 0 ;
133133 response . write ( 'user=' , count ) ;
@@ -144,7 +144,7 @@ XOAuth2Client.prototype.start = function() {
144144 count += 1 ;
145145 response . writeUInt8 ( 1 , count ) ;
146146 count += 1 ;
147- return response ;
147+ callback ( response ) ;
148148} ;
149149
150150/**
@@ -228,25 +228,41 @@ SaslClient.prototype.on_sasl_mechanisms = function (frame) {
228228 var mech = frame . performative . sasl_server_mechanisms [ i ] ;
229229 var f = this . mechanisms [ mech ] ;
230230 if ( f ) {
231- this . mechanism = f ( ) ;
231+ this . mechanism = typeof f === 'function' ? f ( ) : f ;
232232 this . mechanism_name = mech ;
233233 }
234234 }
235235 if ( this . mechanism ) {
236- var response = this . mechanism . start ( ) ;
237- var init = { 'mechanism' :this . mechanism_name , 'initial_response' :response } ;
238- if ( this . hostname ) {
239- init . hostname = this . hostname ;
236+ var self = this ;
237+ this . mechanism . start ( function ( err , response ) {
238+ if ( err ) {
239+ self . failed = true ;
240+ self . connection . sasl_failed ( 'SASL mechanism init failed: ' + err ) ;
241+ } else {
242+ var init = { 'mechanism' :self . mechanism_name , 'initial_response' :response } ;
243+ if ( self . hostname ) {
244+ init . hostname = self . hostname ;
245+ }
246+ self . transport . encode ( frames . sasl_frame ( frames . sasl_init ( init ) . described ( ) ) ) ;
247+ self . connection . output ( ) ;
240248 }
241- this . transport . encode ( frames . sasl_frame ( frames . sasl_init ( init ) . described ( ) ) ) ;
249+ } ) ;
242250 } else {
243251 this . failed = true ;
244252 this . connection . sasl_failed ( 'No suitable mechanism; server supports ' + frame . performative . sasl_server_mechanisms ) ;
245253 }
246254} ;
247255SaslClient . prototype . on_sasl_challenge = function ( frame ) {
248- var response = this . mechanism . step ( frame . performative . challenge ) ;
249- this . transport . encode ( frames . sasl_frame ( frames . sasl_response ( { 'response' :response } ) . described ( ) ) ) ;
256+ var self = this ;
257+ this . mechanism . step ( frame . performative . challenge , function ( err , response ) {
258+ if ( err ) {
259+ self . failed = true ;
260+ self . connection . sasl_failed ( 'SASL mechanism challenge failed: ' + err ) ;
261+ } else {
262+ self . transport . encode ( frames . sasl_frame ( frames . sasl_response ( { 'response' :response } ) . described ( ) ) ) ;
263+ self . connection . output ( ) ;
264+ }
265+ } ) ;
250266} ;
251267SaslClient . prototype . on_sasl_outcome = function ( frame ) {
252268 switch ( frame . performative . code ) {
0 commit comments