Beschikbaarheidspoort: verlopen token op een geblokkeerde route geeft 401 i.p.v. 503, zodat de app de sessie stil ververst en als admin door de poort komt

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-09 20:58:12 +01:00
co-authored by Claude Sonnet 5.5
parent e17d967b3f
commit 85d0f6a1b1
2 changed files with 97 additions and 3 deletions
@@ -69,7 +69,8 @@ public class AvailabilityMiddleware
return;
}
if (IsAdminBypass(context))
var admin = CheckAdmin(context);
if (admin.IsVerifiedAdmin)
{
await _next(context);
return;
@@ -81,6 +82,7 @@ public class AvailabilityMiddleware
_logger.LogWarning(
"Request blocked by master gate. Path: {Path}, DisableMessage: {DisableMessage}",
path, masterStatus.DisableMessage);
if (await TryAnswerExpiredTokenAsync(context, admin)) return;
context.Response.StatusCode = StatusCodes.Status503ServiceUnavailable;
await context.Response.WriteAsJsonAsync(new ProblemDetails
{
@@ -100,6 +102,7 @@ public class AvailabilityMiddleware
}
_logger.LogWarning("Request geblokkeerd vanwege systeemstatus: {Status}. Path: {Path}", status, context.Request.Path);
if (await TryAnswerExpiredTokenAsync(context, admin)) return;
context.Response.StatusCode = StatusCodes.Status503ServiceUnavailable;
await context.Response.WriteAsJsonAsync(new ProblemDetails
{
@@ -122,7 +125,7 @@ public class AvailabilityMiddleware
/// without verifying the signature, which meant an unauthenticated caller could present a
/// self-made token carrying an Owner role claim and bypass the gate.
/// </remarks>
private bool IsAdminBypass(HttpContext context)
private AdminTokenResult CheckAdmin(HttpContext context)
{
var result = _adminTokenValidator.Validate(context.Request.Headers.Authorization.ToString());
@@ -135,6 +138,26 @@ public class AvailabilityMiddleware
SecurityEvents.AdminBypassRejected(_logger, context.Request.Path, result.Reason);
}
return result.IsVerifiedAdmin;
return result;
}
/// <summary>
/// A token that has merely EXPIRED is answered with 401 instead of 503. The 503 left a signed-in Owner stuck on a
/// disabled CMS: the app only refreshes its session on a 401, so it never got a fresh token and never got back
/// through the gate as an admin. An expired token reveals nothing; a missing, forged or non-admin token still gets 503.
/// </summary>
private async Task<bool> TryAnswerExpiredTokenAsync(HttpContext context, AdminTokenResult admin)
{
if (admin.Reason != BypassRejectionReason.Expired) return false;
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
await context.Response.WriteAsJsonAsync(new ProblemDetails
{
Status = StatusCodes.Status401Unauthorized,
Title = "Unauthorized",
Detail = "De sessie is verlopen. Ververs de sessie en probeer het opnieuw.",
Instance = context.Request.Path
});
return true;
}
}