Merge pull request #132 from BinaryAlley/bugfix/redirect-stale-authenticated-sessions-to-login

Redirects stale authenticated sessions to login instead of the not-found page
This commit is contained in:
BinaryAlley authored and GitHub committed 2026-08-09 00:51:07 -07:00
commit 268e07d495
4 files changed
+34 -9

No files matched your search

@@ -150,7 +150,7 @@ public class ApiHttpClient : IApiHttpClient
problemDetails = JsonSerializer.Deserialize<ProblemDetailsModel>(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<ProblemDetailsModel>(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);
}
}
@@ -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
/// </summary>
public ProblemDetailsModel? ProblemDetails { get; }
/// <summary>
/// Gets or sets the absolute path of the API endpoint that produced this exception.
/// </summary>
public string? RequestPath { get; }
/// <summary>
/// Initializes a new instance of the <see cref="ApiException"/> class.
/// </summary>
/// <param name="problemDetails">The problem details object returned by the API.</param>
/// <param name="httpStatusCode">The HTTP status code returned by the API.</param>
public ApiException(ProblemDetailsModel? problemDetails, HttpStatusCode httpStatusCode) : base(problemDetails?.Title ?? "An error occurred.")
/// <param name="requestPath">The absolute path of the API endpoint that produced this exception.</param>
public ApiException(ProblemDetailsModel? problemDetails, HttpStatusCode httpStatusCode, string? requestPath = null) : base(problemDetails?.Title ?? "An error occurred.")
{
ProblemDetails = problemDetails;
HttpStatusCode = httpStatusCode;
RequestPath = requestPath;
}
}
}
@@ -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
/// <summary>
/// Handles not found exceptions.
/// </summary>
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);
}
/// <summary>
/// Determines whether the API exception originated from the authorization check of the currently logged in user.
/// </summary>
/// <param name="apiException">The API exception to inspect.</param>
/// <returns><see langword="true"/> if the exception came from the current-user authorization endpoint, <see langword="false"/> otherwise.</returns>
private static bool IsCurrentUserAuthorizationCheck(ApiException apiException)
{
return apiException.RequestPath?.Contains("/auth/get-authorization", StringComparison.OrdinalIgnoreCase) == true;
}
}
@@ -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)
{