diff --git a/src/shared/Core.Tests/GenericOAuthConfigTests.cs b/src/shared/Core.Tests/GenericOAuthConfigTests.cs index a0cb5f731..b05ae2e8b 100644 --- a/src/shared/Core.Tests/GenericOAuthConfigTests.cs +++ b/src/shared/Core.Tests/GenericOAuthConfigTests.cs @@ -72,15 +72,11 @@ public void GenericOAuthConfig_TryGet_Gitea() var protocol = "https"; var host = "example.com"; var remoteUri = new Uri($"{protocol}://{host}"); - // https://docs.gitea.com/next/development/oauth2-provider?_highlight=oauth#pre-configured-applications - const string expectedClientId = "e90ee53c-94e2-48ac-9358-a874fb9e0662"; - // https://docs.gitea.com/next/development/oauth2-provider?_highlight=oauth#endpoints - const string authzEndpoint = "/login/oauth/authorize"; - const string tokenEndpoint = "/login/oauth/access_token"; + const string expectedClientId = GenericOAuthConfig.WellKnown.GiteaClientId; string[] expectedScopes = Array.Empty(); - var expectedRedirectUri = new Uri("http://127.0.0.1"); - var expectedAuthzEndpoint = new Uri(remoteUri, authzEndpoint); - var expectedTokenEndpoint = new Uri(remoteUri, tokenEndpoint); + var expectedRedirectUri = GenericOAuthConfig.WellKnown.LocalIPv4RedirectUri; + var expectedAuthzEndpoint = new Uri(remoteUri, GenericOAuthConfig.WellKnown.GiteaAuthzEndpoint); + var expectedTokenEndpoint = new Uri(remoteUri, GenericOAuthConfig.WellKnown.GiteaTokenEndpoint); var trace = new NullTrace(); var settings = new TestSettings diff --git a/src/shared/Core/GenericOAuthConfig.cs b/src/shared/Core/GenericOAuthConfig.cs index 09d21436f..fc0434b67 100644 --- a/src/shared/Core/GenericOAuthConfig.cs +++ b/src/shared/Core/GenericOAuthConfig.cs @@ -14,15 +14,14 @@ public static bool TryGet(ITrace trace, ISettings settings, InputArguments input Uri tokenEndpointUri = null; var remoteUri = input.GetRemoteUri(); - if (input.WwwAuth.Any(x => x.Contains("Basic realm=\"Gitea\""))) + if (input.WwwAuth.Any(x => x.Contains("Basic realm=\"Gitea\"", StringComparison.OrdinalIgnoreCase))) { trace.WriteLine($"Using universal Gitea OAuth configuration"); // https://docs.gitea.com/next/development/oauth2-provider?_highlight=oauth#pre-configured-applications - config.ClientId = "e90ee53c-94e2-48ac-9358-a874fb9e0662"; - // https://docs.gitea.com/next/development/oauth2-provider?_highlight=oauth#endpoints - authzEndpointUri = new Uri(remoteUri, "/login/oauth/authorize"); - tokenEndpointUri = new Uri(remoteUri, "/login/oauth/access_token"); - config.RedirectUri = new Uri("http://127.0.0.1"); + config.ClientId = WellKnown.GiteaClientId; + authzEndpointUri = new Uri(remoteUri, WellKnown.GiteaAuthzEndpoint); + tokenEndpointUri = new Uri(remoteUri, WellKnown.GiteaTokenEndpoint); + config.RedirectUri = WellKnown.LocalIPv4RedirectUri; } if (settings.TryGetSetting( @@ -158,5 +157,15 @@ public static bool TryGet(ITrace trace, ISettings settings, InputArguments input public string DefaultUserName { get; set; } public bool SupportsDeviceCode => Endpoints.DeviceAuthorizationEndpoint != null; + + public static class WellKnown + { + // https://docs.gitea.com/next/development/oauth2-provider?_highlight=oauth#pre-configured-applications + public const string GiteaClientId = "e90ee53c-94e2-48ac-9358-a874fb9e0662"; + // https://docs.gitea.com/next/development/oauth2-provider?_highlight=oauth#endpoints + public const string GiteaAuthzEndpoint = "/login/oauth/authorize"; + public const string GiteaTokenEndpoint = "/login/oauth/access_token"; + public static Uri LocalIPv4RedirectUri = new Uri("http://127.0.0.1"); + } } }