Processing the authentication result

Lee Painter

When you authenticate with the SshClient it returns an integer value to indicate the result. 

int result = ssh.authenticate(new PasswordAuthentication("xxxxxx"));

The result is any number of values defined as constants on the SshAuthentication interface.

These are:

COMPLETE - The user has been authenticated and you can proceed to perform secure operations over the SSH connection

FAILED - The authentication attempt failed, try again with different credentials.

FURTHER_AUTHENTICATION_REQUIRED - The authentication attempt succeeded, but the server requires that the user also authenticate using another mechanism. For example, a server may require password and publickey authentication be completed. Normally you can examine the return array of Ssh2Client's getAuthenticationMethods to see what authentication methods can continue.

CANCELLED - It's possible for some authentication types, like keyboard-interactive to be canceled by the user during authentication. 

PUBLIC_KEY_ACCEPTABLE - In order to avoid using up authentication attempts, when using public key authentication you can set the authenticating flag on the PublicKeyAuthentication object to false, to see if the key is acceptable for the user. If you set this flag, and the user can authenticate with the key then this reason will be returned. You should then repeat the public key authentication attempt setting the authenticating flag to true. The following example checks a number of different files and authenticates with it if it's acceptable to the server:

String[] paths = new String[] { ".ssh/id_rsa", ".ssh/id_dsa", ".ssh/id_ecdsa" };
for(String path : paths) {
   PublicKeyAuthentication pk = new PublicKeyAuthentication(new File(path), null);
   pk.setAuthenticating(false);
   int result = ssh.authenticate(pk);
   if(result ==PUBLIC_KEY_ACCEPTABLE) {
      pk.setAuthenticating(true);
      result = ssh.authenticate(pk);
      if(result == COMPLETE) {
         break;
      }
   }
}