From 6a006ef94599f880eb885837b20bb8ef6ea70223 Mon Sep 17 00:00:00 2001 From: Binary Alley Date: Sun, 9 Aug 2026 00:45:31 -0700 Subject: [PATCH] Redirects stale authenticated sessions to login instead of the not-found page --- .../Common/Api/ApiHttpClient.cs | 4 ++-- .../Common/Exceptions/ApiException.cs | 13 +++++++--- .../ExceptionHandlingMiddleware.cs | 24 ++++++++++++++++--- .../Services/CachedAuthorizationHandler.cs | 2 +- 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/Lumina.Presentation.Web/Common/Api/ApiHttpClient.cs b/src/Lumina.Presentation.Web/Common/Api/ApiHttpClient.cs index f904959d..350dce6b 100644 --- a/src/Lumina.Presentation.Web/Common/Api/ApiHttpClient.cs +++ b/src/Lumina.Presentation.Web/Common/Api/ApiHttpClient.cs @@ -150,7 +150,7 @@ public class ApiHttpClient : IApiHttpClient problemDetails = JsonSerializer.Deserialize(content, _jsonOptions); } catch { /* if we can't deserialize to ProblemDetails, we'll just use the status code */ } - throw new ApiException(problemDetails, response.StatusCode); + throw new ApiException(problemDetails, response.StatusCode, request.RequestUri?.AbsolutePath); } } @@ -223,7 +223,7 @@ public class ApiHttpClient : IApiHttpClient problemDetails = JsonSerializer.Deserialize(content, _jsonOptions); } catch { /* if we can't deserialize to ProblemDetails, we'll just use the status code */ } - throw new ApiException(problemDetails, response.StatusCode); + throw new ApiException(problemDetails, response.StatusCode, request.RequestUri?.AbsolutePath); } } diff --git a/src/Lumina.Presentation.Web/Common/Exceptions/ApiException.cs b/src/Lumina.Presentation.Web/Common/Exceptions/ApiException.cs index 48cbf41d..5557f482 100644 --- a/src/Lumina.Presentation.Web/Common/Exceptions/ApiException.cs +++ b/src/Lumina.Presentation.Web/Common/Exceptions/ApiException.cs @@ -1,4 +1,4 @@ -#region ========================================================================= USING ===================================================================================== +#region ========================================================================= USING ===================================================================================== using Lumina.Presentation.Web.Common.Models.Common; using System; using System.Net; @@ -21,14 +21,21 @@ public class ApiException : Exception /// public ProblemDetailsModel? ProblemDetails { get; } + /// + /// Gets or sets the absolute path of the API endpoint that produced this exception. + /// + public string? RequestPath { get; } + /// /// Initializes a new instance of the class. /// /// The problem details object returned by the API. /// The HTTP status code returned by the API. - public ApiException(ProblemDetailsModel? problemDetails, HttpStatusCode httpStatusCode) : base(problemDetails?.Title ?? "An error occurred.") + /// The absolute path of the API endpoint that produced this exception. + public ApiException(ProblemDetailsModel? problemDetails, HttpStatusCode httpStatusCode, string? requestPath = null) : base(problemDetails?.Title ?? "An error occurred.") { ProblemDetails = problemDetails; HttpStatusCode = httpStatusCode; + RequestPath = requestPath; } -} \ No newline at end of file +} diff --git a/src/Lumina.Presentation.Web/Common/Middlewares/ExceptionHandlingMiddleware.cs b/src/Lumina.Presentation.Web/Common/Middlewares/ExceptionHandlingMiddleware.cs index 05c52008..5be0b61e 100644 --- a/src/Lumina.Presentation.Web/Common/Middlewares/ExceptionHandlingMiddleware.cs +++ b/src/Lumina.Presentation.Web/Common/Middlewares/ExceptionHandlingMiddleware.cs @@ -81,7 +81,7 @@ public class ExceptionHandlingMiddleware if (apiException.HttpStatusCode == HttpStatusCode.Unauthorized) // HTTP 401 Unauthorized actually means lack of valid authentication credentials (not logged in) await HandleUnauthorizedAsync(context); else if (apiException.HttpStatusCode == HttpStatusCode.NotFound) // - await HandleNotFoundAsync(context); + await HandleNotFoundAsync(context, apiException); else if (apiException.HttpStatusCode == HttpStatusCode.Forbidden) { if (IsApiRequest(context)) @@ -110,8 +110,16 @@ public class ExceptionHandlingMiddleware /// /// Handles not found exceptions. /// - private async Task HandleNotFoundAsync(HttpContext context) - { + private async Task HandleNotFoundAsync(HttpContext context, ApiException apiException) + { + // when the API reports the currently logged-in user no longer exists (404 from the current-user authorization check), + // the stored session is stale: sign out and send the user to the login page instead of the not-found page + if (context.User?.Identity?.IsAuthenticated == true && IsCurrentUserAuthorizationCheck(apiException)) + { + await HandleUnauthorizedAsync(context).ConfigureAwait(false); + return; + } + if (IsApiRequest(context)) { context.Response.StatusCode = (int)HttpStatusCode.NotFound; @@ -210,4 +218,14 @@ public class ExceptionHandlingMiddleware context.Response.ContentType = "application/json"; await context.Response.WriteAsJsonAsync(response); } + + /// + /// Determines whether the API exception originated from the authorization check of the currently logged in user. + /// + /// The API exception to inspect. + /// if the exception came from the current-user authorization endpoint, otherwise. + private static bool IsCurrentUserAuthorizationCheck(ApiException apiException) + { + return apiException.RequestPath?.Contains("/auth/get-authorization", StringComparison.OrdinalIgnoreCase) == true; + } } diff --git a/src/Lumina.Presentation.Web/Common/Services/CachedAuthorizationHandler.cs b/src/Lumina.Presentation.Web/Common/Services/CachedAuthorizationHandler.cs index 0dfcc01a..3bd96147 100644 --- a/src/Lumina.Presentation.Web/Common/Services/CachedAuthorizationHandler.cs +++ b/src/Lumina.Presentation.Web/Common/Services/CachedAuthorizationHandler.cs @@ -69,7 +69,7 @@ public class CachedAuthorizationHandler : DelegatingHandler ); // if the API returned 401 Unauthorized (i.e. token expired), DO NOT cache this response - it will be used for authorization endpoints even after successful login, // resulting in incorrect login redirection cycles! - if (response.StatusCode == HttpStatusCode.Unauthorized) + if (response.StatusCode is HttpStatusCode.Unauthorized or HttpStatusCode.NotFound) await _hybridCache.RemoveAsync(AUTHORIZATION_ENDPOINT, cancellationToken).ConfigureAwait(false); return new HttpResponseMessage(response.StatusCode) {