public void authenticate ( AuthenticationProtocol authentication , String servicename ) throws SshException , AuthenticationResult { try { if ( getUsername ( ) == null || getPassword ( ) == null ) { throw new SshException ( "Username or password not set!" , SshException . BAD_API_USAGE ) ; } if ( passwordChangeRequired && newpassword == null ) { throw new SshException ( "You must set a new password!" , SshException . BAD_API_USAGE ) ; } ByteArrayWriter msg = new ByteArrayWriter ( ) ; try { msg . writeBoolean ( passwordChangeRequired ) ; msg . writeString ( getPassword ( ) ) ; if ( passwordChangeRequired ) { msg . writeString ( newpassword ) ; } authentication . sendRequest ( getUsername ( ) , servicename , "password" , msg . toByteArray ( ) ) ; } finally { try { msg . close ( ) ; } catch ( IOException e ) { } } // We need to read the response since we may have password change. byte [ ] response = authentication . readMessage ( ) ; if ( response [ 0 ] != SSH_MSG_USERAUTH_PASSWD_CHANGEREQ ) { authentication . transport . disconnect ( TransportProtocol . PROTOCOL_ERROR , "Unexpected message received" ) ; throw new SshException ( "Unexpected response from Authentication Protocol" , SshException . PROTOCOL_VIOLATION ) ; } passwordChangeRequired = true ; throw new AuthenticationResult ( SshAuthentication . FAILED ) ; } catch ( IOException ex ) { throw new SshException ( ex , SshException . INTERNAL_ERROR ) ; } }
Implementation of the authentication method .