Exemplo n.º 1
0
        protected override SmartSubtransportStream Action(string url, GitSmartSubtransportAction action)
        {
            string postContentType = null;
            string serviceUri;

            switch (action)
            {
            case GitSmartSubtransportAction.UploadPackList:
                serviceUri = url + "/info/refs?service=git-upload-pack";
                break;

            case GitSmartSubtransportAction.UploadPack:
                serviceUri      = url + "/git-upload-pack";
                postContentType = "application/x-git-upload-pack-request";
                break;

            case GitSmartSubtransportAction.ReceivePackList:
                serviceUri = url + "/info/refs?service=git-receive-pack";
                break;

            case GitSmartSubtransportAction.ReceivePack:
                serviceUri      = url + "/git-receive-pack";
                postContentType = "application/x-git-receive-pack-request";
                break;

            default:
                throw new InvalidOperationException();
            }

            // Grab the credentials from the user.
            var httpClient = new HttpClient {
                Timeout = TimeSpan.FromMinutes(1.0),
            };

            var res = httpClient.GetAsync(serviceUri).Result;

            if (res.StatusCode == System.Net.HttpStatusCode.Unauthorized)
            {
                var cred = (UsernamePasswordCredentials)GitCredentials.TryGet(url, "", SupportedCredentialTypes.UsernamePassword, GitCredentialsType.Tfs);

                httpClient = new HttpClient(new HttpClientHandler {
                    Credentials = new System.Net.NetworkCredential(cred.Username, cred.Password)
                })
                {
                    Timeout = TimeSpan.FromMinutes(1.0),
                };
            }

            return(new TfsSmartSubtransportStream(this)
            {
                HttpClient = httpClient,
                ServiceUri = new Uri(serviceUri),
                PostContentType = postContentType,
            });
        }
Exemplo n.º 2
0
        void OnCredentialsChanged()
        {
            Credentials.PrivateKey = privateKeyLocationTextEntry.Text ?? string.Empty;
            Credentials.PublicKey  = publicKeyLocationTextEntry.Text ?? string.Empty;
            Credentials.Passphrase = passphraseEntry.Password ?? string.Empty;

            bool privateKeyIsValid = ValidatePrivateKey(Credentials.PrivateKey);
            bool publicKeyIsValid  = System.IO.File.Exists(Credentials.PublicKey);
            bool hasPassphrase     = false;

            if (privateKeyIsValid)
            {
                hasPassphrase = passphraseEntry.Sensitive = GitCredentials.KeyHasPassphrase(Credentials.PrivateKey);
                if (!hasPassphrase)
                {
                    passphraseEntry.Password        = "";
                    passphraseEntry.PlaceholderText = passphraseEntry.Accessible.Description = GettextCatalog.GetString("Private Key is not encrypted");
                }
                warningPrivateKey.Hide();
            }
            else
            {
                warningPrivateKey.Message = GettextCatalog.GetString("Please select a valid private key file");
                warningPrivateKey.Visible = true;
            }

            if (publicKeyIsValid)
            {
                warningPublicKey.Hide();
            }
            else
            {
                warningPublicKey.Message = GettextCatalog.GetString("Please select a valid public key (.pub) file");
                warningPublicKey.Show();
            }

            CredentialsAreValid = privateKeyIsValid && publicKeyIsValid && (!hasPassphrase || Credentials.Passphrase.Length > 0);

            CredentialsChanged?.Invoke(this, EventArgs.Empty);
        }