Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Matthew John Cheetham <mjcheetham@github.com>
  • Loading branch information
hickford and Matthew John Cheetham committed Oct 23, 2023
1 parent c512d1b commit 66292a9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
12 changes: 4 additions & 8 deletions src/shared/Core.Tests/GenericOAuthConfigTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>();
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
Expand Down
21 changes: 15 additions & 6 deletions src/shared/Core/GenericOAuthConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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");
}
}
}

0 comments on commit 66292a9

Please sign in to comment.