-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preserve exact original redirect URL in OAuth client (#1281)
The OAuth 2.0 spec requires that redirect URLs be matched _exactly_ if specified, including matching trailing slashes. Since the .NET `Uri` type's `.ToString()` method will append a trailing slash to the end of path-less URLs (e.g., "http://foo" => "http://foo/") we need to use the `.OriginalString` property instead. Shoring up this area in anticipation for changes to support multiple GitHub redirect URLs with #594
- Loading branch information
Showing
5 changed files
with
142 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/shared/Core.Tests/Authentication/OAuth2SystemWebBrowserTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System; | ||
using GitCredentialManager.Authentication.OAuth; | ||
using GitCredentialManager.Tests.Objects; | ||
using Xunit; | ||
|
||
namespace GitCredentialManager.Tests.Authentication; | ||
|
||
public class OAuth2SystemWebBrowserTests | ||
{ | ||
[Fact] | ||
public void OAuth2SystemWebBrowser_UpdateRedirectUri_NonLoopback_ThrowsError() | ||
{ | ||
var env = new TestEnvironment(); | ||
var options = new OAuth2WebBrowserOptions(); | ||
var browser = new OAuth2SystemWebBrowser(env, options); | ||
|
||
Assert.Throws<ArgumentException>(() => browser.UpdateRedirectUri(new Uri("http://example.com"))); | ||
} | ||
|
||
[Theory] | ||
[InlineData("http://localhost:1234", "http://localhost:1234")] | ||
[InlineData("http://localhost:1234/", "http://localhost:1234/")] | ||
[InlineData("http://localhost:1234/oauth-callback", "http://localhost:1234/oauth-callback")] | ||
[InlineData("http://localhost:1234/oauth-callback/", "http://localhost:1234/oauth-callback/")] | ||
[InlineData("http://127.0.0.7:1234", "http://127.0.0.7:1234")] | ||
[InlineData("http://127.0.0.7:1234/", "http://127.0.0.7:1234/")] | ||
[InlineData("http://127.0.0.7:1234/oauth-callback", "http://127.0.0.7:1234/oauth-callback")] | ||
[InlineData("http://127.0.0.7:1234/oauth-callback/", "http://127.0.0.7:1234/oauth-callback/")] | ||
public void OAuth2SystemWebBrowser_UpdateRedirectUri_SpecificPort(string input, string expected) | ||
{ | ||
var env = new TestEnvironment(); | ||
var options = new OAuth2WebBrowserOptions(); | ||
var browser = new OAuth2SystemWebBrowser(env, options); | ||
|
||
Uri actualUri = browser.UpdateRedirectUri(new Uri(input)); | ||
|
||
Assert.Equal(expected, actualUri.OriginalString); | ||
} | ||
|
||
[Theory] | ||
[InlineData("http://localhost")] | ||
[InlineData("http://localhost/")] | ||
[InlineData("http://localhost/oauth-callback")] | ||
[InlineData("http://localhost/oauth-callback/")] | ||
[InlineData("http://127.0.0.7")] | ||
[InlineData("http://127.0.0.7/")] | ||
[InlineData("http://127.0.0.7/oauth-callback")] | ||
[InlineData("http://127.0.0.7/oauth-callback/")] | ||
public void OAuth2SystemWebBrowser_UpdateRedirectUri_AnyPort(string input) | ||
{ | ||
var env = new TestEnvironment(); | ||
var options = new OAuth2WebBrowserOptions(); | ||
var browser = new OAuth2SystemWebBrowser(env, options); | ||
|
||
var inputUri = new Uri(input); | ||
Uri actualUri = browser.UpdateRedirectUri(inputUri); | ||
|
||
Assert.Equal(inputUri.Scheme, actualUri.Scheme); | ||
Assert.Equal(inputUri.Host, actualUri.Host); | ||
Assert.Equal( | ||
inputUri.GetComponents(UriComponents.Path, UriFormat.Unescaped), | ||
actualUri.GetComponents(UriComponents.Path, UriFormat.Unescaped) | ||
); | ||
Assert.False(actualUri.IsDefaultPort); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters