UDT-001: Login (scaffolding + JWT RS256 end-to-end) #1

Merged
dmolinari merged 14 commits from feature/UDT-001 into main 2026-04-14 14:44:28 +00:00
12 changed files with 168 additions and 0 deletions
Showing only changes of commit 8c26cd3ac5 - Show all commits

View File

@@ -0,0 +1,6 @@
namespace SIGCM2.Application.Abstractions;
public interface ICommandHandler<TCommand, TResult>
{
Task<TResult> Handle(TCommand command);
}

View File

@@ -0,0 +1,6 @@
namespace SIGCM2.Application.Abstractions;
public interface IDispatcher
{
Task<TResult> Send<TCommand, TResult>(TCommand command);
}

View File

@@ -0,0 +1,6 @@
namespace SIGCM2.Application.Abstractions;
public interface IQueryHandler<TQuery, TResult>
{
Task<TResult> Handle(TQuery query);
}

View File

@@ -0,0 +1,8 @@
using SIGCM2.Domain.Entities;
namespace SIGCM2.Application.Abstractions.Persistence;
public interface IUsuarioRepository
{
Task<Usuario?> GetByUsernameAsync(string username);
}

View File

@@ -0,0 +1,8 @@
using SIGCM2.Domain.Entities;
namespace SIGCM2.Application.Abstractions.Security;
public interface IJwtService
{
string GenerateAccessToken(Usuario usuario);
}

View File

@@ -0,0 +1,7 @@
namespace SIGCM2.Application.Abstractions.Security;
public interface IPasswordHasher
{
bool Verify(string plainPassword, string hash);
string Hash(string plainPassword);
}

View File

@@ -0,0 +1,3 @@
namespace SIGCM2.Application.Auth.Login;
public sealed record LoginCommand(string Username, string Password);

View File

@@ -0,0 +1,54 @@
using System.Text.Json;
using SIGCM2.Application.Abstractions;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Abstractions.Security;
using SIGCM2.Domain.Exceptions;
namespace SIGCM2.Application.Auth.Login;
public sealed class LoginCommandHandler : ICommandHandler<LoginCommand, LoginResponseDto>
{
private readonly IUsuarioRepository _repository;
private readonly IPasswordHasher _hasher;
private readonly IJwtService _jwtService;
public LoginCommandHandler(
IUsuarioRepository repository,
IPasswordHasher hasher,
IJwtService jwtService)
{
_repository = repository;
_hasher = hasher;
_jwtService = jwtService;
}
public async Task<LoginResponseDto> Handle(LoginCommand command)
{
var usuario = await _repository.GetByUsernameAsync(command.Username);
// Deliberately vague — never reveal which check failed
if (usuario is null || !usuario.Activo)
throw new InvalidCredentialsException();
if (!_hasher.Verify(command.Password, usuario.PasswordHash))
throw new InvalidCredentialsException();
var accessToken = _jwtService.GenerateAccessToken(usuario);
var refreshToken = Guid.NewGuid().ToString("N"); // opaque, not persisted in UDT-001
var permisos = JsonSerializer.Deserialize<string[]>(usuario.PermisosJson)
?? Array.Empty<string>();
return new LoginResponseDto(
AccessToken: accessToken,
RefreshToken: refreshToken,
ExpiresIn: 3600,
Usuario: new UsuarioDto(
Id: usuario.Id,
Nombre: $"{usuario.Nombre} {usuario.Apellido}".Trim(),
Rol: usuario.Rol,
Permisos: permisos
)
);
}
}

View File

@@ -0,0 +1,17 @@
using FluentValidation;
namespace SIGCM2.Application.Auth.Login;
public sealed class LoginCommandValidator : AbstractValidator<LoginCommand>
{
public LoginCommandValidator()
{
RuleFor(x => x.Username)
.NotEmpty()
.WithMessage("El nombre de usuario es requerido.");
RuleFor(x => x.Password)
.NotEmpty()
.WithMessage("La contraseña es requerida.");
}
}

View File

@@ -0,0 +1,15 @@
namespace SIGCM2.Application.Auth.Login;
public sealed record LoginResponseDto(
string AccessToken,
string RefreshToken,
int ExpiresIn,
UsuarioDto Usuario
);
public sealed record UsuarioDto(
int Id,
string Nombre,
string Rol,
string[] Permisos
);

View File

@@ -0,0 +1,20 @@
using FluentValidation;
using Microsoft.Extensions.DependencyInjection;
using SIGCM2.Application.Abstractions;
using SIGCM2.Application.Auth.Login;
namespace SIGCM2.Application;
public static class DependencyInjection
{
public static IServiceCollection AddApplication(this IServiceCollection services)
{
// Register command handlers
services.AddScoped<ICommandHandler<LoginCommand, LoginResponseDto>, LoginCommandHandler>();
// Register FluentValidation validators from this assembly
services.AddValidatorsFromAssemblyContaining<LoginCommandValidator>();
return services;
}
}

View File

@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>SIGCM2.Application</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentValidation.AspNetCore" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SIGCM2.Domain\SIGCM2.Domain.csproj" />
</ItemGroup>
</Project>