/// <summary> /// Identify the Hosting service from the the targetUri. /// </summary> /// <param name="targetUri"></param> /// <returns> /// A <see cref="BaseAuthentication"/> instance if the targetUri represents Bitbucket, null otherwise. /// </returns> public static BaseAuthentication GetAuthentication(TargetUri targetUri, ICredentialStore personalAccessTokenStore, AcquireCredentialsDelegate acquireCredentialsCallback, AcquireAuthenticationOAuthDelegate acquireAuthenticationOAuthCallback) { BaseAuthentication authentication = null; BaseSecureStore.ValidateTargetUri(targetUri); if (personalAccessTokenStore == null) { throw new ArgumentNullException(nameof(personalAccessTokenStore), $"The `{nameof(personalAccessTokenStore)}` is null or invalid."); } if (targetUri.QueryUri.DnsSafeHost.EndsWith(BitbucketBaseUrlHost, StringComparison.OrdinalIgnoreCase)) { authentication = new Authentication(personalAccessTokenStore, acquireCredentialsCallback, acquireAuthenticationOAuthCallback); Trace.WriteLine("authentication for Bitbucket created"); } else { authentication = null; } return(authentication); }
/// <summary> /// Gets a configured authentication object for 'github.com'. /// </summary> /// <param name="targetUri"> /// The uniform resource indicator of the resource which requires authentication. /// </param> /// <param name="tokenScope">The desired scope of any personal access tokens acquired.</param> /// <param name="personalAccessTokenStore"> /// A secure secret store for any personal access tokens acquired. /// </param> /// <param name="authentication">(out) The authentication object if successful.</param> /// <returns>True if success; otherwise false.</returns> public static BaseAuthentication GetAuthentication( RuntimeContext context, TargetUri targetUri, TokenScope tokenScope, ICredentialStore personalAccessTokenStore, AcquireCredentialsDelegate acquireCredentialsCallback, AcquireAuthenticationCodeDelegate acquireAuthenticationCodeCallback, AuthenticationResultDelegate authenticationResultCallback) { BaseAuthentication authentication = null; BaseSecureStore.ValidateTargetUri(targetUri); if (personalAccessTokenStore == null) { throw new ArgumentNullException("personalAccessTokenStore", "The `personalAccessTokenStore` is null or invalid."); } if (targetUri.DnsSafeHost.EndsWith(GitHubBaseUrlHost, StringComparison.OrdinalIgnoreCase)) { var normalizedTargetUri = NormalizeUri(targetUri); authentication = new Authentication(context, normalizedTargetUri, tokenScope, personalAccessTokenStore, acquireCredentialsCallback, acquireAuthenticationCodeCallback, authenticationResultCallback); context.Trace.WriteLine($"created GitHub authentication for '{normalizedTargetUri}'."); } else { authentication = null; context.Trace.WriteLine($"not github.com, authentication creation aborted."); } return(authentication); }
public async Task <bool> ValidateCredentials(TargetUri targetUri, Credential credentials) { BaseSecureStore.ValidateTargetUri(targetUri); BaseSecureStore.ValidateCredential(credentials); 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) { Git.Trace.WriteLine($"credential validation for '{targetUri}' succeeded."); return(true); } else { Git.Trace.WriteLine($"credential validation for '{targetUri}' failed."); return(false); } } } }
/// <inheritdoc/> public async Task <bool> ValidateCredentials(TargetUri targetUri, string username, Credential credentials) { BaseSecureStore.ValidateTargetUri(targetUri); BaseSecureStore.ValidateCredential(credentials); // We don't know when the credentials arrive here if they are using OAuth or Basic Auth, // so we try both. // Try the simplest Basic Auth first var authEncode = GetEncodedCredentials(username, credentials); if (await ValidateCredentials(targetUri, GetBasicAuthHeader(authEncode))) { return(true); } // if the Basic Auth test failed then try again as OAuth if (await ValidateCredentials(targetUri, GetBearerHeaderAuthHeader(credentials.Password))) { return(true); } return(false); }
/// <inheritdoc/> public async Task <bool> ValidateCredentials(TargetUri targetUri, string username, Credential credentials) { BaseSecureStore.ValidateTargetUri(targetUri); BaseSecureStore.ValidateCredential(credentials); // We don't know when the credentials arrive here if they are using OAuth or Basic Auth, // so we try both. // Try the simplest basic authentication first var authEncode = GetEncodedCredentials(username, credentials); if (await ValidateCredentials(targetUri, credentials)) { return(true); } // If the basic authentication test failed then try again as OAuth if (await ValidateCredentials(targetUri, new Token(credentials.Password, TokenType.BitbucketPassword))) { return(true); } return(false); }