/*
- * Copyright 2000-2013 JetBrains s.r.o.
+ * Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
boolean usernameNeeded = arguments.getFirst();
String url = arguments.getSecond();
- int handler = Integer.parseInt(getNotNull(GitAskPassXmlRpcHandler.GIT_ASK_PASS_HANDLER_ENV));
+ String token = getNotNull(GitAskPassXmlRpcHandler.GIT_ASK_PASS_HANDLER_ENV);
int xmlRpcPort = Integer.parseInt(getNotNull(GitAskPassXmlRpcHandler.GIT_ASK_PASS_PORT_ENV));
GitAskPassXmlRpcClient xmlRpcClient = new GitAskPassXmlRpcClient(xmlRpcPort);
if (usernameNeeded) {
- String username = xmlRpcClient.askUsername(handler, url);
+ String username = xmlRpcClient.askUsername(token, url);
System.out.println(username);
}
else {
- String pass = xmlRpcClient.askPassword(handler, url);
+ String pass = xmlRpcClient.askPassword(token, url);
System.out.println(pass);
}
}
@NotNull
private static String getNotNull(@NotNull String env) {
- String handlerValue = System.getenv(env);
- if (handlerValue == null) {
+ String value = System.getenv(env);
+ if (value == null) {
throw new IllegalStateException(env + " environment variable is not defined!");
}
- return handlerValue;
+ return value;
}
@NotNull
/*
- * Copyright 2000-2013 JetBrains s.r.o.
+ * Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
// Obsolete collection usage because of the XmlRpcClientLite API
@SuppressWarnings({"UseOfObsoleteCollectionType", "unchecked"})
- String askUsername(int handler, @NotNull String url) {
+ String askUsername(String token, @NotNull String url) {
Vector parameters = new Vector();
- parameters.add(handler);
+ parameters.add(token);
parameters.add(url);
try {
// Obsolete collection usage because of the XmlRpcClientLite API
@SuppressWarnings({"UseOfObsoleteCollectionType", "unchecked"})
- String askPassword(int handler, @NotNull String url) {
+ String askPassword(String token, @NotNull String url) {
Vector parameters = new Vector();
- parameters.add(handler);
+ parameters.add(token);
parameters.add(url);
try {
/*
- * Copyright 2000-2013 JetBrains s.r.o.
+ * Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
/**
* Get the username from the user to access the given URL.
- * @param handler XML RPC handler number.
+ * @param token Access token.
* @param url URL which Git tries to access.
* @return The Username which should be used for the URL.
*/
// UnusedDeclaration suppressed: the method is used via XML RPC
@SuppressWarnings("UnusedDeclaration")
@NotNull
- String askUsername(int handler, @NotNull String url);
+ String askUsername(String token, @NotNull String url);
/**
* Get the password from the user to access the given URL.
* It is assumed that the username either is specified in the URL (http://username@host.com), or has been asked earlier.
- * @param handler XML RPC handler number.
+ * @param token Access token.
* @param url URL which Git tries to access.
* @return The password which should be used for the URL.
*/
// UnusedDeclaration suppressed: the method is used via XML RPC
@SuppressWarnings("UnusedDeclaration")
@NotNull
- String askPassword(int handler, @NotNull String url);
+ String askPassword(String token, @NotNull String url);
}
/*
- * Copyright 2000-2012 JetBrains s.r.o.
+ * Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*/
@NonNls String GIT_SSH_PREFIX = "git-ssh-";
/**
- * Name of environment variable for SSH handler number
+ * Name of environment variable for SSH handler access token
*/
@NonNls String SSH_HANDLER_ENV = "GIT4IDEA_SSH_HANDLER";
- /**
- * Name of environment variable for SSH handler number
- */
@NonNls String SSH_IGNORE_KNOWN_HOSTS_ENV = "GIT4IDEA_SSH_IGNORE_KNOWN_HOSTS";
- /**
- * Name of environment variable for SSH handler
- */
@NonNls String SSH_PORT_ENV = "GIT4IDEA_SSH_PORT";
/**
* Name of environment variable for SSH executable
/**
* Verify server host key
*
- * @param handler a handler identifier
+ * @param token Access token.
* @param hostName a host name
* @param port a port number
* @param serverHostKeyAlgorithm an algorithm
* @param isNew true if the key is a new, false if the key was changed
* @return true the host is verified, false otherwise
*/
- boolean verifyServerHostKey(int handler,
+ boolean verifyServerHostKey(String token,
String hostName,
int port,
String serverHostKeyAlgorithm,
/**
* Ask passphrase for the key
*
- * @param handler a handler identifier
+ * @param token Access token.
* @param userName a name of user
* @param keyPath a path for the key
* @param resetPassword a reset password if one was stored in password database
* @return the passphrase entered by the user
*/
@Nullable
- String askPassphrase(final int handler, final String userName, final String keyPath, boolean resetPassword, final String lastError);
+ String askPassphrase(String token, final String userName, final String keyPath, boolean resetPassword, final String lastError);
/**
* Reply to challenge for keyboard-interactive method. Also used for
*
- * @param handlerNo a handler identifier
+ * @param token Access token.
* @param userName a user name (includes host and port)
* @param name name of challenge
* @param instruction instruction
*/
@SuppressWarnings({"UseOfObsoleteCollectionType"})
@Nullable
- Vector<String> replyToChallenge(final int handlerNo,
+ Vector<String> replyToChallenge(String token,
final String userName,
final String name,
final String instruction,
/**
* Ask password for the specified user name
*
- * @param handlerNo a handler identifier
+ * @param token Access token.
* @param userName a name of user to ask password for
* @param resetPassword a reset password if one was stored in password database
* @param lastError a last error
* @return the password or null if authentication failed.
*/
@Nullable
- String askPassword(final int handlerNo, final String userName, boolean resetPassword, final String lastError);
+ String askPassword(String token, final String userName, boolean resetPassword, final String lastError);
/**
* Notify invoker about last successful authentication attempt.
*
- * @param handlerNo the handler
+ * @param token the handler
* @param userName the user name
* @param method the authentication method, the empty string means that authentication failed
* @param error the error shown in the case when authentication process failed
* @return The method doesn't return any sensible value, but it is needed here, since the Apache XML-RPC implementation which we use
* doesn't allow void methods: "IllegalArgumentException: void return types for handler methods not supported".
*/
- String setLastSuccessful(final int handlerNo, final String userName, final String method, final String error);
+ String setLastSuccessful(String token, final String userName, final String method, final String error);
/**
* Get last successful authentication method
*
- * @param handlerNo the handler no
+ * @param token Access token
* @param userName the user name
* @return the authentication method, the empty string means that last authentication failed
*/
- String getLastSuccessful(final int handlerNo, final String userName);
+ String getLastSuccessful(String token, final String userName);
}
/*
- * Copyright 2000-2012 JetBrains s.r.o.
+ * Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
- public boolean verifyServerHostKey(final int handler,
+ public boolean verifyServerHostKey(String token,
final String hostname,
final int port,
final String serverHostKeyAlgorithm,
return false;
}
Vector parameters = new Vector();
- parameters.add(handler);
+ parameters.add(token);
parameters.add(hostname);
parameters.add(port);
parameters.add(serverHostKeyAlgorithm);
*/
@Nullable
@SuppressWarnings("unchecked")
- public String askPassphrase(final int handler,
+ public String askPassphrase(String token,
final String username,
final String keyPath,
final boolean resetPassword,
return null;
}
Vector parameters = new Vector();
- parameters.add(handler);
+ parameters.add(token);
parameters.add(username);
parameters.add(keyPath);
parameters.add(resetPassword);
*/
@Nullable
@SuppressWarnings("unchecked")
- public Vector<String> replyToChallenge(final int handlerNo,
+ public Vector<String> replyToChallenge(String token,
final String username,
final String name,
final String instruction,
return null;
}
Vector parameters = new Vector();
- parameters.add(handlerNo);
+ parameters.add(token);
parameters.add(username);
parameters.add(name);
parameters.add(instruction);
*/
@Nullable
@SuppressWarnings("unchecked")
- public String askPassword(final int handlerNo, final String username, final boolean resetPassword, final String lastError) {
+ public String askPassword(String token, final String username, final boolean resetPassword, final String lastError) {
if (myClient == null) {
return null;
}
Vector parameters = new Vector();
- parameters.add(handlerNo);
+ parameters.add(token);
parameters.add(username);
parameters.add(resetPassword);
parameters.add(lastError);
@Override
@SuppressWarnings("unchecked")
- public String setLastSuccessful(int handlerNo, String userName, String method, String error) {
+ public String setLastSuccessful(String token, String userName, String method, String error) {
if (myClient == null) {
return "";
}
Vector parameters = new Vector();
- parameters.add(handlerNo);
+ parameters.add(token);
parameters.add(userName);
parameters.add(method);
parameters.add(error);
*/
@Override
@SuppressWarnings("unchecked")
- public String getLastSuccessful(int handlerNo, String userName) {
+ public String getLastSuccessful(String token, String userName) {
if (myClient == null) {
return "";
}
Vector parameters = new Vector();
- parameters.add(handlerNo);
+ parameters.add(token);
parameters.add(userName);
try {
return (String)myClient.execute(methodName("getLastSuccessful"), parameters);
}
catch (XmlRpcException e) {
- log("getLastSuccessful failed. handlerNo: " + handlerNo + ", userName: " + userName + ", client: " + myClient.getURL());
+ log("getLastSuccessful failed. token: " + token + ", userName: " + userName + ", client: " + myClient.getURL());
throw new RuntimeException("Invocation failed " + e.getMessage(), e);
}
catch (IOException e) {
- log("getLastSuccessful failed. handlerNo: " + handlerNo + ", userName: " + userName + ", client: " + myClient.getURL());
+ log("getLastSuccessful failed. token: " + token + ", userName: " + userName + ", client: " + myClient.getURL());
throw new RuntimeException("Invocation failed " + e.getMessage(), e);
}
}
/*
- * Copyright 2000-2012 JetBrains s.r.o.
+ * Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
/**
* Handler number
*/
- private final int myHandlerNo;
+ private final String myHandlerNo;
/**
* the xml RPC port
*/
private SSHMain(String host, String username, Integer port, String command) throws IOException {
SSHConfig config = SSHConfig.load();
myHost = config.lookup(username, host, port);
- myHandlerNo = Integer.parseInt(System.getenv(GitSSHHandler.SSH_HANDLER_ENV));
+ myHandlerNo = System.getenv(GitSSHHandler.SSH_HANDLER_ENV);
int xmlRpcPort = Integer.parseInt(System.getenv(GitSSHHandler.SSH_PORT_ENV));
myXmlRpcClient = new GitSSHXmlRpcClient(xmlRpcPort, myHost.isBatchMode());
myCommand = command;
/*
- * Copyright 2000-2014 JetBrains s.r.o.
+ * Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
private final File myWorkingDirectory;
private boolean myEnvironmentCleanedUp = true; // the flag indicating that environment has been cleaned up, by default is true because there is nothing to clean
- private int mySshHandler = -1;
- private int myHttpHandler = -1;
+ private UUID mySshHandler;
+ private UUID myHttpHandler;
private Processor<OutputStream> myInputProcessor; // The processor for stdin
// if true process might be cancelled
GitHttpAuthenticator httpAuthenticator = service.createAuthenticator(myProject, myCommand, ObjectUtils.assertNotNull(myUrls));
myHttpHandler = service.registerHandler(httpAuthenticator, myProject);
myEnvironmentCleanedUp = false;
- myEnv.put(GitAskPassXmlRpcHandler.GIT_ASK_PASS_HANDLER_ENV, Integer.toString(myHttpHandler));
+ myEnv.put(GitAskPassXmlRpcHandler.GIT_ASK_PASS_HANDLER_ENV, myHttpHandler.toString());
int port = service.getXmlRcpPort();
myEnv.put(GitAskPassXmlRpcHandler.GIT_ASK_PASS_PORT_ENV, Integer.toString(port));
LOG.debug(String.format("handler=%s, port=%s", myHttpHandler, port));
myEnv.put(GitSSHHandler.GIT_SSH_ENV, ssh.getScriptPath().getPath());
mySshHandler = ssh.registerHandler(new GitSSHGUIHandler(myProject), myProject);
myEnvironmentCleanedUp = false;
- myEnv.put(GitSSHHandler.SSH_HANDLER_ENV, Integer.toString(mySshHandler));
+ myEnv.put(GitSSHHandler.SSH_HANDLER_ENV, mySshHandler.toString());
int port = ssh.getXmlRcpPort();
myEnv.put(GitSSHHandler.SSH_PORT_ENV, Integer.toString(port));
LOG.debug(String.format("handler=%s, port=%s", mySshHandler, port));
if (myEnvironmentCleanedUp) {
return;
}
- if (mySshHandler >= 0) {
+ if (mySshHandler != null) {
ServiceManager.getService(GitXmlRpcSshService.class).unregisterHandler(mySshHandler);
}
- if (myHttpHandler >= 0) {
+ if (myHttpHandler != null) {
ServiceManager.getService(GitHttpAuthService.class).unregisterHandler(myHttpHandler);
}
myEnvironmentCleanedUp = true;
/*
- * Copyright 2000-2013 JetBrains s.r.o.
+ * Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import org.jetbrains.git4idea.util.ScriptGenerator;
import java.util.Collection;
+import java.util.UUID;
/**
* Provides the authentication mechanism for Git HTTP connections.
public class InternalRequestHandlerDelegate implements GitAskPassXmlRpcHandler {
@NotNull
@Override
- public String askUsername(int handler, @NotNull String url) {
- return getHandler(handler).askUsername(url);
+ public String askUsername(String token, @NotNull String url) {
+ return getHandler(UUID.fromString(token)).askUsername(url);
}
@NotNull
@Override
- public String askPassword(int handler, @NotNull String url) {
- return getHandler(handler).askPassword(url);
+ public String askPassword(String token, @NotNull String url) {
+ return getHandler(UUID.fromString(token)).askPassword(url);
}
}
/*
- * Copyright 2000-2009 JetBrains s.r.o.
+ * Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import org.jetbrains.git4idea.util.ScriptGenerator;
import org.jetbrains.ide.BuiltInServerManager;
+import java.security.SecureRandom;
import java.util.Map;
import java.util.Random;
/**
* Random number generator
*/
- private static final Random oursRandom = new Random();
+ private static final Random oursRandom = new SecureRandom();
/**
* The prefix for rebase editors
*/
/*
- * Copyright 2000-2013 JetBrains s.r.o.
+ * Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import java.io.File;
import java.io.IOException;
+import java.util.UUID;
/**
* <p>The provider of external application scripts called by Git when a remote operation needs communication with the user.</p>
@Nullable private File myScriptPath;
@NotNull private final Object SCRIPT_FILE_LOCK = new Object();
- @NotNull private final THashMap<Integer, T> handlers = new THashMap<Integer, T>();
- private int myNextHandlerKey;
+ @NotNull private final THashMap<UUID, T> handlers = new THashMap<UUID, T>();
@NotNull private final Object HANDLERS_LOCK = new Object();
/**
* @param parentDisposable a disposable to unregister the handler if it doesn't get unregistered manually
* @return an identifier to pass to the environment variable
*/
- public int registerHandler(@NotNull T handler, @NotNull Disposable parentDisposable) {
+ public UUID registerHandler(@NotNull T handler, @NotNull Disposable parentDisposable) {
synchronized (HANDLERS_LOCK) {
XmlRpcServer xmlRpcServer = XmlRpcServer.SERVICE.getInstance();
if (!xmlRpcServer.hasHandler(myHandlerName)) {
xmlRpcServer.addHandler(myHandlerName, createRpcRequestHandlerDelegate());
}
- final int key = myNextHandlerKey;
+ final UUID key = UUID.randomUUID();
handlers.put(key, handler);
Disposer.register(parentDisposable, new Disposable() {
@Override
handlers.remove(key);
}
});
- myNextHandlerKey++;
return key;
}
}
* @return the registered handler
*/
@NotNull
- protected T getHandler(int key) {
+ protected T getHandler(UUID key) {
synchronized (HANDLERS_LOCK) {
T rc = handlers.get(key);
if (rc == null) {
*
* @param key the key to unregister
*/
- public void unregisterHandler(int key) {
+ public void unregisterHandler(UUID key) {
synchronized (HANDLERS_LOCK) {
if (handlers.remove(key) == null) {
throw new IllegalArgumentException("The handler " + key + " is not registered");
/*
- * Copyright 2000-2013 JetBrains s.r.o.
+ * Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import org.jetbrains.annotations.NotNull;
import org.jetbrains.git4idea.util.ScriptGenerator;
+import java.util.UUID;
import java.util.Vector;
/**
public class InternalRequestHandler implements GitSSHHandler {
@Override
- public boolean verifyServerHostKey(int handler, String hostname, int port, String serverHostKeyAlgorithm, String serverHostKey,
+ public boolean verifyServerHostKey(String handler, String hostname, int port, String serverHostKeyAlgorithm, String serverHostKey,
boolean isNew) {
- return getHandler(handler).verifyServerHostKey(hostname, port, serverHostKeyAlgorithm, serverHostKey, isNew);
+ return getHandler(UUID.fromString(handler)).verifyServerHostKey(hostname, port, serverHostKeyAlgorithm, serverHostKey, isNew);
}
@Override
- public String askPassphrase(int handler, String username, String keyPath, boolean resetPassword, String lastError) {
- return adjustNull(getHandler(handler).askPassphrase(username, keyPath, resetPassword, lastError));
+ public String askPassphrase(String handler, String username, String keyPath, boolean resetPassword, String lastError) {
+ return adjustNull(getHandler(UUID.fromString(handler)).askPassphrase(username, keyPath, resetPassword, lastError));
}
@Override
@SuppressWarnings({"UseOfObsoleteCollectionType"})
- public Vector<String> replyToChallenge(int handlerNo, String username, String name, String instruction, int numPrompts,
+ public Vector<String> replyToChallenge(String token, String username, String name, String instruction, int numPrompts,
Vector<String> prompt, Vector<Boolean> echo, String lastError) {
- return adjustNull(getHandler(handlerNo).replyToChallenge(username, name, instruction, numPrompts, prompt, echo, lastError));
+ return adjustNull(getHandler(UUID.fromString(token)).replyToChallenge(username, name, instruction, numPrompts, prompt, echo, lastError));
}
@Override
- public String askPassword(int handlerNo, String username, boolean resetPassword, String lastError) {
- return adjustNull(getHandler(handlerNo).askPassword(username, resetPassword, lastError));
+ public String askPassword(String token, String username, boolean resetPassword, String lastError) {
+ return adjustNull(getHandler(UUID.fromString(token)).askPassword(username, resetPassword, lastError));
}
@Override
- public String setLastSuccessful(int handlerNo, String userName, String method, String error) {
- getHandler(handlerNo).setLastSuccessful(userName, method, error);
+ public String setLastSuccessful(String token, String userName, String method, String error) {
+ getHandler(UUID.fromString(token)).setLastSuccessful(userName, method, error);
return "";
}
@Override
- public String getLastSuccessful(int handlerNo, String userName) {
- return getHandler(handlerNo).getLastSuccessful(userName);
+ public String getLastSuccessful(String token, String userName) {
+ return getHandler(UUID.fromString(token)).getLastSuccessful(userName);
}
/**