/// <summary> /// /// </summary> /// <param name="tokenScope"></param> /// <param name="personalAccessTokenStore"></param> public GitHubAuthentication( GitHubTokenScope tokenScope, ICredentialStore personalAccessTokenStore, AcquireCredentialsDelegate acquireCredentialsCallback, AcquireAuthenticationCodeDelegate acquireAuthenticationCodeCallback, AuthenticationResultDelegate authenticationResultCallback) { if (tokenScope == null) { throw new ArgumentNullException("tokenScope", "The parameter `tokenScope` is null or invalid."); } if (personalAccessTokenStore == null) { throw new ArgumentNullException("personalAccessTokenStore", "The parameter `personalAccessTokenStore` is null or invalid."); } if (acquireCredentialsCallback == null) { throw new ArgumentNullException("acquireCredentialsCallback", "The parameter `acquireCredentialsCallback` is null or invalid."); } if (acquireAuthenticationCodeCallback == null) { throw new ArgumentNullException("acquireAuthenticationCodeCallback", "The parameter `acquireAuthenticationCodeCallback` is null or invalid."); } TokenScope = tokenScope; PersonalAccessTokenStore = personalAccessTokenStore; GitHubAuthority = new GitHubAuthority(); AcquireCredentialsCallback = acquireCredentialsCallback; AcquireAuthenticationCodeCallback = acquireAuthenticationCodeCallback; AuthenticationResultCallback = authenticationResultCallback; }
/// <summary> /// <para></para> /// <para>Tokens acquired are stored in the secure secret store provided during /// initialization.</para> /// </summary> /// <param name="targetUri">The unique identifier for the resource for which access is to /// be acquired.</param> /// <param name="username">The username of the account for which access is to be acquired.</param> /// <param name="password">The password of the account for which access is to be acquired.</param> /// <param name="authenticationCode">The two-factor authentication code for use in access acquisition.</param> /// <returns>True if success; otherwise false.</returns> public async Task <bool> NoninteractiveLogonWithCredentials(TargetUri targetUri, string username, string password, string authenticationCode = null) { BaseSecureStore.ValidateTargetUri(targetUri); if (String.IsNullOrWhiteSpace(username)) { throw new ArgumentNullException("username", "The `username` parameter is null or invalid."); } if (String.IsNullOrWhiteSpace(password)) { throw new ArgumentNullException("username", "The `password` parameter is null or invalid."); } Trace.WriteLine("GitHubAuthentication::NoninteractiveLogonWithCredentials"); GitHubAuthenticationResult result; if (result = await GitHubAuthority.AcquireToken(targetUri, username, password, authenticationCode, this.TokenScope)) { Trace.WriteLine(" token acquisition succeeded"); PersonalAccessTokenStore.WriteCredentials(targetUri, (Credential)result.Token); return(true); } Trace.WriteLine(" non-interactive logon failed"); return(false); }
/// <summary> /// /// </summary> /// <param name="tokenScope"></param> /// <param name="personalAccessTokenStore"></param> public GitHubAuthentication( GitHubTokenScope tokenScope, ICredentialStore personalAccessTokenStore, AcquireCredentialsDelegate acquireCredentialsCallback, AcquireAuthenticationCodeDelegate acquireAuthenticationCodeCallback, AuthenticationResultDelegate authenticationResultCallback) { if (tokenScope == null) throw new ArgumentNullException("tokenScope", "The parameter `tokenScope` is null or invalid."); if (personalAccessTokenStore == null) throw new ArgumentNullException("personalAccessTokenStore", "The parameter `personalAccessTokenStore` is null or invalid."); if (acquireCredentialsCallback == null) throw new ArgumentNullException("acquireCredentialsCallback", "The parameter `acquireCredentialsCallback` is null or invalid."); if (acquireAuthenticationCodeCallback == null) throw new ArgumentNullException("acquireAuthenticationCodeCallback", "The parameter `acquireAuthenticationCodeCallback` is null or invalid."); TokenScope = tokenScope; PersonalAccessTokenStore = personalAccessTokenStore; GitHubAuthority = new GitHubAuthority(); AcquireCredentialsCallback = acquireCredentialsCallback; AcquireAuthenticationCodeCallback = acquireAuthenticationCodeCallback; AuthenticationResultCallback = authenticationResultCallback; }
/// <summary> /// Validates that a set of credentials grants access to the target resource. /// </summary> /// <param name="targetUri">The unique identifier for the resource for which credentials /// are being validated against.</param> /// <param name="credentials">The credentials to validate.</param> /// <returns>True is successful; otherwise false.</returns> public async Task <bool> ValidateCredentials(TargetUri targetUri, Credential credentials) { BaseSecureStore.ValidateTargetUri(targetUri); BaseSecureStore.ValidateCredential(credentials); return(await GitHubAuthority.ValidateCredentials(targetUri, credentials)); }
/// <summary> /// <para></para> /// <para>Tokens acquired are stored in the secure secret store provided during /// initialization.</para> /// </summary> /// <param name="targetUri">The unique identifier for the resource for which access is to /// be acquired.</param> /// <param name="username">The username of the account for which access is to be acquired.</param> /// <param name="password">The password of the account for which access is to be acquired.</param> /// <param name="authenticationCode">The two-factor authentication code for use in access acquisition.</param> /// <returns>Acquired <see cref="Credential"/> if successful; otherwise <see langword="null"/>.</returns> public async Task <Credential> NoninteractiveLogonWithCredentials(TargetUri targetUri, string username, string password, string authenticationCode) { BaseSecureStore.ValidateTargetUri(targetUri); if (String.IsNullOrWhiteSpace(username)) { throw new ArgumentNullException("username", "The `username` parameter is null or invalid."); } if (String.IsNullOrWhiteSpace(password)) { throw new ArgumentNullException("username", "The `password` parameter is null or invalid."); } Credential credentials = null; GitHubAuthenticationResult result; if (result = await GitHubAuthority.AcquireToken(targetUri, username, password, authenticationCode, this.TokenScope)) { Git.Trace.WriteLine($"token acquisition for '{targetUri}' succeeded."); credentials = (Credential)result.Token; PersonalAccessTokenStore.WriteCredentials(targetUri, credentials); return(credentials); } Git.Trace.WriteLine($"non-interactive logon for '{targetUri}' failed."); return(credentials); }
/// <summary> /// <para></para> /// <para>Tokens acquired are stored in the secure secret store provided during /// initialization.</para> /// </summary> /// <param name="targetUri">The unique identifier for the resource for which access is to /// be acquired.</param> /// <param name="credentials">(out) Credentials when acquisition is successful; null otherwise.</param> /// <returns>True if success; otherwise false.</returns> public bool InteractiveLogon(TargetUri targetUri, out Credential credentials) { string username; string password; if (AcquireCredentialsCallback(targetUri, out username, out password)) { GitHubAuthenticationResult result; if (result = GitHubAuthority.AcquireToken(targetUri, username, password, null, this.TokenScope).Result) { Trace.WriteLine(" token acquisition succeeded"); credentials = (Credential)result.Token; this.PersonalAccessTokenStore.WriteCredentials(targetUri, credentials); // if a result callback was registered, call it if (AuthenticationResultCallback != null) { AuthenticationResultCallback(targetUri, result); } return(true); } else if (result == GitHubAuthenticationResultType.TwoFactorApp || result == GitHubAuthenticationResultType.TwoFactorSms) { string authenticationCode; if (AcquireAuthenticationCodeCallback(targetUri, result, username, out authenticationCode)) { if (result = GitHubAuthority.AcquireToken(targetUri, username, password, authenticationCode, this.TokenScope).Result) { Trace.WriteLine(" token acquisition succeeded"); credentials = (Credential)result.Token; this.PersonalAccessTokenStore.WriteCredentials(targetUri, credentials); // if a result callback was registered, call it if (AuthenticationResultCallback != null) { AuthenticationResultCallback(targetUri, result); } return(true); } } } // if a result callback was registered, call it if (AuthenticationResultCallback != null) { AuthenticationResultCallback(targetUri, result); } } Trace.WriteLine(" interactive logon failed"); credentials = null; return(false); }
/// <summary> /// Validates that a set of credentials grants access to the target resource. /// </summary> /// <param name="targetUri">The unique identifier for the resource for which credentials /// are being validated against.</param> /// <param name="credentials">The credentials to validate.</param> /// <returns>True is successful; otherwise false.</returns> public async Task <bool> ValidateCredentials(TargetUri targetUri, Credential credentials) { BaseSecureStore.ValidateTargetUri(targetUri); Credential.Validate(credentials); Trace.WriteLine("GitHubAuthentication::ValidateCredentials"); return(await GitHubAuthority.ValidateCredentials(targetUri, credentials)); }
/// <summary> /// <para></para> /// <para>Tokens acquired are stored in the secure secret store provided during /// initialization.</para> /// </summary> /// <param name="targetUri">The unique identifier for the resource for which access is to /// be acquired.</param> /// /// <returns>Acquired <see cref="Credential"/> if successful; otherwise <see langword="null"/>.</returns> public async Task <Credential> InteractiveLogon(TargetUri targetUri) { Credential credentials = null; string username; string password; if (AcquireCredentialsCallback(targetUri, out username, out password)) { GitHubAuthenticationResult result; if (result = await GitHubAuthority.AcquireToken(targetUri, username, password, null, this.TokenScope)) { Git.Trace.WriteLine($"token acquisition for '{targetUri}' succeeded"); credentials = (Credential)result.Token; this.PersonalAccessTokenStore.WriteCredentials(targetUri, credentials); // if a result callback was registered, call it AuthenticationResultCallback?.Invoke(targetUri, result); return(credentials); } else if (result == GitHubAuthenticationResultType.TwoFactorApp || result == GitHubAuthenticationResultType.TwoFactorSms) { string authenticationCode; if (AcquireAuthenticationCodeCallback(targetUri, result, username, out authenticationCode)) { if (result = await GitHubAuthority.AcquireToken(targetUri, username, password, authenticationCode, this.TokenScope)) { Git.Trace.WriteLine($"token acquisition for '{targetUri}' succeeded."); credentials = (Credential)result.Token; this.PersonalAccessTokenStore.WriteCredentials(targetUri, credentials); // if a result callback was registered, call it AuthenticationResultCallback?.Invoke(targetUri, result); return(credentials); } } } // if a result callback was registered, call it if (AuthenticationResultCallback != null) { AuthenticationResultCallback(targetUri, result); } } Git.Trace.WriteLine($"interactive logon for '{targetUri}' failed."); return(credentials); }