feat(audit): security events en Auth + authorization handlers (UDT-010 B9)
Instruments auth pipeline with ISecurityEventLogger per #REQ-AUTH-SEC:
LoginCommandHandler:
- login success → action=login result=success actorUserId=user.Id
- login failure disaggregated internally (client still sees 401 unified):
user_not_found / user_inactive / invalid_password
— attempts captured with attemptedUsername + FailureReason
LogoutCommandHandler:
- action=logout result=success actorUserId=cmd.UsuarioId
RefreshCommandHandler:
- refresh.issue success on successful rotation
- refresh.reuse_detected failure when revoked token is presented (chain
revoke already happens; we add the security event with metadata.familyId)
- refresh.issue failure for: token_expired / sub_mismatch / user_not_found /
user_inactive
PermissionAuthorizationHandler:
- permission.denied failure on require-permission rejection, with metadata
{ permissionRequired, endpoint, method }. ActorUserId from JWT sub.
DI: ISecurityEventLogger was already registered by B6 (AddInfrastructure).
Test updates: 4 test classes now inject ISecurityEventLogger mock:
- LoginCommandHandlerTests, LogoutCommandHandlerTests, RefreshCommandHandlerTests
- PermissionAuthorizationHandlerTests (Api.Tests)
Suite: 378/378 Application.Tests + 141/141 Api.Tests = 519/519 passing.
Refs: sdd/udt-010-auditoria-trazabilidad/{spec#REQ-SEC-2/3/4/5 #REQ-AUTH-SEC,
design, tasks#B9}
This commit is contained in:
@@ -3,6 +3,7 @@ using NSubstitute;
|
||||
using SIGCM2.Application.Abstractions;
|
||||
using SIGCM2.Application.Abstractions.Persistence;
|
||||
using SIGCM2.Application.Abstractions.Security;
|
||||
using SIGCM2.Application.Audit;
|
||||
using SIGCM2.Application.Auth;
|
||||
using SIGCM2.Application.Auth.Login;
|
||||
using SIGCM2.Domain.Entities;
|
||||
@@ -20,6 +21,7 @@ public class LoginCommandHandlerTests
|
||||
private readonly IRefreshTokenGenerator _refreshGenerator = Substitute.For<IRefreshTokenGenerator>();
|
||||
private readonly IClientContext _clientCtx = Substitute.For<IClientContext>();
|
||||
private readonly IRolPermisoRepository _rolPermisoRepo = Substitute.For<IRolPermisoRepository>();
|
||||
private readonly ISecurityEventLogger _security = Substitute.For<ISecurityEventLogger>();
|
||||
private readonly ILogger<LoginCommandHandler> _logger = Substitute.For<ILogger<LoginCommandHandler>>();
|
||||
private readonly AuthOptions _authOptions = new() { AccessTokenMinutes = 60, RefreshTokenDays = 7 };
|
||||
private readonly LoginCommandHandler _handler;
|
||||
@@ -42,7 +44,7 @@ public class LoginCommandHandlerTests
|
||||
_handler = new LoginCommandHandler(
|
||||
_repository, _hasher, _jwtService,
|
||||
_refreshRepo, _refreshGenerator, _clientCtx, _authOptions,
|
||||
_rolPermisoRepo, _logger);
|
||||
_rolPermisoRepo, _security, _logger);
|
||||
}
|
||||
|
||||
// Scenario: valid credentials → returns token response with usuario populated
|
||||
|
||||
Reference in New Issue
Block a user