Skip to content

Commit

Permalink
Mastodon Git: clicking cancel on username, password dialog is handled…
Browse files Browse the repository at this point in the history
… better
  • Loading branch information
maarzt committed Nov 10, 2023
1 parent 1bec310 commit 97c225e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -384,6 +385,10 @@ private void run( String title, RunnableWithException action )
{
action.run();
}
catch ( CancellationException e )
{
// ignore
}
catch ( Exception e )
{
ErrorDialog.showErrorMessage( title, e );
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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.
* <p>
* 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
{
Expand All @@ -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 );
Expand All @@ -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()
Expand Down

0 comments on commit 97c225e

Please sign in to comment.