private void authenticate() {

    try {
      if (connection.authenticateWithNone(profile.getUser())) {
        Log.d(TAG, "Authenticate with none");
        return;
      }
    } catch (Exception e) {
      Log.d(TAG, "Host does not support 'none' authentication.");
    }

    try {
      if (connection.isAuthMethodAvailable(profile.getUser(), AUTH_PUBLICKEY)) {
        File f = new File(profile.getKeyPath());
        if (f.exists()) if (profile.getPassword().equals("")) profile.setPassword(null);
        if (connection.authenticateWithPublicKey(profile.getUser(), f, profile.getPassword())) {
          Log.d(TAG, "Authenticate with public key");
          return;
        }
      }
    } catch (Exception e) {
      Log.d(TAG, "Host does not support 'Public key' authentication.");
    }

    try {
      if (connection.isAuthMethodAvailable(profile.getUser(), AUTH_PASSWORD)) {
        if (connection.authenticateWithPassword(profile.getUser(), profile.getPassword())) {
          Log.d(TAG, "Authenticate with password");
          return;
        }
      }
    } catch (IllegalStateException e) {
      Log.e(TAG, "Connection went away while we were trying to authenticate", e);
    } catch (Exception e) {
      Log.e(TAG, "Problem during handleAuthentication()", e);
    }

    // TODO: Need verification

    try {
      if (connection.isAuthMethodAvailable(profile.getUser(), AUTH_KEYBOARDINTERACTIVE)) {
        if (connection.authenticateWithKeyboardInteractive(profile.getUser(), this)) return;
      }
    } catch (Exception e) {
      Log.d(TAG, "Host does not support 'Keyboard-Interactive' authentication.");
    }
  }
Example #2
0
 /**
  * Authenticate using some supported methods. If authentication fails, the method throws {@link
  * IOException}.
  *
  * @param c the connection to use for authentication
  * @throws IOException in case of IO error or authentication failure
  */
 private void authenticate(final Connection c) throws IOException {
   for (String method : myHost.getPreferredMethods()) {
     if (c.isAuthenticationComplete()) {
       return;
     }
     if (PUBLIC_KEY_METHOD.equals(method)) {
       if (!myHost.supportsPubkeyAuthentication()) {
         continue;
       }
       if (!c.isAuthMethodAvailable(myHost.getUser(), PUBLIC_KEY_METHOD)) {
         continue;
       }
       File key = myHost.getIdentityFile();
       if (key == null) {
         for (String a : myHost.getHostKeyAlgorithms()) {
           if (SSH_RSA_ALGORITHM.equals(a)) {
             if (tryPublicKey(c, idRSAPath)) {
               return;
             }
           } else if (SSH_DSS_ALGORITHM.equals(a)) {
             if (tryPublicKey(c, idDSAPath)) {
               return;
             }
           }
         }
       } else {
         if (tryPublicKey(c, key.getPath())) {
           return;
         }
       }
     } else if (KEYBOARD_INTERACTIVE_METHOD.equals(method)) {
       if (!c.isAuthMethodAvailable(myHost.getUser(), KEYBOARD_INTERACTIVE_METHOD)) {
         continue;
       }
       InteractiveSupport interactiveSupport = new InteractiveSupport();
       for (int i = myHost.getNumberOfPasswordPrompts(); i > 0; i--) {
         if (c.isAuthenticationComplete()) {
           return;
         }
         if (c.authenticateWithKeyboardInteractive(myHost.getUser(), interactiveSupport)) {
           myLastError = "";
           return;
         } else {
           myLastError = GitBundle.getString("sshmain.keyboard.interactive.failed");
         }
         if (interactiveSupport.myPromptCount == 0 || interactiveSupport.myCancelled) {
           // the interactive callback has never been asked or it was cancelled, exit the loop
           myLastError = "";
           break;
         }
       }
     } else if (PASSWORD_METHOD.equals(method)) {
       if (!myHost.supportsPasswordAuthentication()) {
         continue;
       }
       if (!c.isAuthMethodAvailable(myHost.getUser(), PASSWORD_METHOD)) {
         continue;
       }
       for (int i = myHost.getNumberOfPasswordPrompts(); i > 0; i--) {
         String password =
             myXmlRpcClient.askPassword(myHandlerNo, getUserHostString(), myLastError);
         if (password == null) {
           break;
         } else {
           if (c.authenticateWithPassword(myHost.getUser(), password)) {
             myLastError = "";
             return;
           } else {
             myLastError = GitBundle.getString("sshmain.password.failed");
           }
         }
       }
     }
   }
   throw new IOException("Authentication failed");
 }