Exemple #1
0
        public void Add(Uri requestUri, ICredentials credentials, CredentialType credentialType)
        {
            credentialCache.TryAdd(requestUri, credentials);
            if (credentialType == CredentialType.RequestCredentials)
            {
                var rootUri = GetRootUri(requestUri);
                credentialCache.AddOrUpdate(rootUri, credentials, (u, c) => credentials);
            }

            var cred = Utility.GetCredentialsForUriFromICredentials(requestUri, credentials);

            if (cred != null && !string.IsNullOrWhiteSpace(cred.UserName) && !string.IsNullOrWhiteSpace(cred.Password))
            {
                PasswordService.AddWebUserNameAndPassword(requestUri, cred.UserName, cred.Password);
            }
        }
        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)
            {
                var user = items.OfType <CredentialItem.Username> ().FirstOrDefault();
                if (passwordItem != null)
                {
                    PasswordService.AddWebUserNameAndPassword(new Uri(uri.ToString()), user.GetValue(), new string (passwordItem.GetValue()));
                }
                else if (passphraseItem != null)
                {
                    PasswordService.AddWebPassword(new Uri(uri.ToString()), passphraseItem.GetValue());
                }
            }
            return(result);
        }
        static ICredentials GetCredentialsFromUser(Uri uri, IWebProxy proxy, CredentialType credentialType)
        {
            NetworkCredential result = null;

            Runtime.RunInMainThread(() => {
                var form = new PlaceholderForm(credentialType, uri, null);
                if (GdkWin32.RunModalWin32Form(form, IdeApp.Workbench.RootWindow))
                {
                    result = new NetworkCredential(form.Username, form.Password, form.Domain);
                }
            }).Wait();

            // store the obtained credentials in the auth store
            // but don't store for the root url since it may have other credentials
            if (result != null)
            {
                PasswordService.AddWebUserNameAndPassword(uri, result.UserName, result.Password);
            }

            return(result);
        }
Exemple #4
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);
        }
Exemple #5
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);
        }