public override bool Get (URIish uri, params CredentialItem[] items)
		{
			bool result = false;
			CredentialItem.Password passwordItem = null;
			CredentialItem.StringType passphraseItem = null;
			
			// 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 (TryGetUsernamePassword (uri, items, out passwordItem) || TryGetPassphrase (uri, items, out passphraseItem)) {
				// If the password store has a password and we already tried using it, it could be incorrect.
				// If this happens, do not return true and ask the user for a new password.
				if (!HasReset) {
					return true;
				}
			}

			DispatchService.GuiSyncDispatch (delegate {
				CredentialsDialog dlg = new CredentialsDialog (uri, items);
				try {
					result = MessageService.ShowCustomDialog (dlg) == (int)Gtk.ResponseType.Ok;
				} finally {
					dlg.Destroy ();
				}
			});
				
			HasReset = false;
			if (result) {
				if (passwordItem != null) {
					PasswordService.AddWebPassword (new Uri (uri.ToString ()), new string (passwordItem.GetValue ()));
				} else if (passphraseItem != null) {
					PasswordService.AddWebPassword (new Uri (uri.ToString ()), passphraseItem.GetValue ());
				}
			}
			return result;
		}
示例#2
0
		public override bool Get (URIish uri, params CredentialItem[] items)
		{
			bool result = false;
			DispatchService.GuiSyncDispatch (delegate {
				CredentialsDialog dlg = new CredentialsDialog (uri, items);
				try {
					result = MessageService.ShowCustomDialog (dlg) == (int)Gtk.ResponseType.Ok;
				} finally {
					dlg.Destroy ();
				}
			});
			return result;
		}
示例#3
0
        public override bool Get(URIish uri, params CredentialItem[] items)
        {
            bool result = false;

            DispatchService.GuiSyncDispatch(delegate {
                CredentialsDialog dlg = new CredentialsDialog(uri, items);
                try {
                    result = MessageService.ShowCustomDialog(dlg) == (int)Gtk.ResponseType.Ok;
                } finally {
                    dlg.Destroy();
                }
            });
            return(result);
        }
示例#4
0
        public override bool Get(URIish uri, params CredentialItem[] items)
        {
            bool result = false;

            CredentialItem.Password   passwordItem   = null;
            CredentialItem.StringType passphraseItem = null;

            // 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 (TryGetUsernamePassword(uri, items, out passwordItem) || TryGetPassphrase(uri, items, out passphraseItem))
            {
                // If the password store has a password and we already tried using it, it could be incorrect.
                // If this happens, do not return true and ask the user for a new password.
                if (!HasReset)
                {
                    return(true);
                }
            }

            DispatchService.GuiSyncDispatch(delegate
            {
                CredentialsDialog dlg = new CredentialsDialog(uri, items);
                try
                {
                    result = MessageService.ShowCustomDialog(dlg) == (int)Gtk.ResponseType.Ok;
                }
                finally
                {
                    dlg.Destroy();
                }
            });

            HasReset = false;
            if (result)
            {
                if (passwordItem != null)
                {
                    PasswordService.AddWebPassword(new Uri(uri.ToString()), new string (passwordItem.GetValue()));
                }
                else if (passphraseItem != null)
                {
                    PasswordService.AddWebPassword(new Uri(uri.ToString()), passphraseItem.GetValue());
                }
            }
            return(result);
        }
示例#5
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;
		}
示例#6
0
        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);
        }