From de1212d1535d3192a4f9df8bc2be2d80153b5c19 Mon Sep 17 00:00:00 2001 From: Nadya Zabrodina Date: Tue, 27 Nov 2018 16:10:29 +0300 Subject: [PATCH] git: implement NONE authentication mode for ssh --- .../GitHandlerAuthenticationManager.java | 9 +++----- .../GitNativeSshGuiAuthenticator.java | 15 +++++++----- .../git4idea/commands/GitSSHGUIHandler.java | 23 +++++++++++-------- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/plugins/git4idea/src/git4idea/commands/GitHandlerAuthenticationManager.java b/plugins/git4idea/src/git4idea/commands/GitHandlerAuthenticationManager.java index 547d280af765..23d6c7126a57 100644 --- a/plugins/git4idea/src/git4idea/commands/GitHandlerAuthenticationManager.java +++ b/plugins/git4idea/src/git4idea/commands/GitHandlerAuthenticationManager.java @@ -27,7 +27,6 @@ import java.util.Collection; import java.util.UUID; import static com.intellij.util.ObjectUtils.notNull; -import static git4idea.commands.GitAuthenticationMode.FULL; /** * Manager for Git remotes authentication. @@ -134,7 +133,7 @@ public class GitHandlerAuthenticationManager implements AutoCloseable { myHandler.addCustomEnvironmentVariable(GitSSHHandler.GIT_SSH_ENV, ssh.getScriptPath().getPath()); myHandler.addCustomEnvironmentVariable(GitSSHHandler.GIT_SSH_VAR, "ssh"); GitAuthenticationGate authenticationGate = notNull(myHandler.getAuthenticationGate(), GitPassthroughAuthenticationGate.getInstance()); - GitSSHGUIHandler guiHandler = new GitSSHGUIHandler(myProject, authenticationGate, myHandler.getIgnoreAuthenticationMode() != FULL); + GitSSHGUIHandler guiHandler = new GitSSHGUIHandler(myProject, authenticationGate, myHandler.getIgnoreAuthenticationMode()); mySshHandler = ssh.registerHandler(guiHandler, myProject); myHandler.addCustomEnvironmentVariable(GitSSHHandler.SSH_HANDLER_ENV, mySshHandler.toString()); int port = ssh.getXmlRcpPort(); @@ -172,10 +171,8 @@ public class GitHandlerAuthenticationManager implements AutoCloseable { boolean doNotRememberPasswords = myHandler.getUrls().size() > 1; GitAuthenticationGate authenticationGate = notNull(myHandler.getAuthenticationGate(), GitPassthroughAuthenticationGate.getInstance()); - GitNativeSshGuiAuthenticator authenticator = new GitNativeSshGuiAuthenticator(myProject, - authenticationGate, - myHandler.getIgnoreAuthenticationMode() != FULL, - doNotRememberPasswords); + GitNativeSshGuiAuthenticator authenticator = + new GitNativeSshGuiAuthenticator(myProject, authenticationGate, myHandler.getIgnoreAuthenticationMode(), doNotRememberPasswords); myNativeSshHandler = service.registerHandler(authenticator, myProject); int port = service.getXmlRcpPort(); diff --git a/plugins/git4idea/src/git4idea/commands/GitNativeSshGuiAuthenticator.java b/plugins/git4idea/src/git4idea/commands/GitNativeSshGuiAuthenticator.java index f14350570b25..424ab71eb8bf 100644 --- a/plugins/git4idea/src/git4idea/commands/GitNativeSshGuiAuthenticator.java +++ b/plugins/git4idea/src/git4idea/commands/GitNativeSshGuiAuthenticator.java @@ -16,10 +16,12 @@ import org.jetbrains.annotations.Nullable; import java.util.regex.Matcher; +import static git4idea.commands.GitAuthenticationMode.FULL; + class GitNativeSshGuiAuthenticator implements GitNativeSshAuthenticator { @NotNull private final Project myProject; @NotNull private final GitAuthenticationGate myAuthenticationGate; - private final boolean myIgnoreAuthenticationRequest; + @NotNull private final GitAuthenticationMode myAuthenticationMode; private final boolean myDoNotRememberPasswords; @Nullable private String myLastAskedKeyPath = null; @@ -27,17 +29,18 @@ class GitNativeSshGuiAuthenticator implements GitNativeSshAuthenticator { GitNativeSshGuiAuthenticator(@NotNull Project project, @NotNull GitAuthenticationGate authenticationGate, - boolean isIgnoreAuthenticationRequest, + @NotNull GitAuthenticationMode authenticationMode, boolean doNotRememberPasswords) { myProject = project; myAuthenticationGate = authenticationGate; - myIgnoreAuthenticationRequest = isIgnoreAuthenticationRequest; + myAuthenticationMode = authenticationMode; myDoNotRememberPasswords = doNotRememberPasswords; } @Nullable @Override public String handleInput(@NotNull String description) { + if(myAuthenticationMode == GitAuthenticationMode.NONE) return null; return myAuthenticationGate.waitAndCompute(() -> { if (isKeyPassphrase(description)) return askKeyPassphraseInput(description); if (isSshPassword(description)) return askSshPasswordInput(description); @@ -68,7 +71,7 @@ class GitNativeSshGuiAuthenticator implements GitNativeSshAuthenticator { }); } else { - return GitSSHGUIHandler.askPassphrase(myProject, keyPath, resetPassword, myIgnoreAuthenticationRequest, null); + return GitSSHGUIHandler.askPassphrase(myProject, keyPath, resetPassword, myAuthenticationMode, null); } } @@ -92,7 +95,7 @@ class GitNativeSshGuiAuthenticator implements GitNativeSshAuthenticator { }); } else { - return GitSSHGUIHandler.askPassword(myProject, username, resetPassword, myIgnoreAuthenticationRequest, null); + return GitSSHGUIHandler.askPassword(myProject, username, resetPassword, myAuthenticationMode, null); } } @@ -129,7 +132,7 @@ class GitNativeSshGuiAuthenticator implements GitNativeSshAuthenticator { @Nullable private String askUser(@NotNull Computable query) { - if (myIgnoreAuthenticationRequest) return null; + if (myAuthenticationMode != FULL) return null; Ref answerRef = new Ref<>(); ApplicationManager.getApplication().invokeAndWait(() -> { diff --git a/plugins/git4idea/src/git4idea/commands/GitSSHGUIHandler.java b/plugins/git4idea/src/git4idea/commands/GitSSHGUIHandler.java index 287e06f7288d..85d527c22db9 100644 --- a/plugins/git4idea/src/git4idea/commands/GitSSHGUIHandler.java +++ b/plugins/git4idea/src/git4idea/commands/GitSSHGUIHandler.java @@ -39,20 +39,21 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; import static com.intellij.credentialStore.CredentialAttributesKt.*; +import static git4idea.commands.GitAuthenticationMode.FULL; /** * Swing GUI handler for the SSH events */ public class GitSSHGUIHandler { @Nullable private final Project myProject; - private final boolean myIgnoreAuthenticationRequest; + @NotNull private final GitAuthenticationMode myAuthenticationMode; @NotNull private final GitAuthenticationGate myAuthenticationGate; GitSSHGUIHandler(@Nullable Project project, @NotNull GitAuthenticationGate authenticationGate, - boolean ignoreAuthenticationRequest) { + @NotNull GitAuthenticationMode authenticationMode) { myProject = project; - myIgnoreAuthenticationRequest = ignoreAuthenticationRequest; + myAuthenticationMode = authenticationMode; myAuthenticationGate = authenticationGate; } @@ -81,7 +82,7 @@ public class GitSSHGUIHandler { public String askPassphrase(final String username, final String keyPath, boolean resetPassword, final String lastError) { return myAuthenticationGate.waitAndCompute(() -> { String error = processLastError(resetPassword, lastError); - return askPassphrase(myProject, keyPath, resetPassword, myIgnoreAuthenticationRequest, error); + return askPassphrase(myProject, keyPath, resetPassword, myAuthenticationMode, error); }); } @@ -89,8 +90,9 @@ public class GitSSHGUIHandler { static String askPassphrase(@Nullable Project project, @NotNull String keyPath, boolean resetPassword, - boolean ignoreAuthenticationRequest, + @NotNull GitAuthenticationMode authenticationMode, @Nullable String lastError) { + if (authenticationMode == GitAuthenticationMode.NONE) return null; CredentialAttributes oldAttributes = oldCredentialAttributes("PASSPHRASE:" + keyPath); CredentialAttributes newAttributes = passphraseCredentialAttributes(keyPath); Credentials credentials = getAndMigrateCredentials(oldAttributes, newAttributes); @@ -98,7 +100,7 @@ public class GitSSHGUIHandler { String password = credentials.getPasswordAsString(); if (password != null && !password.isEmpty()) return password; } - if (ignoreAuthenticationRequest) return null; + if (authenticationMode == GitAuthenticationMode.SILENT) return null; return CredentialPromptDialog.askPassword(project, GitBundle.getString("ssh.ask.passphrase.title"), GitBundle.message("ssh.ask.passphrase.message", PathUtil.getFileName(keyPath)), newAttributes, true, lastError); @@ -165,7 +167,7 @@ public class GitSSHGUIHandler { final Vector prompt, final Vector echo, final String lastError) { - if (myIgnoreAuthenticationRequest) return null; + if (myAuthenticationMode != FULL) return null; return myAuthenticationGate.waitAndCompute(() -> { final AtomicReference> rc = new AtomicReference<>(); ApplicationManager.getApplication().invokeAndWait(() -> { @@ -191,7 +193,7 @@ public class GitSSHGUIHandler { public String askPassword(final String username, boolean resetPassword, final String lastError) { return myAuthenticationGate.waitAndCompute(() -> { String error = processLastError(resetPassword, lastError); - return askPassword(myProject, username, resetPassword, myIgnoreAuthenticationRequest, error); + return askPassword(myProject, username, resetPassword, myAuthenticationMode, error); }); } @@ -199,8 +201,9 @@ public class GitSSHGUIHandler { static String askPassword(@Nullable Project project, @NotNull String username, boolean resetPassword, - boolean ignoreAuthenticationRequest, + @NotNull GitAuthenticationMode authenticationMode, @Nullable String error) { + if(authenticationMode == GitAuthenticationMode.NONE) return null; CredentialAttributes oldAttributes = oldCredentialAttributes("PASSWORD:" + username); CredentialAttributes newAttributes = passwordCredentialAttributes(username); Credentials credentials = getAndMigrateCredentials(oldAttributes, newAttributes); @@ -208,7 +211,7 @@ public class GitSSHGUIHandler { String password = credentials.getPasswordAsString(); if (password != null) return password; } - if (ignoreAuthenticationRequest) return null; + if (authenticationMode == GitAuthenticationMode.SILENT) return null; return CredentialPromptDialog.askPassword(project, GitBundle.getString("ssh.password.title"), GitBundle.message("ssh.password.message", username),