Example #1
0
 public static Task <bool> Run(string url, SupportedCredentialTypes types, Credentials cred, Components.Window parentWindow = null)
 {
     return(Runtime.RunInMainThread(() => {
         var engine = Platform.IsMac ? Toolkit.NativeEngine : Toolkit.CurrentEngine;
         var response = false;
         engine.Invoke(() => {
             using (var xwtDialog = new XwtCredentialsDialog(url, types, cred)) {
                 response = xwtDialog.Run(parentWindow ?? IdeServices.DesktopService.GetFocusedTopLevelWindow()) == Command.Ok;
             }
         });
         return response;
     }));
 }
Example #2
0
 static bool GetCredentials(string uri, SupportedCredentialTypes type, Credentials cred)
 {
     return(XwtCredentialsDialog.Run(uri, type, cred).Result);
 }
Example #3
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);
        }