Beveiliging: gedeactiveerde of verwijderde gebruiker wordt direct geweigerd

Het JWT werd alleen op handtekening en looptijd gecontroleerd, dus een gedeactiveerde
gebruiker kon tot het verlopen van het token lezen en schrijven. Nu controleert de
bearer-validatie per request of het account bestaat en actief is, en weigert de refresh
een inactieve gebruiker.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-08 14:36:09 +01:00
co-authored by Claude Sonnet 5.5
parent 6c33a7ce36
commit 33a7500648
5 changed files with 274 additions and 2 deletions
@@ -82,7 +82,7 @@ public static class ServiceCollectionExtensions
})
.AddJwtBearer(options =>
{
options.TokenValidationParameters = tokenValidationParameters;
options.UseCmsTokenValidation(tokenValidationParameters);
});
// 5. Authorization
@@ -0,0 +1,61 @@
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.JsonWebTokens;
using SlpModularCms.Core.Data;
namespace SlpModularCms.Core.Identity.Authorization;
/// <summary>
/// Rejects a structurally valid access token when its account is no longer allowed to act:
/// the user was deactivated or deleted after the token was issued.
/// </summary>
/// <remarks>
/// A signed JWT stays valid until it expires, so without this check a deactivated or deleted user
/// could keep reading and writing for up to <c>JwtSettings:ExpiryMinutes</c>. The check runs for
/// every authenticated request (reads and writes alike) and deliberately has no cache: a cache
/// would bring back a window in which a deactivated user is still served.
/// </remarks>
public static class ActiveUserTokenValidation
{
/// <summary>
/// The one place the bearer scheme is configured: signature/lifetime parameters plus the
/// active-account check. Hosts and tests both go through this, so the check cannot be dropped
/// from one without the other noticing.
/// </summary>
public static JwtBearerOptions UseCmsTokenValidation(
this JwtBearerOptions options,
Microsoft.IdentityModel.Tokens.TokenValidationParameters parameters)
{
options.TokenValidationParameters = parameters;
options.Events = new JwtBearerEvents { OnTokenValidated = ValidateAsync };
return options;
}
public static async Task ValidateAsync(TokenValidatedContext context)
{
var principal = context.Principal;
var rawId = principal?.FindFirstValue(ClaimTypes.NameIdentifier)
?? principal?.FindFirstValue(JwtRegisteredClaimNames.Sub);
if (!Guid.TryParse(rawId, out var userId))
{
context.Fail("Token has no valid subject.");
return;
}
var db = context.HttpContext.RequestServices.GetRequiredService<ApplicationDbContext>();
var isActive = await db.Users
.AsNoTracking()
.Where(u => u.Id == userId)
.Select(u => (bool?)u.IsActive)
.FirstOrDefaultAsync(context.HttpContext.RequestAborted);
if (isActive != true)
{
// Same 401 whether the user is gone or deactivated: which one is not the caller's business.
context.Fail("Account is not active.");
}
}
}
@@ -71,6 +71,14 @@ public class AuthService : IAuthService
throw new UnauthorizedException("Mogelijk token misbruik gedetecteerd. Alle sessies zijn beëindigd.");
}
if (!savedRefreshToken.User.IsActive)
{
// A deactivated account must not get a new access token from a still-valid refresh token.
savedRefreshToken.IsRevoked = true;
await _context.SaveChangesAsync();
throw new UnauthorizedException("Ongeldig refresh token.");
}
savedRefreshToken.IsUsed = true;
await _context.SaveChangesAsync();