|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. |
| 3 | +using System; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Microsoft.Git.CredentialManager; |
| 6 | + |
| 7 | +namespace GitHub |
| 8 | +{ |
| 9 | + public class GitHubHostProvider : HostProvider |
| 10 | + { |
| 11 | + private static readonly string[] GitHubCredentialScopes = |
| 12 | + { |
| 13 | + GitHubConstants.TokenScopes.Gist, |
| 14 | + GitHubConstants.TokenScopes.Repo |
| 15 | + }; |
| 16 | + |
| 17 | + private readonly IGitHubRestApi _gitHubApi; |
| 18 | + private readonly IGitHubAuthentication _gitHubAuth; |
| 19 | + |
| 20 | + public GitHubHostProvider(ICommandContext context) |
| 21 | + : this(context, new GitHubRestApi(context), new TtyGitHubPromptAuthentication(context)) { } |
| 22 | + |
| 23 | + public GitHubHostProvider(ICommandContext context, IGitHubRestApi gitHubApi, IGitHubAuthentication gitHubAuth) |
| 24 | + : base(context) |
| 25 | + { |
| 26 | + EnsureArgument.NotNull(gitHubApi, nameof(gitHubApi)); |
| 27 | + EnsureArgument.NotNull(gitHubAuth, nameof(gitHubAuth)); |
| 28 | + |
| 29 | + _gitHubApi = gitHubApi; |
| 30 | + _gitHubAuth = gitHubAuth; |
| 31 | + } |
| 32 | + |
| 33 | + public override string Name => "GitHub"; |
| 34 | + |
| 35 | + public override bool IsSupported(InputArguments input) |
| 36 | + { |
| 37 | + // We do not support unencrypted HTTP communications to GitHub, |
| 38 | + // but we report `true` here for HTTP so that we can show a helpful |
| 39 | + // error message for the user in `CreateCredentialAsync`. |
| 40 | + return input != null && |
| 41 | + (StringComparer.OrdinalIgnoreCase.Equals(input.Protocol, "http") || |
| 42 | + StringComparer.OrdinalIgnoreCase.Equals(input.Protocol, "https")) && |
| 43 | + (StringComparer.OrdinalIgnoreCase.Equals(input.Host, GitHubConstants.GitHubBaseUrlHost) || |
| 44 | + StringComparer.OrdinalIgnoreCase.Equals(input.Host, GitHubConstants.GistBaseUrlHost)); |
| 45 | + } |
| 46 | + |
| 47 | + public override string GetCredentialKey(InputArguments input) |
| 48 | + { |
| 49 | + string url = GetTargetUri(input).AbsoluteUri; |
| 50 | + |
| 51 | + // Trim trailing slash |
| 52 | + if (url.EndsWith("/")) |
| 53 | + { |
| 54 | + return url.Substring(0, url.Length - 1); |
| 55 | + } |
| 56 | + |
| 57 | + return url; |
| 58 | + } |
| 59 | + |
| 60 | + public override async Task<GitCredential> CreateCredentialAsync(InputArguments input) |
| 61 | + { |
| 62 | + // We should not allow unencrypted communication and should inform the user |
| 63 | + if (StringComparer.OrdinalIgnoreCase.Equals(input.Protocol, "http")) |
| 64 | + { |
| 65 | + throw new Exception("Unencrypted HTTP is not supported for GitHub. Ensure the repository remote URL is using HTTPS."); |
| 66 | + } |
| 67 | + |
| 68 | + Uri targetUri = GetTargetUri(input); |
| 69 | + |
| 70 | + if (_gitHubAuth.TryGetCredentials(targetUri, out string username, out string password)) |
| 71 | + { |
| 72 | + AuthenticationResult result = await _gitHubApi.AcquireTokenAsync( |
| 73 | + targetUri, username, password, null, GitHubCredentialScopes); |
| 74 | + |
| 75 | + if (result.Type == GitHubAuthenticationResultType.Success) |
| 76 | + { |
| 77 | + Context.Trace.WriteLine($"Token acquisition for '{targetUri}' succeeded"); |
| 78 | + |
| 79 | + return result.Token; |
| 80 | + } |
| 81 | + |
| 82 | + if (result.Type == GitHubAuthenticationResultType.TwoFactorApp || |
| 83 | + result.Type == GitHubAuthenticationResultType.TwoFactorSms) |
| 84 | + { |
| 85 | + bool isSms = result.Type == GitHubAuthenticationResultType.TwoFactorSms; |
| 86 | + |
| 87 | + if (_gitHubAuth.TryGetAuthenticationCode(targetUri, isSms, out string authenticationCode)) |
| 88 | + { |
| 89 | + result = await _gitHubApi.AcquireTokenAsync( |
| 90 | + targetUri, username, password, authenticationCode, GitHubCredentialScopes); |
| 91 | + |
| 92 | + if (result.Type == GitHubAuthenticationResultType.Success) |
| 93 | + { |
| 94 | + Context.Trace.WriteLine($"Token acquisition for '{targetUri}' succeeded."); |
| 95 | + |
| 96 | + return result.Token; |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + throw new Exception($"Interactive logon for '{targetUri}' failed."); |
| 103 | + } |
| 104 | + |
| 105 | + protected override void Dispose(bool disposing) |
| 106 | + { |
| 107 | + if (disposing) |
| 108 | + { |
| 109 | + _gitHubApi.Dispose(); |
| 110 | + } |
| 111 | + |
| 112 | + base.Dispose(disposing); |
| 113 | + } |
| 114 | + |
| 115 | + #region Private Methods |
| 116 | + |
| 117 | + private static Uri NormalizeUri(Uri targetUri) |
| 118 | + { |
| 119 | + if (targetUri is null) |
| 120 | + throw new ArgumentNullException(nameof(targetUri)); |
| 121 | + |
| 122 | + // Special case for gist.github.com which are git backed repositories under the hood. |
| 123 | + // Credentials for these repositories are the same as the one stored with "github.com" |
| 124 | + if (targetUri.DnsSafeHost.Equals(GitHubConstants.GistBaseUrlHost, StringComparison.OrdinalIgnoreCase)) |
| 125 | + { |
| 126 | + return new Uri("https://" + GitHubConstants.GitHubBaseUrlHost); |
| 127 | + } |
| 128 | + |
| 129 | + return targetUri; |
| 130 | + } |
| 131 | + |
| 132 | + private static Uri GetTargetUri(InputArguments input) |
| 133 | + { |
| 134 | + Uri uri = new UriBuilder |
| 135 | + { |
| 136 | + Scheme = input.Protocol, |
| 137 | + Host = input.Host, |
| 138 | + }.Uri; |
| 139 | + |
| 140 | + return NormalizeUri(uri); |
| 141 | + } |
| 142 | + |
| 143 | + #endregion |
| 144 | + } |
| 145 | +} |
0 commit comments