1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2025-05-05 12:34:06 +02:00

Optimize web identity middleware to only run on login and logout pages

This commit is contained in:
chylex 2022-10-22 21:06:26 +02:00
parent 1c2c32c2e6
commit 205b1f0697
Signed by: chylex
GPG Key ID: 4DE42C8F19A80548
2 changed files with 14 additions and 6 deletions

View File

@ -32,7 +32,7 @@ public static class PhantomIdentityExtensions {
public static void UsePhantomIdentity(this IApplicationBuilder application) {
application.UseAuthentication();
application.UseAuthorization();
application.UseMiddleware<PhantomIdentityMiddleware>();
application.UseWhen(PhantomIdentityMiddleware.AcceptsPath, static app => app.UseMiddleware<PhantomIdentityMiddleware>());
}
private static void ConfigureIdentity(IdentityOptions o) {
@ -60,9 +60,9 @@ public static class PhantomIdentityExtensions {
o.ExpireTimeSpan = TimeSpan.FromDays(30);
o.SlidingExpiration = true;
o.LoginPath = "/login";
o.LogoutPath = "/logout";
o.AccessDeniedPath = "/login";
o.LoginPath = PhantomIdentityMiddleware.LoginPath;
o.LogoutPath = PhantomIdentityMiddleware.LogoutPath;
o.AccessDeniedPath = PhantomIdentityMiddleware.LoginPath;
}
private static void ConfigureAuthorization(AuthorizationOptions o) {

View File

@ -5,6 +5,14 @@ using Phantom.Server.Web.Identity.Authentication;
namespace Phantom.Server.Web.Identity;
sealed class PhantomIdentityMiddleware {
public const string LoginPath = "/login";
public const string LogoutPath = "/logout";
public static bool AcceptsPath(HttpContext context) {
var path = context.Request.Path;
return path == LoginPath || path == LogoutPath;
}
private readonly RequestDelegate next;
public PhantomIdentityMiddleware(RequestDelegate next) {
@ -14,10 +22,10 @@ sealed class PhantomIdentityMiddleware {
[SuppressMessage("ReSharper", "UnusedMember.Global")]
public async Task InvokeAsync(HttpContext context, INavigation navigation, PhantomLoginManager loginManager) {
var path = context.Request.Path;
if (path == "/login" && context.Request.Query.TryGetValue("token", out var tokens) && tokens[0] is {} token && await loginManager.ProcessTokenAndGetReturnUrl(token) is {} returnUrl) {
if (path == LoginPath && context.Request.Query.TryGetValue("token", out var tokens) && tokens[0] is {} token && await loginManager.ProcessTokenAndGetReturnUrl(token) is {} returnUrl) {
context.Response.Redirect(navigation.BasePath + returnUrl);
}
else if (path == "/logout") {
else if (path == LogoutPath) {
await loginManager.SignOut();
context.Response.Redirect(navigation.BasePath);
}