use of com.oxygenxml.git.options.UserAndPasswordCredentials in project oxygen-git-client-addon by oxygenxml.
the class AuthUtil method getCredentialsProvider.
/**
* Get the credentials provider for the given host.
*
* @param host The host.
*
* @return The credentials provider.
*/
public static SSHCapableUserCredentialsProvider getCredentialsProvider(String host) {
CredentialsBase credentials = OptionsManager.getInstance().getGitCredentials(host);
CredentialsType credentialsType = credentials.getType();
return new SSHCapableUserCredentialsProvider(credentialsType == CredentialsType.USER_AND_PASSWORD ? ((UserAndPasswordCredentials) credentials).getUsername() : ((PersonalAccessTokenInfo) credentials).getTokenValue(), credentialsType == CredentialsType.USER_AND_PASSWORD ? ((UserAndPasswordCredentials) credentials).getPassword() : ((PersonalAccessTokenInfo) credentials).getTokenValue(), OptionsManager.getInstance().getSshPassphrase(), credentials.getHost());
}
use of com.oxygenxml.git.options.UserAndPasswordCredentials 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;
}
use of com.oxygenxml.git.options.UserAndPasswordCredentials in project oxygen-git-client-addon by oxygenxml.
the class LoginDialog method doOK.
@Override
protected void doOK() {
if (basicAuthRadio.isSelected()) {
String username = tfUsername.getText().trim();
String password = new String(pfPassword.getPassword());
credentials = new UserAndPasswordCredentials(username, password, host);
} else {
String tokenValue = new String(tokenPassField.getPassword());
credentials = new PersonalAccessTokenInfo(host, tokenValue);
}
OptionsManager.getInstance().saveGitCredentials(credentials);
super.doOK();
}
Aggregations