ValidateCredential() private method

private ValidateCredential ( Credential credentials ) : void
credentials Credential
return void
        /// <summary>
        /// Writes credentials for a target URI to the credential store
        /// </summary>
        /// <param name="targetUri">The URI of the target for which credentials are being stored</param>
        /// <param name="credentials">The credentials to be stored</param>
        public void WriteCredentials(TargetUri targetUri, Credential credentials)
        {
            BaseSecureStore.ValidateTargetUri(targetUri);
            BaseSecureStore.ValidateCredential(credentials);

            string targetName = this.GetTargetName(targetUri);

            lock (_cache)
            {
                if (_cache.ContainsKey(targetName))
                {
                    _cache[targetName] = credentials;
                }
                else
                {
                    _cache.Add(targetName, credentials);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Writes credentials for a target URI to the credential store
        /// </summary>
        /// <param name="targetUri">The URI of the target for which credentials are being stored</param>
        /// <param name="credentials">The credentials to be stored</param>
        public Task <bool> WriteCredentials(TargetUri targetUri, Credential credentials)
        {
            BaseSecureStore.ValidateTargetUri(targetUri);
            BaseSecureStore.ValidateCredential(credentials);

            string targetName = GetTargetName(targetUri);

            lock (_cache)
            {
                if (_cache.ContainsKey(targetName))
                {
                    _cache[targetName] = credentials;
                }
                else
                {
                    _cache.Add(targetName, credentials);
                }
            }

            return(Task.FromResult(true));
        }
        public async Task <bool> ValidateCredentials(TargetUri targetUri, Credential credentials)
        {
            const string ValidationUrl = "https://api.github.com/user/subscriptions";

            BaseSecureStore.ValidateTargetUri(targetUri);
            BaseSecureStore.ValidateCredential(credentials);

            Trace.WriteLine("   GitHubAuthority::ValidateCredentials");

            string authString = String.Format("{0}:{1}", credentials.Username, credentials.Password);

            byte[] authBytes  = Encoding.UTF8.GetBytes(authString);
            string authEncode = Convert.ToBase64String(authBytes);

            // craft the request header for the GitHub v3 API w/ credentials
            using (HttpClientHandler handler = targetUri.HttpClientHandler)
                using (HttpClient httpClient = new HttpClient(handler)
                {
                    Timeout = TimeSpan.FromMilliseconds(RequestTimeout)
                })
                {
                    httpClient.DefaultRequestHeaders.Add("User-Agent", Global.UserAgent);
                    httpClient.DefaultRequestHeaders.Add("Accept", GitHubApiAcceptsHeaderValue);
                    httpClient.DefaultRequestHeaders.Add("Authorization", "Basic " + authEncode);

                    using (HttpResponseMessage response = await httpClient.GetAsync(ValidationUrl))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            Trace.WriteLine("   credential validation succeeded");
                            return(true);
                        }
                        else
                        {
                            Trace.WriteLine("   credential validation failed");
                            return(false);
                        }
                    }
                }
        }
 /// <summary>
 /// Sets credentials for future use with this authentication object.
 /// </summary>
 /// <remarks>Not supported.</remarks>
 /// <param name="targetUri">
 /// The uniform resource indicator of the resource access tokens are being set for.
 /// </param>
 /// <param name="credentials">The credentials being set.</param>
 public override void SetCredentials(TargetUri targetUri, Credential credentials)
 {
     BaseSecureStore.ValidateTargetUri(targetUri);
     BaseSecureStore.ValidateCredential(credentials);
 }
Exemplo n.º 5
0
        /// <summary>
        /// Validates that a `<see cref="Credential"/>` is valid to grant access to the VSTS resource referenced by `<paramref name="targetUri"/>`.
        /// <para/>
        /// Returns `<see langword="true"/>` if successful; otherwise `<see langword="false"/>`.
        /// </summary>
        /// <param name="targetUri">URI of the VSTS resource.</param>
        /// <param name="credentials">`<see cref="Credential"/>` expected to grant access to the VSTS service.</param>
        public async Task <bool> ValidateCredentials(TargetUri targetUri, Credential credentials)
        {
            BaseSecureStore.ValidateTargetUri(targetUri);
            BaseSecureStore.ValidateCredential(credentials);

            try
            {
                // Create an request to the VSTS deployment data end-point.
                HttpWebRequest request = GetConnectionDataRequest(targetUri, credentials);

                Git.Trace.WriteLine($"validating credentials against '{request.RequestUri}'.");

                // Send the request and wait for the response.
                using (HttpWebResponse response = await request.GetResponseAsync() as HttpWebResponse)
                {
                    // We're looking for 'OK 200' here, anything else is failure
                    Git.Trace.WriteLine($"server returned: ({response.StatusCode}).");
                    return(response.StatusCode == HttpStatusCode.OK);
                }
            }
            catch (WebException webException)
            {
                // Avoid invalidation credentials based on what is likely a networking problem.
                switch (webException.Status)
                {
                case WebExceptionStatus.ConnectFailure:
                case WebExceptionStatus.ConnectionClosed:
                case WebExceptionStatus.NameResolutionFailure:
                case WebExceptionStatus.ProxyNameResolutionFailure:
                case WebExceptionStatus.ReceiveFailure:
                case WebExceptionStatus.RequestCanceled:
                case WebExceptionStatus.RequestProhibitedByCachePolicy:
                case WebExceptionStatus.RequestProhibitedByProxy:
                case WebExceptionStatus.SecureChannelFailure:
                case WebExceptionStatus.SendFailure:
                case WebExceptionStatus.TrustFailure:
                {
                    Git.Trace.WriteLine($"unable to validate credentials due to '{webException.Status}'.");

                    return(true);
                }
                }

                // Even if the service responded, if the issue isn't a 400 class response then
                // the credentials were likely not rejected.
                if (webException.Response is HttpWebResponse response)
                {
                    int statusCode = (int)response.StatusCode;

                    if (statusCode < 400 && statusCode >= 500)
                    {
                        Git.Trace.WriteLine($"server returned: ({statusCode}).");

                        return(true);
                    }
                }

                Git.Trace.WriteLine($"server returned: '{webException.Message}.");
            }
            catch
            {
                Git.Trace.WriteLine("! unexpected error");
            }

            Git.Trace.WriteLine($"credential validation for '{targetUri}' failed.");
            return(false);
        }