UDT-001: Login (scaffolding + JWT RS256 end-to-end) #1
@@ -0,0 +1,6 @@
|
||||
namespace SIGCM2.Application.Abstractions;
|
||||
|
||||
public interface ICommandHandler<TCommand, TResult>
|
||||
{
|
||||
Task<TResult> Handle(TCommand command);
|
||||
}
|
||||
6
src/api/SIGCM2.Application/Abstractions/IDispatcher.cs
Normal file
6
src/api/SIGCM2.Application/Abstractions/IDispatcher.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace SIGCM2.Application.Abstractions;
|
||||
|
||||
public interface IDispatcher
|
||||
{
|
||||
Task<TResult> Send<TCommand, TResult>(TCommand command);
|
||||
}
|
||||
6
src/api/SIGCM2.Application/Abstractions/IQueryHandler.cs
Normal file
6
src/api/SIGCM2.Application/Abstractions/IQueryHandler.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace SIGCM2.Application.Abstractions;
|
||||
|
||||
public interface IQueryHandler<TQuery, TResult>
|
||||
{
|
||||
Task<TResult> Handle(TQuery query);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using SIGCM2.Domain.Entities;
|
||||
|
||||
namespace SIGCM2.Application.Abstractions.Persistence;
|
||||
|
||||
public interface IUsuarioRepository
|
||||
{
|
||||
Task<Usuario?> GetByUsernameAsync(string username);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using SIGCM2.Domain.Entities;
|
||||
|
||||
namespace SIGCM2.Application.Abstractions.Security;
|
||||
|
||||
public interface IJwtService
|
||||
{
|
||||
string GenerateAccessToken(Usuario usuario);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace SIGCM2.Application.Abstractions.Security;
|
||||
|
||||
public interface IPasswordHasher
|
||||
{
|
||||
bool Verify(string plainPassword, string hash);
|
||||
string Hash(string plainPassword);
|
||||
}
|
||||
3
src/api/SIGCM2.Application/Auth/Login/LoginCommand.cs
Normal file
3
src/api/SIGCM2.Application/Auth/Login/LoginCommand.cs
Normal file
@@ -0,0 +1,3 @@
|
||||
namespace SIGCM2.Application.Auth.Login;
|
||||
|
||||
public sealed record LoginCommand(string Username, string Password);
|
||||
54
src/api/SIGCM2.Application/Auth/Login/LoginCommandHandler.cs
Normal file
54
src/api/SIGCM2.Application/Auth/Login/LoginCommandHandler.cs
Normal 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
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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.");
|
||||
}
|
||||
}
|
||||
15
src/api/SIGCM2.Application/Auth/Login/LoginResponseDto.cs
Normal file
15
src/api/SIGCM2.Application/Auth/Login/LoginResponseDto.cs
Normal 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
|
||||
);
|
||||
20
src/api/SIGCM2.Application/DependencyInjection.cs
Normal file
20
src/api/SIGCM2.Application/DependencyInjection.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
18
src/api/SIGCM2.Application/SIGCM2.Application.csproj
Normal file
18
src/api/SIGCM2.Application/SIGCM2.Application.csproj
Normal 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>
|
||||
Reference in New Issue
Block a user