public CloneOptions cloningSSHAuthentication() { var sshDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".ssh"); CloneOptions options = new CloneOptions(); SshUserKeyCredentials credentials = new SshUserKeyCredentials { Username = "******", PublicKey = Path.Combine(sshDir, "id_rsa.pub"), PrivateKey = Path.Combine(sshDir, "id_rsa"), Passphrase = "ce n'est rien..." }; var handler = new LibGit2Sharp.Handlers.CredentialsHandler((url, usernameFromUrl, types) => credentials); options.CredentialsProvider = handler; return(options); }
public static Credentials TryGet (string url, string userFromUrl, SupportedCredentialTypes types, GitCredentialsType type) { bool result = true; Uri uri = null; GitCredentialsState state; if (!credState.TryGetValue (type, out state)) credState [type] = state = new GitCredentialsState (); state.UrlUsed = url; // We always need to run the TryGet* methods as we need the passphraseItem/passwordItem populated even // if the password store contains an invalid password/no password if ((types & SupportedCredentialTypes.UsernamePassword) != 0) { uri = new Uri (url); string username; string password; if (!state.NativePasswordUsed && TryGetUsernamePassword (uri, out username, out password)) { state.NativePasswordUsed = true; return new UsernamePasswordCredentials { Username = username, Password = password }; } } Credentials cred; if ((types & SupportedCredentialTypes.UsernamePassword) != 0) cred = new UsernamePasswordCredentials (); else { // Try ssh-agent on Linux. if (!Platform.IsWindows && !state.AgentUsed) { bool agentUsable; if (!state.AgentForUrl.TryGetValue (url, out agentUsable)) state.AgentForUrl [url] = agentUsable = true; if (agentUsable) { state.AgentUsed = true; return new SshAgentCredentials { Username = userFromUrl, }; } } int key; if (!state.KeyForUrl.TryGetValue (url, out key)) { if (state.KeyUsed + 1 < Keys.Count) state.KeyUsed++; else { SelectFileDialog dlg = null; bool success = false; DispatchService.GuiSyncDispatch (() => { dlg = new SelectFileDialog (GettextCatalog.GetString ("Select a private SSH key to use.")); dlg.ShowHidden = true; dlg.CurrentFolder = Environment.GetFolderPath (Environment.SpecialFolder.Personal); success = dlg.Run (); }); if (!success || !File.Exists (dlg.SelectedFile + ".pub")) throw new VersionControlException (GettextCatalog.GetString ("Invalid credentials were supplied. Aborting operation.")); cred = new SshUserKeyCredentials { Username = userFromUrl, Passphrase = "", PrivateKey = dlg.SelectedFile, PublicKey = dlg.SelectedFile + ".pub", }; if (KeyHasPassphrase (dlg.SelectedFile)) { DispatchService.GuiSyncDispatch (delegate { using (var credDlg = new CredentialsDialog (url, types, cred)) result = MessageService.ShowCustomDialog (credDlg) == (int)Gtk.ResponseType.Ok; }); } if (result) return cred; throw new VersionControlException (GettextCatalog.GetString ("Invalid credentials were supplied. Aborting operation.")); } } else state.KeyUsed = key; cred = new SshUserKeyCredentials { Username = userFromUrl, Passphrase = "", PrivateKey = Keys [state.KeyUsed], PublicKey = Keys [state.KeyUsed] + ".pub", }; return cred; } DispatchService.GuiSyncDispatch (delegate { using (var credDlg = new CredentialsDialog (url, types, cred)) result = MessageService.ShowCustomDialog (credDlg) == (int)Gtk.ResponseType.Ok; }); if (result) { if ((types & SupportedCredentialTypes.UsernamePassword) != 0) { var upcred = (UsernamePasswordCredentials)cred; if (!string.IsNullOrEmpty (upcred.Password)) { PasswordService.AddWebUserNameAndPassword (uri, upcred.Username, upcred.Password); } } } return cred; }
public SshCredentialsWidget(SshUserKeyCredentials creds) { Credentials = creds ?? new SshUserKeyCredentials(); DefaultRowSpacing = XwtCredentialsDialog.InputContainerContainerSpacing; int inputContainerCurrentRow = 0; var privateKeyLocationLabel = new Label(GettextCatalog.GetString("Private Key:")) { MinWidth = XwtCredentialsDialog.DefaultlLabelWidth, TextAlignment = Alignment.End }; Add(privateKeyLocationLabel, 0, inputContainerCurrentRow, hexpand: false, vpos: WidgetPlacement.Center); var privateKeyLocationContainer = new HBox(); Add(privateKeyLocationContainer, 1, inputContainerCurrentRow, hexpand: true); privateKeyLocationTextEntry = new TextEntry { Text = Credentials.PrivateKey ?? string.Empty }; privateKeyLocationTextEntry.Accessible.LabelWidget = privateKeyLocationLabel; privateKeyLocationTextEntry.Changed += PrivateKeyLocationTextEntry_Changed; privateKeyLocationContainer.PackStart(privateKeyLocationTextEntry, true, vpos: WidgetPlacement.Center); warningPrivateKey = new InformationPopoverWidget { Severity = Ide.Tasks.TaskSeverity.Warning }; privateKeyLocationContainer.PackStart(warningPrivateKey); privateKeyLocationButton = new Button("…"); privateKeyLocationButton.Accessible.LabelWidget = privateKeyLocationLabel; privateKeyLocationButton.Accessible.Title = GettextCatalog.GetString("Select a key file"); privateKeyLocationContainer.PackStart(privateKeyLocationButton); inputContainerCurrentRow++; //Public key location var publicKeyLocationLabel = new Label(GettextCatalog.GetString("Public Key:")) { MinWidth = XwtCredentialsDialog.DefaultlLabelWidth, TextAlignment = Alignment.End }; Add(publicKeyLocationLabel, 0, inputContainerCurrentRow, hexpand: false, vpos: WidgetPlacement.Center); var publicKeyLocationContainer = new HBox(); Add(publicKeyLocationContainer, 1, inputContainerCurrentRow, hexpand: true); publicKeyLocationTextEntry = new TextEntry { Text = Credentials.PublicKey ?? string.Empty }; publicKeyLocationTextEntry.Accessible.LabelWidget = publicKeyLocationLabel; publicKeyLocationTextEntry.Changed += PublicKeyLocationTextEntry_Changed; publicKeyLocationContainer.PackStart(publicKeyLocationTextEntry, true, vpos: WidgetPlacement.Center); warningPublicKey = new InformationPopoverWidget { Severity = Ide.Tasks.TaskSeverity.Warning }; publicKeyLocationContainer.PackStart(warningPublicKey); publicKeyLocationButton = new Button("…"); publicKeyLocationButton.Accessible.LabelWidget = publicKeyLocationLabel; publicKeyLocationButton.Accessible.Title = GettextCatalog.GetString("Select a key file"); publicKeyLocationContainer.PackStart(publicKeyLocationButton); inputContainerCurrentRow++; //password container var passwordLabel = new Label() { TextAlignment = Alignment.End, Text = GettextCatalog.GetString("Passphrase:"), MinWidth = XwtCredentialsDialog.DefaultlLabelWidth }; Add(passwordLabel, 0, inputContainerCurrentRow, hexpand: false, vpos: WidgetPlacement.Center); passphraseEntry = new PasswordEntry() { MarginTop = 5 }; passphraseEntry.Accessible.LabelWidget = passwordLabel; Add(passphraseEntry, 1, inputContainerCurrentRow, hexpand: true, vpos: WidgetPlacement.Center, marginRight: Toolkit.CurrentEngine.Type == ToolkitType.XamMac ? 1 : -1); passphraseEntry.Changed += PasswordEntry_Changed; privateKeyLocationButton.Clicked += PrivateKeyLocationButton_Clicked; publicKeyLocationButton.Clicked += PublicKeyLocationButton_Clicked; OnCredentialsChanged(); }
public static Credentials TryGet(string url, string userFromUrl, SupportedCredentialTypes types, GitCredentialsType type) { bool result = false; Uri uri = null; GitCredentialsState state; if (!credState.TryGetValue(type, out state)) { credState [type] = state = new GitCredentialsState(); } state.UrlUsed = url; Credentials cred = null; if ((types & SupportedCredentialTypes.Ssh) != 0) { // Try ssh-agent on Linux. if (!Platform.IsWindows && !state.AgentUsed) { bool agentUsable; if (!state.AgentForUrl.TryGetValue(url, out agentUsable)) { state.AgentForUrl [url] = agentUsable = true; } if (agentUsable) { state.AgentUsed = true; return(new SshAgentCredentials { Username = userFromUrl, }); } } int keyIndex; if (state.KeyForUrl.TryGetValue(url, out keyIndex)) { state.KeyUsed = keyIndex; } else { if (state.KeyUsed + 1 < Keys.Count) { state.KeyUsed++; } else { var sshCred = new SshUserKeyCredentials { Username = userFromUrl, Passphrase = string.Empty }; cred = sshCred; if (XwtCredentialsDialog.Run(url, SupportedCredentialTypes.Ssh, cred).Result) { keyIndex = Keys.IndexOf(sshCred.PrivateKey); if (keyIndex < 0) { Keys.Add(sshCred.PrivateKey); PublicKeys.Add(sshCred.PublicKey); state.KeyUsed++; } else { state.KeyUsed = keyIndex; } return(cred); } throw new UserCancelledException(UserCancelledExceptionMessage); } } var key = Keys [state.KeyUsed]; cred = new SshUserKeyCredentials { Username = userFromUrl, Passphrase = string.Empty, PrivateKey = key, PublicKey = PublicKeys [state.KeyUsed] }; if (KeyHasPassphrase(key)) { if (XwtCredentialsDialog.Run(url, SupportedCredentialTypes.Ssh, cred).Result) { var sshCred = (SshUserKeyCredentials)cred; keyIndex = Keys.IndexOf(sshCred.PrivateKey); if (keyIndex < 0) { Keys.Add(sshCred.PrivateKey); PublicKeys.Add(sshCred.PublicKey); state.KeyUsed++; } else { state.KeyUsed = keyIndex; } } else { throw new UserCancelledException(UserCancelledExceptionMessage); } } return(cred); } // We always need to run the TryGet* methods as we need the passphraseItem/passwordItem populated even // if the password store contains an invalid password/no password if ((types & SupportedCredentialTypes.UsernamePassword) != 0) { if (Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out uri)) { if (!state.NativePasswordUsed && TryGetUsernamePassword(uri, out var username, out var password)) { state.NativePasswordUsed = true; return(new UsernamePasswordCredentials { Username = username, Password = password }); } } } if (cred == null) { cred = new UsernamePasswordCredentials(); } var gitCredentialsProviders = AddinManager.GetExtensionObjects <IGitCredentialsProvider> (); if (gitCredentialsProviders != null) { foreach (var gitCredentialsProvider in gitCredentialsProviders) { if (gitCredentialsProvider.SupportsUrl(url)) { var providerResult = GetCredentialsFromProvider(gitCredentialsProvider, url, types, cred); if (providerResult == GitCredentialsProviderResult.Cancelled) { throw new UserCancelledException(UserCancelledExceptionMessage); } if (result = providerResult == GitCredentialsProviderResult.Found) { break; } } } } if (!result) { result = GetCredentials(url, types, cred); } if (result) { if ((types & SupportedCredentialTypes.UsernamePassword) != 0) { var upcred = (UsernamePasswordCredentials)cred; if (!string.IsNullOrEmpty(upcred.Password) && uri != null) { PasswordService.AddWebUserNameAndPassword(uri, upcred.Username, upcred.Password); } } return(cred); } throw new UserCancelledException(UserCancelledExceptionMessage); }
public static Credentials TryGet(string url, string userFromUrl, SupportedCredentialTypes types) { bool result = true; Uri uri = null; urlUsed = url; // We always need to run the TryGet* methods as we need the passphraseItem/passwordItem populated even // if the password store contains an invalid password/no password if ((types & SupportedCredentialTypes.UsernamePassword) != 0) { uri = new Uri(url); string username; string password; if (!nativePasswordUsed && TryGetUsernamePassword(uri, out username, out password)) { nativePasswordUsed = true; return(new UsernamePasswordCredentials { Username = username, Password = password }); } } Credentials cred; if ((types & SupportedCredentialTypes.UsernamePassword) != 0) { cred = new UsernamePasswordCredentials(); } else { // Try ssh-agent on Linux. if (!Platform.IsWindows && !agentUsed) { bool agentUsable; if (!AgentForUrl.TryGetValue(url, out agentUsable)) { AgentForUrl [url] = agentUsable = true; } if (agentUsable) { agentUsed = true; return(new SshAgentCredentials { Username = userFromUrl, }); } } int key; if (!KeyForUrl.TryGetValue(url, out key)) { if (keyUsed + 1 < Keys.Count) { keyUsed++; } else { SelectFileDialog dlg = null; bool success = false; DispatchService.GuiSyncDispatch(() => { dlg = new SelectFileDialog(GettextCatalog.GetString("Select a private SSH key to use.")); dlg.ShowHidden = true; dlg.CurrentFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal); success = dlg.Run(); }); if (!success || !File.Exists(dlg.SelectedFile + ".pub")) { throw new VersionControlException(GettextCatalog.GetString("Invalid credentials were supplied. Aborting operation.")); } cred = new SshUserKeyCredentials { Username = userFromUrl, Passphrase = "", PrivateKey = dlg.SelectedFile, PublicKey = dlg.SelectedFile + ".pub", }; if (KeyHasPassphrase(dlg.SelectedFile)) { DispatchService.GuiSyncDispatch(delegate { using (var credDlg = new CredentialsDialog(url, types, cred)) result = MessageService.ShowCustomDialog(credDlg) == (int)Gtk.ResponseType.Ok; }); } if (result) { return(cred); } throw new VersionControlException(GettextCatalog.GetString("Invalid credentials were supplied. Aborting operation.")); } } else { keyUsed = key; } cred = new SshUserKeyCredentials { Username = userFromUrl, Passphrase = "", PrivateKey = Keys [keyUsed], PublicKey = Keys [keyUsed] + ".pub", }; return(cred); } DispatchService.GuiSyncDispatch(delegate { using (var credDlg = new CredentialsDialog(url, types, cred)) result = MessageService.ShowCustomDialog(credDlg) == (int)Gtk.ResponseType.Ok; }); if (result) { if ((types & SupportedCredentialTypes.UsernamePassword) != 0) { var upcred = (UsernamePasswordCredentials)cred; if (!string.IsNullOrEmpty(upcred.Password)) { PasswordService.AddWebUserNameAndPassword(uri, upcred.Username, upcred.Password); } } } return(cred); }