mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 17:11:10 +00:00
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:
4 files changed
+34
-9
No files matched your search
@@ -150,7 +150,7 @@ public class ApiHttpClient : IApiHttpClient
|
|||||||
problemDetails = JsonSerializer.Deserialize<ProblemDetailsModel>(content, _jsonOptions);
|
problemDetails = JsonSerializer.Deserialize<ProblemDetailsModel>(content, _jsonOptions);
|
||||||
}
|
}
|
||||||
catch { /* if we can't deserialize to ProblemDetails, we'll just use the status code */ }
|
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);
|
problemDetails = JsonSerializer.Deserialize<ProblemDetailsModel>(content, _jsonOptions);
|
||||||
}
|
}
|
||||||
catch { /* if we can't deserialize to ProblemDetails, we'll just use the status code */ }
|
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 Lumina.Presentation.Web.Common.Models.Common;
|
||||||
using System;
|
using System;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
@@ -21,14 +21,21 @@ public class ApiException : Exception
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public ProblemDetailsModel? ProblemDetails { get; }
|
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>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="ApiException"/> class.
|
/// Initializes a new instance of the <see cref="ApiException"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="problemDetails">The problem details object returned by the API.</param>
|
/// <param name="problemDetails">The problem details object returned by the API.</param>
|
||||||
/// <param name="httpStatusCode">The HTTP status code 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;
|
ProblemDetails = problemDetails;
|
||||||
HttpStatusCode = httpStatusCode;
|
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)
|
if (apiException.HttpStatusCode == HttpStatusCode.Unauthorized) // HTTP 401 Unauthorized actually means lack of valid authentication credentials (not logged in)
|
||||||
await HandleUnauthorizedAsync(context);
|
await HandleUnauthorizedAsync(context);
|
||||||
else if (apiException.HttpStatusCode == HttpStatusCode.NotFound) //
|
else if (apiException.HttpStatusCode == HttpStatusCode.NotFound) //
|
||||||
await HandleNotFoundAsync(context);
|
await HandleNotFoundAsync(context, apiException);
|
||||||
else if (apiException.HttpStatusCode == HttpStatusCode.Forbidden)
|
else if (apiException.HttpStatusCode == HttpStatusCode.Forbidden)
|
||||||
{
|
{
|
||||||
if (IsApiRequest(context))
|
if (IsApiRequest(context))
|
||||||
@@ -110,8 +110,16 @@ public class ExceptionHandlingMiddleware
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Handles not found exceptions.
|
/// Handles not found exceptions.
|
||||||
/// </summary>
|
/// </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))
|
if (IsApiRequest(context))
|
||||||
{
|
{
|
||||||
context.Response.StatusCode = (int)HttpStatusCode.NotFound;
|
context.Response.StatusCode = (int)HttpStatusCode.NotFound;
|
||||||
@@ -210,4 +218,14 @@ public class ExceptionHandlingMiddleware
|
|||||||
context.Response.ContentType = "application/json";
|
context.Response.ContentType = "application/json";
|
||||||
await context.Response.WriteAsJsonAsync(response);
|
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,
|
// 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!
|
// 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);
|
await _hybridCache.RemoveAsync(AUTHORIZATION_ENDPOINT, cancellationToken).ConfigureAwait(false);
|
||||||
return new HttpResponseMessage(response.StatusCode)
|
return new HttpResponseMessage(response.StatusCode)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in new issue
Block a user