From 97c225e52667a9f43ff76bf6931e8f1d77c76748 Mon Sep 17 00:00:00 2001 From: Matthias Arzt Date: Fri, 10 Nov 2023 12:14:53 +0100 Subject: [PATCH] Mastodon Git: clicking cancel on username, password dialog is handled better --- .../collaboration/MastodonGitController.java | 5 +++++ .../credentials/PersistentCredentials.java | 18 +++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/mastodon/mamut/tomancak/collaboration/MastodonGitController.java b/src/main/java/org/mastodon/mamut/tomancak/collaboration/MastodonGitController.java index ad549ab..1fa082f 100644 --- a/src/main/java/org/mastodon/mamut/tomancak/collaboration/MastodonGitController.java +++ b/src/main/java/org/mastodon/mamut/tomancak/collaboration/MastodonGitController.java @@ -31,6 +31,7 @@ import java.io.File; import java.util.Arrays; import java.util.List; +import java.util.concurrent.CancellationException; import javax.swing.JOptionPane; import javax.swing.SwingUtilities; @@ -384,6 +385,10 @@ private void run( String title, RunnableWithException action ) { action.run(); } + catch ( CancellationException e ) + { + // ignore + } catch ( Exception e ) { ErrorDialog.showErrorMessage( title, e ); diff --git a/src/main/java/org/mastodon/mamut/tomancak/collaboration/credentials/PersistentCredentials.java b/src/main/java/org/mastodon/mamut/tomancak/collaboration/credentials/PersistentCredentials.java index 6a68461..33e1409 100644 --- a/src/main/java/org/mastodon/mamut/tomancak/collaboration/credentials/PersistentCredentials.java +++ b/src/main/java/org/mastodon/mamut/tomancak/collaboration/credentials/PersistentCredentials.java @@ -1,5 +1,7 @@ package org.mastodon.mamut.tomancak.collaboration.credentials; +import java.util.concurrent.CancellationException; + import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; @@ -9,6 +11,7 @@ import net.miginfocom.swing.MigLayout; import org.apache.commons.lang3.tuple.Pair; +import org.eclipse.jgit.transport.CredentialItem; import org.eclipse.jgit.transport.CredentialsProvider; import org.eclipse.jgit.transport.URIish; @@ -18,6 +21,13 @@ * The credentials are stored in memory and reused for all subsequent requests. * It also tries to detect if the user entered wrong credentials, * and asks for new credentials. + *

+ * WARNING: JGIT expects a {@link CredentialsProvider} to return false if the + * user cancels for example a username/password dialog. (see {@link CredentialsProvider#get}) + * This class behaves differently: It throws a {@link CancellationException} + * instead. This difference is on purpose. The CancellationException + * better describes the situation and is easier to handle, than the + * {@link org.eclipse.jgit.errors.TransportException} that JGIT throws. */ public class PersistentCredentials { @@ -35,12 +45,11 @@ private synchronized Pair< String, String > getUsernameAndPassword( URIish uri, { boolean missingCredentials = password == null || username == null; if ( missingCredentials || authenticationFailure ) - if ( !queryPassword( uri.toString(), authenticationFailure ) ) - return null; + queryPassword( uri.toString(), authenticationFailure ); return Pair.of( username, password ); } - private boolean queryPassword( String url, boolean previousAuthenticationFailed ) + private void queryPassword( String url, boolean previousAuthenticationFailed ) { JTextField usernameField = new JTextField( 20 ); JPasswordField passwordField = new JPasswordField( 20 ); @@ -59,11 +68,10 @@ private boolean queryPassword( String url, boolean previousAuthenticationFailed panel, "Authentication for Git Repository", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE ); if ( !ok ) - return false; + throw new CancellationException( "User cancelled username & password dialog." ); username = usernameField.getText(); password = new String( passwordField.getPassword() ); - return true; } public CredentialsProvider getSingleUseCredentialsProvider()