|
| 1 | +using System; |
| 2 | +using GitCredentialManager.Authentication.OAuth; |
| 3 | + |
| 4 | +namespace GitCredentialManager |
| 5 | +{ |
| 6 | + public class GenericOAuthConfig |
| 7 | + { |
| 8 | + public static bool TryGet(ITrace trace, ISettings settings, Uri remoteUri, out GenericOAuthConfig config) |
| 9 | + { |
| 10 | + config = new GenericOAuthConfig(); |
| 11 | + |
| 12 | + if (!settings.TryGetSetting( |
| 13 | + Constants.EnvironmentVariables.OAuthAuthzEndpoint, |
| 14 | + Constants.GitConfiguration.Credential.SectionName, |
| 15 | + Constants.GitConfiguration.Credential.OAuthAuthzEndpoint, |
| 16 | + out string authzEndpoint) || |
| 17 | + !Uri.TryCreate(remoteUri, authzEndpoint, out Uri authzEndpointUri)) |
| 18 | + { |
| 19 | + trace.WriteLine($"Invalid OAuth configuration - missing/invalid authorize endpoint: {authzEndpoint}"); |
| 20 | + config = null; |
| 21 | + return false; |
| 22 | + } |
| 23 | + |
| 24 | + if (!settings.TryGetSetting( |
| 25 | + Constants.EnvironmentVariables.OAuthTokenEndpoint, |
| 26 | + Constants.GitConfiguration.Credential.SectionName, |
| 27 | + Constants.GitConfiguration.Credential.OAuthTokenEndpoint, |
| 28 | + out string tokenEndpoint) || |
| 29 | + !Uri.TryCreate(remoteUri, tokenEndpoint, out Uri tokenEndpointUri)) |
| 30 | + { |
| 31 | + trace.WriteLine($"Invalid OAuth configuration - missing/invalid token endpoint: {tokenEndpoint}"); |
| 32 | + config = null; |
| 33 | + return false; |
| 34 | + } |
| 35 | + |
| 36 | + // Device code endpoint is optional |
| 37 | + Uri deviceEndpointUri = null; |
| 38 | + if (settings.TryGetSetting( |
| 39 | + Constants.EnvironmentVariables.OAuthDeviceEndpoint, |
| 40 | + Constants.GitConfiguration.Credential.SectionName, |
| 41 | + Constants.GitConfiguration.Credential.OAuthDeviceEndpoint, |
| 42 | + out string deviceEndpoint)) |
| 43 | + { |
| 44 | + if (!Uri.TryCreate(remoteUri, deviceEndpoint, out deviceEndpointUri)) |
| 45 | + { |
| 46 | + trace.WriteLine($"Invalid OAuth configuration - invalid device endpoint: {deviceEndpoint}"); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + config.Endpoints = new OAuth2ServerEndpoints(authzEndpointUri, tokenEndpointUri) |
| 51 | + { |
| 52 | + DeviceAuthorizationEndpoint = deviceEndpointUri |
| 53 | + }; |
| 54 | + |
| 55 | + if (settings.TryGetSetting( |
| 56 | + Constants.EnvironmentVariables.OAuthClientId, |
| 57 | + Constants.GitConfiguration.Credential.SectionName, |
| 58 | + Constants.GitConfiguration.Credential.OAuthClientId, |
| 59 | + out string clientId)) |
| 60 | + { |
| 61 | + config.ClientId = clientId; |
| 62 | + } |
| 63 | + |
| 64 | + if (settings.TryGetSetting( |
| 65 | + Constants.EnvironmentVariables.OAuthClientSecret, |
| 66 | + Constants.GitConfiguration.Credential.SectionName, |
| 67 | + Constants.GitConfiguration.Credential.OAuthClientSecret, |
| 68 | + out string clientSecret)) |
| 69 | + { |
| 70 | + config.ClientSecret = clientSecret; |
| 71 | + } |
| 72 | + |
| 73 | + if (settings.TryGetSetting( |
| 74 | + Constants.EnvironmentVariables.OAuthRedirectUri, |
| 75 | + Constants.GitConfiguration.Credential.SectionName, |
| 76 | + Constants.GitConfiguration.Credential.OAuthRedirectUri, |
| 77 | + out string redirectUrl) && |
| 78 | + Uri.TryCreate(redirectUrl, UriKind.Absolute, out Uri redirectUri)) |
| 79 | + { |
| 80 | + config.RedirectUri = redirectUri; |
| 81 | + } |
| 82 | + else |
| 83 | + { |
| 84 | + trace.WriteLine($"Invalid OAuth configuration - missing/invalid redirect URI: {redirectUrl}"); |
| 85 | + config = null; |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + if (settings.TryGetSetting( |
| 90 | + Constants.EnvironmentVariables.OAuthScopes, |
| 91 | + Constants.GitConfiguration.Credential.SectionName, |
| 92 | + Constants.GitConfiguration.Credential.OAuthScopes, |
| 93 | + out string scopesStr) && !string.IsNullOrWhiteSpace(scopesStr)) |
| 94 | + { |
| 95 | + config.Scopes = scopesStr.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); |
| 96 | + } |
| 97 | + else |
| 98 | + { |
| 99 | + config.Scopes = Array.Empty<string>(); |
| 100 | + } |
| 101 | + |
| 102 | + if (settings.TryGetSetting( |
| 103 | + Constants.EnvironmentVariables.OAuthClientAuthHeader, |
| 104 | + Constants.GitConfiguration.Credential.SectionName, |
| 105 | + Constants.GitConfiguration.Credential.OAuthClientAuthHeader, |
| 106 | + out string useHeader)) |
| 107 | + { |
| 108 | + config.UseAuthHeader = useHeader.IsTruthy(); |
| 109 | + } |
| 110 | + else |
| 111 | + { |
| 112 | + // Default to true |
| 113 | + config.UseAuthHeader = true; |
| 114 | + } |
| 115 | + |
| 116 | + config.DefaultUserName = settings.TryGetSetting( |
| 117 | + Constants.EnvironmentVariables.OAuthDefaultUserName, |
| 118 | + Constants.GitConfiguration.Credential.SectionName, |
| 119 | + Constants.GitConfiguration.Credential.OAuthDefaultUserName, |
| 120 | + out string userName) |
| 121 | + ? userName |
| 122 | + : "OAUTH_USER"; |
| 123 | + |
| 124 | + return true; |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | + public OAuth2ServerEndpoints Endpoints { get; set; } |
| 129 | + public string ClientId { get; set; } |
| 130 | + public string ClientSecret { get; set; } |
| 131 | + public Uri RedirectUri { get; set; } |
| 132 | + public string[] Scopes { get; set; } |
| 133 | + public bool UseAuthHeader { get; set; } |
| 134 | + public string DefaultUserName { get; set; } |
| 135 | + |
| 136 | + public bool SupportsDeviceCode => Endpoints.DeviceAuthorizationEndpoint != null; |
| 137 | + } |
| 138 | +} |
0 commit comments