@@ -53,6 +53,7 @@ export class SSHSession extends Session {
5353 private _authHandler : AuthHandler ;
5454 private _workDirectory : string ;
5555 private _workDirectoryParser : LineParser ;
56+ private _authsLeft : AuthenticationType [ ] ;
5657
5758 constructor ( c ?: Config , client ?: Client ) {
5859 super ( ) ;
@@ -65,6 +66,7 @@ export class SSHSession extends Session {
6566 WORK_DIR_END_TAG ,
6667 false ,
6768 ) ;
69+ this . _authsLeft = [ ] ;
6870 }
6971
7072 public sessionId ? = ( ) : string => {
@@ -124,20 +126,30 @@ export class SSHSession extends Session {
124126
125127 public close = ( ) : void | Promise < void > => {
126128 if ( ! this . _stream ) {
129+ this . disposeResources ( ) ;
127130 return ;
128131 }
129132 this . _stream . write ( "endsas;\n" ) ;
130133 this . _stream . close ( ) ;
131134 } ;
132135
133136 private onConnectionClose = ( ) => {
137+ if ( ! this . _sessionReady ) {
138+ this . _reject ?.( new Error ( l10n . t ( "Could not connect to the SAS server." ) ) ) ;
139+ }
140+
141+ this . disposeResources ( ) ;
142+ } ;
143+
144+ private disposeResources = ( ) => {
134145 this . _stream = undefined ;
135146 this . _resolve = undefined ;
136147 this . _reject = undefined ;
137148 this . _html5FileName = "" ;
138149 this . _workDirectory = undefined ;
139150 this . clearAuthState ( ) ;
140151 sessionInstance = undefined ;
152+ this . _authsLeft = [ ] ;
141153 } ;
142154
143155 private onConnectionError = ( err : Error ) => {
@@ -297,7 +309,7 @@ export class SSHSession extends Session {
297309
298310 private handleSSHAuthentication : AuthHandlerMiddleware = (
299311 authsLeft : AuthenticationType [ ] ,
300- _partialSuccess : boolean ,
312+ partialSuccess : boolean ,
301313 nextAuth : NextAuthHandler ,
302314 ) => {
303315 if ( ! authsLeft ) {
@@ -313,30 +325,47 @@ export class SSHSession extends Session {
313325 return false ; //returning false will stop the authentication process
314326 }
315327
316- const authMethod = authsLeft . shift ( ) ;
328+ if ( this . _authsLeft . length === 0 || partialSuccess ) {
329+ this . _authsLeft = authsLeft ;
330+ }
331+
332+ const authMethod = this . _authsLeft . shift ( ) ;
333+
317334 switch ( authMethod ) {
318335 case "publickey" : {
319336 //user set a keyfile path in profile config
320337 if ( this . _config . privateKeyFilePath ) {
321- this . _authHandler . privateKeyAuth (
322- nextAuth ,
323- this . _config . privateKeyFilePath ,
324- this . _config . username ,
325- ) ;
338+ this . _authHandler
339+ . privateKeyAuth (
340+ nextAuth ,
341+ this . _config . privateKeyFilePath ,
342+ this . _config . username ,
343+ )
344+ . catch ( ( e ) => {
345+ this . _reject ?.( e ) ;
346+ return false ;
347+ } ) ;
326348 } else if ( process . env . SSH_AUTH_SOCK ) {
327349 this . _authHandler . sshAgentAuth ( nextAuth , this . _config . username ) ;
328350 }
329351 break ;
330352 }
331353 case "password" : {
332- this . _authHandler . passwordAuth ( nextAuth , this . _config . username ) ;
354+ this . _authHandler
355+ . passwordAuth ( nextAuth , this . _config . username )
356+ . catch ( ( e ) => {
357+ this . _reject ?.( e ) ;
358+ return false ;
359+ } ) ;
333360 break ;
334361 }
335362 case "keyboard-interactive" : {
336- this . _authHandler . keyboardInteractiveAuth (
337- nextAuth ,
338- this . _config . username ,
339- ) ;
363+ this . _authHandler
364+ . keyboardInteractiveAuth ( nextAuth , this . _config . username )
365+ . catch ( ( e ) => {
366+ this . _reject ?.( e ) ;
367+ return false ;
368+ } ) ;
340369 break ;
341370 }
342371 default :
0 commit comments