예제 #1
0
        internal static void InvalidateCredentials(GitCredentialsType type)
        {
            GitCredentialsState state;

            if (!credState.TryGetValue(type, out state))
            {
                return;
            }

            if (!string.IsNullOrEmpty(state.UrlUsed) && state.AgentForUrl.ContainsKey(state.UrlUsed))
            {
                state.AgentForUrl [state.UrlUsed] &= !state.AgentUsed;
            }

            Cleanup(state);
        }
예제 #2
0
        internal static void StoreCredentials(GitCredentialsType type)
        {
            GitCredentialsState state;

            if (!credState.TryGetValue(type, out state))
            {
                return;
            }

            var url = state.UrlUsed;
            var key = state.KeyUsed;

            state.NativePasswordUsed = false;

            if (!string.IsNullOrEmpty(url) && key != -1)
            {
                state.KeyForUrl [url] = key;
            }

            Cleanup(state);
        }
예제 #3
0
		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;
		}
예제 #4
0
		internal static void InvalidateCredentials (GitCredentialsType type)
		{
			GitCredentialsState state;
			if (!credState.TryGetValue (type, out state))
				return;

			if (!string.IsNullOrEmpty (state.UrlUsed) && state.AgentForUrl.ContainsKey (state.UrlUsed))
				state.AgentForUrl [state.UrlUsed] &= !state.AgentUsed;

			Cleanup (state);
		}
예제 #5
0
		internal static void StoreCredentials (GitCredentialsType type)
		{
			GitCredentialsState state;
			if (!credState.TryGetValue (type, out state))
				return;

			var url = state.UrlUsed;
			var key = state.KeyUsed;

			state.NativePasswordUsed = false;

			if (!string.IsNullOrEmpty (url) && key != -1)
				state.KeyForUrl [url] = key;

			Cleanup (state);
		}
예제 #6
0
        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);
        }
예제 #7
0
        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);
        }