Search in sources :

Example 1 with PassphraseDialog

use of com.oxygenxml.git.view.dialog.PassphraseDialog in project oxygen-git-client-addon by oxygenxml.

the class AuthUtil method handleAuthException.

/**
 * Handle authentication exception.
 *
 * @param ex                The exception to handle.
 * @param hostName          The host name.
 * @param excMessPresenter  Exception message presenter.
 * @param retryLoginHere    <code>true</code> to retry login here, in this method.
 *
 * @return <code>true</code> if the authentication should be tried again.
 */
public static boolean handleAuthException(GitAPIException ex, String hostName, AuthExceptionMessagePresenter excMessPresenter, boolean retryLoginHere) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Handle Auth Exception: ");
        LOGGER.debug(ex.getMessage(), ex);
    }
    Throwable cause = ex;
    while (cause.getCause() != null) {
        cause = cause.getCause();
    }
    CredentialsBase userCredentials = OptionsManager.getInstance().getGitCredentials(hostName);
    boolean tryAgainOutside = false;
    String lowercaseMsg = ex.getMessage().toLowerCase();
    if (lowercaseMsg.contains(NOT_AUTHORIZED) || lowercaseMsg.contains(AUTHENTICATION_NOT_SUPPORTED)) {
        // Authorization problems.
        String loginMessage = TRANSLATOR.getTranslation(Tags.AUTHENTICATION_FAILED) + " ";
        if (userCredentials.getType() == CredentialsType.USER_AND_PASSWORD) {
            String username = ((UserAndPasswordCredentials) userCredentials).getUsername();
            loginMessage += username == null ? TRANSLATOR.getTranslation(Tags.NO_CREDENTIALS_FOUND) : TRANSLATOR.getTranslation(Tags.CHECK_CREDENTIALS);
        } else if (userCredentials.getType() == CredentialsType.PERSONAL_ACCESS_TOKEN) {
            loginMessage += TRANSLATOR.getTranslation(Tags.CHECK_TOKEN_VALUE_AND_PERMISSIONS);
        }
        tryAgainOutside = shouldTryAgainOutside(hostName, retryLoginHere, loginMessage);
    } else if (lowercaseMsg.contains(NOT_PERMITTED)) {
        // The user doesn't have permissions.
        PluginWorkspaceProvider.getPluginWorkspace().showErrorMessage(TRANSLATOR.getTranslation(Tags.NO_RIGHTS_TO_PUSH_MESSAGE));
        String loginMessage = TRANSLATOR.getTranslation(Tags.LOGIN_DIALOG_CREDENTIALS_DOESNT_HAVE_RIGHTS);
        if (userCredentials.getType() == CredentialsType.USER_AND_PASSWORD) {
            loginMessage += ", " + ((UserAndPasswordCredentials) userCredentials).getUsername();
        }
        loginMessage += ".";
        tryAgainOutside = shouldTryAgainOutside(hostName, retryLoginHere, loginMessage);
    } else if (lowercaseMsg.contains(ORIGIN_NOT_FOUND) || lowercaseMsg.contains(NO_VALUE_FOR_ORIGIN_URL_IN_CONFIG)) {
        // No remote linked with the local.
        tryAgainOutside = new AddRemoteDialog().linkRemote();
    } else if (lowercaseMsg.contains(AUTH_FAIL) || (cause instanceof SshException) && ((SshException) cause).getDisconnectCode() == SshConstants.SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE) {
        // This message is thrown for SSH.
        String passPhraseMessage = TRANSLATOR.getTranslation(Tags.ENTER_SSH_PASS_PHRASE);
        String passphrase = new PassphraseDialog(passPhraseMessage).getPassphrase();
        tryAgainOutside = passphrase != null;
    } else if (ex.getCause() instanceof NoRemoteRepositoryException || lowercaseMsg.contains("invalid advertisement of")) {
        if (excMessPresenter != null) {
            if (userCredentials.getType() == CredentialsType.USER_AND_PASSWORD) {
                excMessPresenter.presentMessage(TRANSLATOR.getTranslation(Tags.CLONE_REPOSITORY_DIALOG_URL_IS_NOT_A_REPOSITORY));
            } else if (userCredentials.getType() == CredentialsType.PERSONAL_ACCESS_TOKEN) {
                excMessPresenter.presentMessage(TRANSLATOR.getTranslation(Tags.CANNOT_REACH_HOST) + ". " + TRANSLATOR.getTranslation(Tags.CHECK_TOKEN_VALUE_AND_PERMISSIONS));
            }
        } else {
            LOGGER.error(ex.getMessage(), ex);
        }
    } else {
        // "Unhandled" exception
        if (excMessPresenter != null) {
            excMessPresenter.presentMessage(ex instanceof RefNotAdvertisedException ? TRANSLATOR.getTranslation(Tags.NO_REMOTE_EXCEPTION_MESSAGE) : ex.getClass().getName() + ": " + ex.getMessage());
        } else {
            LOGGER.error(ex.getMessage(), ex);
        }
    }
    return tryAgainOutside;
}
Also used : RefNotAdvertisedException(org.eclipse.jgit.api.errors.RefNotAdvertisedException) CredentialsBase(com.oxygenxml.git.options.CredentialsBase) PassphraseDialog(com.oxygenxml.git.view.dialog.PassphraseDialog) AddRemoteDialog(com.oxygenxml.git.view.dialog.AddRemoteDialog) NoRemoteRepositoryException(org.eclipse.jgit.errors.NoRemoteRepositoryException) SshException(org.apache.sshd.common.SshException) UserAndPasswordCredentials(com.oxygenxml.git.options.UserAndPasswordCredentials)

Aggregations

CredentialsBase (com.oxygenxml.git.options.CredentialsBase)1 UserAndPasswordCredentials (com.oxygenxml.git.options.UserAndPasswordCredentials)1 AddRemoteDialog (com.oxygenxml.git.view.dialog.AddRemoteDialog)1 PassphraseDialog (com.oxygenxml.git.view.dialog.PassphraseDialog)1 SshException (org.apache.sshd.common.SshException)1 RefNotAdvertisedException (org.eclipse.jgit.api.errors.RefNotAdvertisedException)1 NoRemoteRepositoryException (org.eclipse.jgit.errors.NoRemoteRepositoryException)1