Skip to content

Commit 4fc94be

Browse files
authored
core: preserve Gitea deployment prefixes for OAuth (#2479)
## Summary - preserve a Gitea instance's deployment prefix when constructing universal OAuth endpoints - add a regression test for Gitea hosted under a subfolder ## Validation - `dotnet build git-credential-manager.slnx` - `dotnet test git-credential-manager.slnx` Fixes #1650
2 parents cd336c1 + 6ed474b commit 4fc94be

2 files changed

Lines changed: 55 additions & 4 deletions

File tree

‎src/Core.Tests/GenericOAuthConfigTests.cs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,40 @@ public void GenericOAuthConfig_TryGet_Gitea()
101101
Assert.Equal(expectedTokenEndpoint, config.Endpoints.TokenEndpoint);
102102
}
103103

104+
[Theory]
105+
[InlineData("", "/login/oauth/authorize", "/login/oauth/access_token")]
106+
[InlineData("/test/test.git", "/login/oauth/authorize", "/login/oauth/access_token")]
107+
[InlineData("/git/test/test.git", "/git/login/oauth/authorize", "/git/login/oauth/access_token")]
108+
[InlineData("/sub/git/test/test.git", "/sub/git/login/oauth/authorize", "/sub/git/login/oauth/access_token")]
109+
public void GenericOAuthConfig_TryGet_Gitea_PreservesDeploymentPrefix(
110+
string path, string expectedAuthzPath, string expectedTokenPath)
111+
{
112+
var protocol = "https";
113+
var host = "example.com";
114+
var remoteUri = new Uri($"{protocol}://{host}{path}");
115+
var expectedAuthzEndpoint = new Uri($"{protocol}://{host}{expectedAuthzPath}");
116+
var expectedTokenEndpoint = new Uri($"{protocol}://{host}{expectedTokenPath}");
117+
118+
var trace = new NullTrace();
119+
var settings = new TestSettings
120+
{
121+
RemoteUri = remoteUri
122+
};
123+
124+
var request = new GitRequest(new Dictionary<string, string> {
125+
{"protocol", protocol},
126+
{"host", host},
127+
{"path", path},
128+
{"wwwauth", "Basic realm=\"Gitea\""}
129+
});
130+
131+
bool result = GenericOAuthConfig.TryGet(trace, settings, request, out GenericOAuthConfig config);
132+
133+
Assert.True(result);
134+
Assert.Equal(expectedAuthzEndpoint, config.Endpoints.AuthorizationEndpoint);
135+
Assert.Equal(expectedTokenEndpoint, config.Endpoints.TokenEndpoint);
136+
}
137+
104138
[Theory]
105139
[InlineData("query", OAuth2ResponseMode.Query)]
106140
[InlineData("fragment", OAuth2ResponseMode.Fragment)]

‎src/Core/GenericOAuthConfig.cs‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public static bool TryGet(ITrace trace, ISettings settings, GitRequest request,
1919
trace.WriteLine($"Using universal Gitea OAuth configuration");
2020
// https://docs.gitea.com/next/development/oauth2-provider?_highlight=oauth#pre-configured-applications
2121
config.ClientId = WellKnown.GiteaClientId;
22-
authzEndpointUri = new Uri(remoteUri, WellKnown.GiteaAuthzEndpoint);
23-
tokenEndpointUri = new Uri(remoteUri, WellKnown.GiteaTokenEndpoint);
22+
authzEndpointUri = CreateGiteaEndpointUri(remoteUri, WellKnown.GiteaAuthzEndpoint);
23+
tokenEndpointUri = CreateGiteaEndpointUri(remoteUri, WellKnown.GiteaTokenEndpoint);
2424
config.RedirectUri = WellKnown.LocalIPv4RedirectUri;
2525
}
2626

@@ -162,6 +162,23 @@ public static bool TryGet(ITrace trace, ISettings settings, GitRequest request,
162162
return true;
163163
}
164164

165+
// A Gitea remote URL is always '{host}/{prefix}/{owner}/{repo}' — Gitea has no nested
166+
// groups — so dropping the last two segments leaves the '{host}/{prefix}' that the OAuth
167+
// endpoints hang off. When Git omits the path (credential.useHttpPath is false), or passes
168+
// fewer than two segments, the prefix is empty and the endpoints stay at the host root.
169+
private static Uri CreateGiteaEndpointUri(Uri remoteUri, string endpoint)
170+
{
171+
string[] pathSegments = remoteUri.AbsolutePath.Split('/', StringSplitOptions.RemoveEmptyEntries);
172+
string prefixPath = string.Join('/', pathSegments.Take(pathSegments.Length - 2));
173+
174+
return new UriBuilder(remoteUri)
175+
{
176+
Path = prefixPath + '/' + endpoint,
177+
Query = string.Empty,
178+
Fragment = string.Empty,
179+
}.Uri;
180+
}
181+
165182

166183
public OAuth2ServerEndpoints Endpoints { get; set; }
167184
public string ClientId { get; set; }
@@ -179,8 +196,8 @@ public static class WellKnown
179196
// https://docs.gitea.com/next/development/oauth2-provider?_highlight=oauth#pre-configured-applications
180197
public const string GiteaClientId = "e90ee53c-94e2-48ac-9358-a874fb9e0662";
181198
// https://docs.gitea.com/next/development/oauth2-provider?_highlight=oauth#endpoints
182-
public const string GiteaAuthzEndpoint = "/login/oauth/authorize";
183-
public const string GiteaTokenEndpoint = "/login/oauth/access_token";
199+
public const string GiteaAuthzEndpoint = "login/oauth/authorize";
200+
public const string GiteaTokenEndpoint = "login/oauth/access_token";
184201
public static Uri LocalIPv4RedirectUri = new Uri("http://127.0.0.1");
185202
}
186203
}

0 commit comments

Comments
 (0)