-
Notifications
You must be signed in to change notification settings - Fork 733
Redirect to homepage if login page is requested when token auth is not enabled #5123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
adamint
merged 6 commits into
dotnet:main
from
adamint:dev/adamint/3775-redirect-to-root-on-no-token-auth
Aug 5, 2024
+120
−14
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
eb95e86
Redirect to homepage if login page is requested when token auth is no…
adamint a743653
add ValidateTokenMiddleware tests
adamint 18863ca
fix test names
adamint 1baace5
remove nsubstitute
adamint d199dfe
remove unsecured testing changes
adamint 9375985
Update src/Aspire.Dashboard/Model/ValidateTokenMiddleware.cs
JamesNK File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
96 changes: 96 additions & 0 deletions
96
tests/Aspire.Dashboard.Tests/Middleware/ValidateTokenMiddlewareTests.cs
This file contains hidden or 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,96 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Aspire.Dashboard.Configuration; | ||
| using Aspire.Dashboard.Model; | ||
| using Microsoft.AspNetCore.Builder; | ||
| using Microsoft.AspNetCore.Hosting; | ||
| using Microsoft.AspNetCore.TestHost; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Hosting; | ||
| using Xunit; | ||
|
|
||
| namespace Aspire.Dashboard.Tests.Middleware; | ||
|
|
||
| public class ValidateTokenMiddlewareTests | ||
| { | ||
| [Fact] | ||
| public async Task ValidateToken_NotBrowserTokenAuth_RedirectedToHomepage() | ||
| { | ||
| using var host = await SetUpHostAsync(FrontendAuthMode.Unsecured, string.Empty); | ||
| var response = await host.GetTestClient().GetAsync("/login?t=test"); | ||
| Assert.Equal("/", response.Headers.Location?.OriginalString); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ValidateToken_NotBrowserTokenAuth_RedirectedToReturnUrl() | ||
| { | ||
| using var host = await SetUpHostAsync(FrontendAuthMode.Unsecured, string.Empty); | ||
| var response = await host.GetTestClient().GetAsync("/login?t=test&returnUrl=/test"); | ||
| Assert.Equal("/test", response.Headers.Location?.OriginalString); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ValidateToken_BrowserTokenAuth_WrongToken_RedirectsToLogin() | ||
| { | ||
| using var host = await SetUpHostAsync(FrontendAuthMode.BrowserToken, "token"); | ||
| var response = await host.GetTestClient().GetAsync("/login?t=wrong"); | ||
| Assert.Equal("/login", response.Headers.Location?.OriginalString); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ValidateToken_BrowserTokenAuth_WrongToken_RedirectsToLogin_WithReturnUrl() | ||
| { | ||
| using var host = await SetUpHostAsync(FrontendAuthMode.BrowserToken, "token"); | ||
| var response = await host.GetTestClient().GetAsync("/login?t=wrong&returnUrl=/test"); | ||
| Assert.Equal("/login?returnUrl=%2ftest", response.Headers.Location?.OriginalString); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ValidateToken_BrowserTokenAuth_RightToken_RedirectsToHome() | ||
| { | ||
| using var host = await SetUpHostAsync(FrontendAuthMode.BrowserToken, "token"); | ||
| var response = await host.GetTestClient().GetAsync("/login?t=token"); | ||
| Assert.Equal("/", response.Headers.Location?.OriginalString); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ValidateToken_BrowserTokenAuth_RightToken_RedirectsToReturnUrl() | ||
| { | ||
| using var host = await SetUpHostAsync(FrontendAuthMode.BrowserToken, "token"); | ||
| var response = await host.GetTestClient().GetAsync("/login?t=token&returnUrl=/test"); | ||
| Assert.Equal("/test", response.Headers.Location?.OriginalString); | ||
| } | ||
|
|
||
| private static async Task<IHost> SetUpHostAsync(FrontendAuthMode authMode, string expectedToken) | ||
| { | ||
| return await new HostBuilder() | ||
| .ConfigureWebHost(webBuilder => | ||
| { | ||
| webBuilder | ||
| .UseTestServer() | ||
| .ConfigureServices(services => | ||
| { | ||
| services.AddRouting(); | ||
| services.AddAuthentication().AddCookie(); | ||
|
|
||
| services.Configure<DashboardOptions>(o => | ||
| { | ||
| o.Frontend = new FrontendOptions | ||
| { | ||
| AuthMode = authMode, | ||
| BrowserToken = expectedToken, | ||
| EndpointUrls = "http://localhost/" // required for TryParseOptions | ||
| }; | ||
|
|
||
| Assert.True(o.Frontend.TryParseOptions(out _)); | ||
| }); | ||
| }) | ||
| .Configure(app => | ||
| { | ||
| app.UseMiddleware<ValidateTokenMiddleware>(); | ||
| }); | ||
| }) | ||
| .StartAsync(); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.