public override async Task <ICredential> GenerateCredentialAsync(InputArguments input)
        {
            ThrowIfDisposed();

            // We should not allow unencrypted communication and should inform the user
            if (StringComparer.OrdinalIgnoreCase.Equals(input.Protocol, "http"))
            {
                throw new Exception("Unencrypted HTTP is not supported for GitHub. Ensure the repository remote URL is using HTTPS.");
            }

            Uri targetUri = GetTargetUri(input);

            ICredential credentials = await _gitHubAuth.GetCredentialsAsync(targetUri);

            AuthenticationResult result = await _gitHubApi.AcquireTokenAsync(
                targetUri, credentials, null, GitHubCredentialScopes);

            if (result.Type == GitHubAuthenticationResultType.Success)
            {
                Context.Trace.WriteLine($"Token acquisition for '{targetUri}' succeeded");

                return(result.Token);
            }

            if (result.Type == GitHubAuthenticationResultType.TwoFactorApp ||
                result.Type == GitHubAuthenticationResultType.TwoFactorSms)
            {
                bool isSms = result.Type == GitHubAuthenticationResultType.TwoFactorSms;

                string authCode = await _gitHubAuth.GetAuthenticationCodeAsync(targetUri, isSms);

                result = await _gitHubApi.AcquireTokenAsync(
                    targetUri, credentials, authCode, GitHubCredentialScopes);

                if (result.Type == GitHubAuthenticationResultType.Success)
                {
                    Context.Trace.WriteLine($"Token acquisition for '{targetUri}' succeeded.");

                    return(result.Token);
                }
            }

            throw new Exception($"Interactive logon for '{targetUri}' failed.");
        }
 public static Task <AuthenticationResult> AcquireTokenAsync(
     this IGitHubRestApi api,
     Uri targetUri,
     ICredential credentials,
     string authenticationCode,
     IEnumerable <string> scopes)
 {
     return(api.AcquireTokenAsync(
                targetUri,
                credentials?.UserName,
                credentials?.Password,
                authenticationCode,
                scopes));
 }