auth: Pass SSH auth to Git command through GIT_SSH_COMMAND

This commit is contained in:
Hylke Bons 2016-03-26 18:05:12 +00:00
parent 47d821ad2a
commit b76b47f64e
2 changed files with 50 additions and 26 deletions

View file

@ -13,6 +13,7 @@
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
namespace SparkleLib.Git {
@ -32,27 +33,32 @@ namespace SparkleLib.Git {
StartInfo.FileName = GitPath;
StartInfo.WorkingDirectory = path;
if (StartInfo.EnvironmentVariables.ContainsKey ("LANG"))
StartInfo.EnvironmentVariables ["LANG"] = "en_US";
else
StartInfo.EnvironmentVariables.Add ("LANG", "en_US");
if (string.IsNullOrEmpty (SSHPath))
SSHPath = "ssh";
if (StartInfo.EnvironmentVariables.ContainsKey ("GIT_TERMINAL_PROMPT"))
StartInfo.EnvironmentVariables ["GIT_TERMINAL_PROMPT"] = "0";
else
StartInfo.EnvironmentVariables.Add ("GIT_TERMINAL_PROMPT", "0");
string GIT_SSH_COMMAND = SSHPath + " " +
"-o UserKnownHostsFile=" + SSHAuthenticationInfo.DefaultAuthenticationInfo.KnownHostsFilePath + " " +
"-o PasswordAuthentication=no " +
"-F /dev/null " + // Ignore the environment's SSH config file
"-i " + SSHAuthenticationInfo.DefaultAuthenticationInfo.PrivateKeyFilePath;
if (!string.IsNullOrEmpty (SSHPath)) {
if (StartInfo.EnvironmentVariables.ContainsKey ("GIT_SSH_COMMAND"))
StartInfo.EnvironmentVariables ["GIT_SSH_COMMAND"] = SSHPath;
else
StartInfo.EnvironmentVariables.Add ("GIT_SSH_COMMAND", SSHPath);
}
SetEnvironmentVariable ("GIT_SSH_COMMAND", GIT_SSH_COMMAND);
SetEnvironmentVariable ("LANG", "en_US");
SetEnvironmentVariable ("GIT_TERMINAL_PROMPT", "0");
if (string.IsNullOrEmpty (ExecPath))
StartInfo.Arguments = args;
else
StartInfo.Arguments = "--exec-path=\"" + ExecPath + "\" " + args;
}
void SetEnvironmentVariable (string variable, string content)
{
if (StartInfo.EnvironmentVariables.ContainsKey (variable))
StartInfo.EnvironmentVariables [variable] = content;
else
StartInfo.EnvironmentVariables.Add (variable, content);
}
}
}

View file

@ -48,23 +48,38 @@ namespace SparkleLib {
KnownHostsFilePath = IO.Path.Combine (Path, "known_hosts");
if (!IO.Directory.Exists (Path)) {
if (IO.Directory.Exists (Path)) {
ImportKeys ();
} else {
IO.Directory.CreateDirectory (Path);
CreateKeyPair ();
} else {
foreach (string file_path in IO.Directory.GetFiles (Path)) {
if (file_path.EndsWith (".key")) {
PrivateKeyFilePath = file_path;
PrivateKey = IO.File.ReadAllText (file_path);
}
}
}
if (file_path.EndsWith (".key.pub")) {
PublicKeyFilePath = file_path;
PublicKey = IO.File.ReadAllText (file_path);
}
void ImportKeys ()
{
bool key_found = false;
foreach (string file_path in IO.Directory.GetFiles (Path)) {
if (file_path.EndsWith (".key")) {
PrivateKeyFilePath = file_path;
PublicKeyFilePath = file_path + ".pub";
key_found = true;
break;
}
}
if (key_found) {
PrivateKey = IO.File.ReadAllText (PrivateKeyFilePath);
PublicKey = IO.File.ReadAllText (PublicKeyFilePath);
} else {
CreateKeyPair ();
ImportKeys ();
}
}
@ -77,6 +92,9 @@ namespace SparkleLib {
if (computer_name.EndsWith (".local"))
computer_name = computer_name.Substring (0, computer_name.Length - ".local".Length);
if (computer_name.EndsWith (".config"))
computer_name = computer_name.Substring (0, computer_name.Length - ".config".Length);
string arguments =
"-t rsa " + // Crypto type
"-b 4096 " + // Key size