From 49e6c8a6430694f15e91e700f47e8833e29b621d Mon Sep 17 00:00:00 2001 From: Mateusz Skoczek Date: Tue, 20 Jan 2026 02:14:01 +0100 Subject: [PATCH] main project split, authpassword endpoint created --- .../AuthPassword/AuthPasswordCommand.cs | 11 +++ .../AuthPassword/AuthPasswordHandler.cs | 36 ++++++++++ .../AuthPassword/AuthPasswordResult.cs | 21 ++++++ .../Commands/Register/RegisterCommand.cs | 10 +++ .../Commands/Register/RegisterHandler.cs | 21 +++--- .../Commands/Register/RegisterResult.cs | 7 ++ .../Helpers/IPasswordHasher.cs | 2 +- .../Helpers/PasswordHashData.cs | 6 ++ .../Helpers/PasswordHasher.cs | 2 +- .../Helpers/TokenGenerator.cs | 27 ++++++++ ...ackend.Services.Authentication.Core.csproj | 19 ++++++ .../AuthPasswordRequest.cs | 8 +++ .../AuthResponse.cs | 7 ++ ...nticateResponse.cs => AuthTokenRequest.cs} | 2 +- .../AuthenticatePasswordRequest.cs | 8 --- .../AuthenticateTokenRequest.cs | 7 -- .../Configuration/AccountConfiguration.cs | 0 .../RefreshTokenConfiguration.cs | 0 .../DatabaseContext.cs | 0 .../Model/Account.cs | 0 .../Model/RefreshToken.cs | 0 ...nd.Services.Authentication.Database.csproj | 13 ++++ ...igner.Backend.Services.Authentication.slnx | 2 + .../Commands/Register/RegisterCommand.cs | 8 --- .../Commands/Register/RegisterMappers.cs | 20 ------ .../Commands/Register/RegisterResult.cs | 6 -- .../Application/Helpers/PasswordHashData.cs | 6 -- .../Program.cs | 2 +- ...ner.Backend.Services.Authentication.csproj | 5 ++ .../WebAPI/Endpoints.cs | 68 ++++++++++--------- .../WebAPI/Mappers/AuthPasswordMappers.cs | 13 ++++ .../WebAPI/Mappers/RegisterMappers.cs | 13 ++++ 32 files changed, 246 insertions(+), 104 deletions(-) create mode 100644 TimetableDesigner.Backend.Services.Authentication.Core/Commands/AuthPassword/AuthPasswordCommand.cs create mode 100644 TimetableDesigner.Backend.Services.Authentication.Core/Commands/AuthPassword/AuthPasswordHandler.cs create mode 100644 TimetableDesigner.Backend.Services.Authentication.Core/Commands/AuthPassword/AuthPasswordResult.cs create mode 100644 TimetableDesigner.Backend.Services.Authentication.Core/Commands/Register/RegisterCommand.cs rename {TimetableDesigner.Backend.Services.Authentication/Application => TimetableDesigner.Backend.Services.Authentication.Core}/Commands/Register/RegisterHandler.cs (61%) create mode 100644 TimetableDesigner.Backend.Services.Authentication.Core/Commands/Register/RegisterResult.cs rename {TimetableDesigner.Backend.Services.Authentication/Application => TimetableDesigner.Backend.Services.Authentication.Core}/Helpers/IPasswordHasher.cs (64%) create mode 100644 TimetableDesigner.Backend.Services.Authentication.Core/Helpers/PasswordHashData.cs rename {TimetableDesigner.Backend.Services.Authentication/Application => TimetableDesigner.Backend.Services.Authentication.Core}/Helpers/PasswordHasher.cs (93%) create mode 100644 TimetableDesigner.Backend.Services.Authentication.Core/Helpers/TokenGenerator.cs create mode 100644 TimetableDesigner.Backend.Services.Authentication.Core/TimetableDesigner.Backend.Services.Authentication.Core.csproj create mode 100644 TimetableDesigner.Backend.Services.Authentication.DTO.WebAPI/AuthPasswordRequest.cs create mode 100644 TimetableDesigner.Backend.Services.Authentication.DTO.WebAPI/AuthResponse.cs rename TimetableDesigner.Backend.Services.Authentication.DTO.WebAPI/{AuthenticateResponse.cs => AuthTokenRequest.cs} (84%) delete mode 100644 TimetableDesigner.Backend.Services.Authentication.DTO.WebAPI/AuthenticatePasswordRequest.cs delete mode 100644 TimetableDesigner.Backend.Services.Authentication.DTO.WebAPI/AuthenticateTokenRequest.cs rename {TimetableDesigner.Backend.Services.Authentication/Database => TimetableDesigner.Backend.Services.Authentication.Database}/Configuration/AccountConfiguration.cs (100%) rename {TimetableDesigner.Backend.Services.Authentication/Database => TimetableDesigner.Backend.Services.Authentication.Database}/Configuration/RefreshTokenConfiguration.cs (100%) rename {TimetableDesigner.Backend.Services.Authentication/Database => TimetableDesigner.Backend.Services.Authentication.Database}/DatabaseContext.cs (100%) rename {TimetableDesigner.Backend.Services.Authentication/Database => TimetableDesigner.Backend.Services.Authentication.Database}/Model/Account.cs (100%) rename {TimetableDesigner.Backend.Services.Authentication/Database => TimetableDesigner.Backend.Services.Authentication.Database}/Model/RefreshToken.cs (100%) create mode 100644 TimetableDesigner.Backend.Services.Authentication.Database/TimetableDesigner.Backend.Services.Authentication.Database.csproj delete mode 100644 TimetableDesigner.Backend.Services.Authentication/Application/Commands/Register/RegisterCommand.cs delete mode 100644 TimetableDesigner.Backend.Services.Authentication/Application/Commands/Register/RegisterMappers.cs delete mode 100644 TimetableDesigner.Backend.Services.Authentication/Application/Commands/Register/RegisterResult.cs delete mode 100644 TimetableDesigner.Backend.Services.Authentication/Application/Helpers/PasswordHashData.cs create mode 100644 TimetableDesigner.Backend.Services.Authentication/WebAPI/Mappers/AuthPasswordMappers.cs create mode 100644 TimetableDesigner.Backend.Services.Authentication/WebAPI/Mappers/RegisterMappers.cs diff --git a/TimetableDesigner.Backend.Services.Authentication.Core/Commands/AuthPassword/AuthPasswordCommand.cs b/TimetableDesigner.Backend.Services.Authentication.Core/Commands/AuthPassword/AuthPasswordCommand.cs new file mode 100644 index 0000000..9d2ee6d --- /dev/null +++ b/TimetableDesigner.Backend.Services.Authentication.Core/Commands/AuthPassword/AuthPasswordCommand.cs @@ -0,0 +1,11 @@ +using MediatR; + +namespace TimetableDesigner.Backend.Services.Authentication.Core.Commands.AuthPassword; + +public record AuthPasswordCommand +( + string Email, + string Password, + bool RememberMe +) +: IRequest; \ No newline at end of file diff --git a/TimetableDesigner.Backend.Services.Authentication.Core/Commands/AuthPassword/AuthPasswordHandler.cs b/TimetableDesigner.Backend.Services.Authentication.Core/Commands/AuthPassword/AuthPasswordHandler.cs new file mode 100644 index 0000000..9c78dbe --- /dev/null +++ b/TimetableDesigner.Backend.Services.Authentication.Core/Commands/AuthPassword/AuthPasswordHandler.cs @@ -0,0 +1,36 @@ +using MediatR; +using Microsoft.EntityFrameworkCore; +using TimetableDesigner.Backend.Services.Authentication.Core.Helpers; +using TimetableDesigner.Backend.Services.Authentication.Database; +using TimetableDesigner.Backend.Services.Authentication.Database.Model; + +namespace TimetableDesigner.Backend.Services.Authentication.Core.Commands.AuthPassword; + +public class AuthPasswordHandler : IRequestHandler +{ + private readonly DatabaseContext _databaseContext; + private readonly IPasswordHasher _passwordHasher; + + public AuthPasswordHandler(DatabaseContext databaseContext, IPasswordHasher passwordHasher) + { + _databaseContext = databaseContext; + _passwordHasher = passwordHasher; + } + + public async Task Handle(AuthPasswordCommand request, CancellationToken cancellationToken) + { + Account? account = await _databaseContext.Accounts.FirstOrDefaultAsync(x => x.Email == request.Email, cancellationToken); + if (account is null) + { + return AuthPasswordResult.Failure(); + } + + PasswordHashData hash = new PasswordHashData(account.Password, account.PasswordSalt); + if (!_passwordHasher.ValidatePassword(hash, request.Password)) + { + return AuthPasswordResult.Failure(); + } + + return null; + } +} \ No newline at end of file diff --git a/TimetableDesigner.Backend.Services.Authentication.Core/Commands/AuthPassword/AuthPasswordResult.cs b/TimetableDesigner.Backend.Services.Authentication.Core/Commands/AuthPassword/AuthPasswordResult.cs new file mode 100644 index 0000000..100338b --- /dev/null +++ b/TimetableDesigner.Backend.Services.Authentication.Core/Commands/AuthPassword/AuthPasswordResult.cs @@ -0,0 +1,21 @@ +namespace TimetableDesigner.Backend.Services.Authentication.Core.Commands.AuthPassword; + +public record AuthPasswordResult +{ + public bool IsSuccess { get; } + public string? AccessToken { get; } + public string? RefreshToken { get; } + + private AuthPasswordResult(bool isSuccess, string? accessToken, string? refreshToken) + { + IsSuccess = isSuccess; + AccessToken = accessToken; + RefreshToken = refreshToken; + } + + public static AuthPasswordResult Success(string accessToken, string refreshToken) => + new AuthPasswordResult(true, accessToken, refreshToken); + + public static AuthPasswordResult Failure() => + new AuthPasswordResult(false, null, null); +} \ No newline at end of file diff --git a/TimetableDesigner.Backend.Services.Authentication.Core/Commands/Register/RegisterCommand.cs b/TimetableDesigner.Backend.Services.Authentication.Core/Commands/Register/RegisterCommand.cs new file mode 100644 index 0000000..efd6732 --- /dev/null +++ b/TimetableDesigner.Backend.Services.Authentication.Core/Commands/Register/RegisterCommand.cs @@ -0,0 +1,10 @@ +using MediatR; + +namespace TimetableDesigner.Backend.Services.Authentication.Core.Commands.Register; + +public record RegisterCommand +( + string Email, + string Password +) +: IRequest; \ No newline at end of file diff --git a/TimetableDesigner.Backend.Services.Authentication/Application/Commands/Register/RegisterHandler.cs b/TimetableDesigner.Backend.Services.Authentication.Core/Commands/Register/RegisterHandler.cs similarity index 61% rename from TimetableDesigner.Backend.Services.Authentication/Application/Commands/Register/RegisterHandler.cs rename to TimetableDesigner.Backend.Services.Authentication.Core/Commands/Register/RegisterHandler.cs index f5cbd9f..55ae3e4 100644 --- a/TimetableDesigner.Backend.Services.Authentication/Application/Commands/Register/RegisterHandler.cs +++ b/TimetableDesigner.Backend.Services.Authentication.Core/Commands/Register/RegisterHandler.cs @@ -1,23 +1,19 @@ using MediatR; -using TimetableDesigner.Backend.Events; -using TimetableDesigner.Backend.Services.Authentication.Application.Helpers; +using TimetableDesigner.Backend.Services.Authentication.Core.Helpers; using TimetableDesigner.Backend.Services.Authentication.Database; using TimetableDesigner.Backend.Services.Authentication.Database.Model; -using TimetableDesigner.Backend.Services.Authentication.DTO.Events; -namespace TimetableDesigner.Backend.Services.Authentication.Application.Commands.Register; +namespace TimetableDesigner.Backend.Services.Authentication.Core.Commands.Register; public class RegisterHandler : IRequestHandler { private readonly DatabaseContext _databaseContext; private readonly IPasswordHasher _passwordHasher; - private readonly IEventQueuePublisher _eventQueuePublisher; - public RegisterHandler(DatabaseContext databaseContext, IPasswordHasher passwordHasher, IEventQueuePublisher eventQueuePublisher) + public RegisterHandler(DatabaseContext databaseContext, IPasswordHasher passwordHasher) { _databaseContext = databaseContext; _passwordHasher = passwordHasher; - _eventQueuePublisher = eventQueuePublisher; } public async Task Handle(RegisterCommand command, CancellationToken cancellationToken) @@ -31,12 +27,13 @@ public class RegisterHandler : IRequestHandler PasswordSalt = hash.Salt, }; await _databaseContext.Accounts.AddAsync(account, cancellationToken); + + // Change to outbox pattern + //RegisterEvent eventData = account.ToEvent(); + //await _eventQueuePublisher.PublishAsync(eventData); + await _databaseContext.SaveChangesAsync(cancellationToken); - RegisterEvent eventData = account.ToEvent(); - await _eventQueuePublisher.PublishAsync(eventData); - - RegisterResult result = account.ToResult(); - return result; + return new RegisterResult(account.Id, account.Email); } } \ No newline at end of file diff --git a/TimetableDesigner.Backend.Services.Authentication.Core/Commands/Register/RegisterResult.cs b/TimetableDesigner.Backend.Services.Authentication.Core/Commands/Register/RegisterResult.cs new file mode 100644 index 0000000..8ae739a --- /dev/null +++ b/TimetableDesigner.Backend.Services.Authentication.Core/Commands/Register/RegisterResult.cs @@ -0,0 +1,7 @@ +namespace TimetableDesigner.Backend.Services.Authentication.Core.Commands.Register; + +public record RegisterResult +( + long Id, + string Email +); \ No newline at end of file diff --git a/TimetableDesigner.Backend.Services.Authentication/Application/Helpers/IPasswordHasher.cs b/TimetableDesigner.Backend.Services.Authentication.Core/Helpers/IPasswordHasher.cs similarity index 64% rename from TimetableDesigner.Backend.Services.Authentication/Application/Helpers/IPasswordHasher.cs rename to TimetableDesigner.Backend.Services.Authentication.Core/Helpers/IPasswordHasher.cs index 4c9ecd0..e4e29e7 100644 --- a/TimetableDesigner.Backend.Services.Authentication/Application/Helpers/IPasswordHasher.cs +++ b/TimetableDesigner.Backend.Services.Authentication.Core/Helpers/IPasswordHasher.cs @@ -1,4 +1,4 @@ -namespace TimetableDesigner.Backend.Services.Authentication.Application.Helpers; +namespace TimetableDesigner.Backend.Services.Authentication.Core.Helpers; public interface IPasswordHasher { diff --git a/TimetableDesigner.Backend.Services.Authentication.Core/Helpers/PasswordHashData.cs b/TimetableDesigner.Backend.Services.Authentication.Core/Helpers/PasswordHashData.cs new file mode 100644 index 0000000..e3cc9c4 --- /dev/null +++ b/TimetableDesigner.Backend.Services.Authentication.Core/Helpers/PasswordHashData.cs @@ -0,0 +1,6 @@ +namespace TimetableDesigner.Backend.Services.Authentication.Core.Helpers; + +public record PasswordHashData( + byte[] Hash, + string Salt +); \ No newline at end of file diff --git a/TimetableDesigner.Backend.Services.Authentication/Application/Helpers/PasswordHasher.cs b/TimetableDesigner.Backend.Services.Authentication.Core/Helpers/PasswordHasher.cs similarity index 93% rename from TimetableDesigner.Backend.Services.Authentication/Application/Helpers/PasswordHasher.cs rename to TimetableDesigner.Backend.Services.Authentication.Core/Helpers/PasswordHasher.cs index 2bebfc2..7da724c 100644 --- a/TimetableDesigner.Backend.Services.Authentication/Application/Helpers/PasswordHasher.cs +++ b/TimetableDesigner.Backend.Services.Authentication.Core/Helpers/PasswordHasher.cs @@ -2,7 +2,7 @@ using System.Text; using Konscious.Security.Cryptography; -namespace TimetableDesigner.Backend.Services.Authentication.Application.Helpers; +namespace TimetableDesigner.Backend.Services.Authentication.Core.Helpers; public class PasswordHasher : IPasswordHasher { diff --git a/TimetableDesigner.Backend.Services.Authentication.Core/Helpers/TokenGenerator.cs b/TimetableDesigner.Backend.Services.Authentication.Core/Helpers/TokenGenerator.cs new file mode 100644 index 0000000..16848cb --- /dev/null +++ b/TimetableDesigner.Backend.Services.Authentication.Core/Helpers/TokenGenerator.cs @@ -0,0 +1,27 @@ +using Microsoft.Extensions.Configuration; + +namespace TimetableDesigner.Backend.Services.Authentication.Core.Helpers; + +public class TokenGenerator +{ + /* + public TokenGenerator(IConfiguration configuration, DatabaseContext databaseContext) + { + + } + + public string GenerateAccessToken(Account account) + { + + } + + public async Task GenerateRefreshTokenAsync(Account account) + { + + } + + public async Task ExtendRefreshTokenAsync() + { + + }*/ +} \ No newline at end of file diff --git a/TimetableDesigner.Backend.Services.Authentication.Core/TimetableDesigner.Backend.Services.Authentication.Core.csproj b/TimetableDesigner.Backend.Services.Authentication.Core/TimetableDesigner.Backend.Services.Authentication.Core.csproj new file mode 100644 index 0000000..a4ffee4 --- /dev/null +++ b/TimetableDesigner.Backend.Services.Authentication.Core/TimetableDesigner.Backend.Services.Authentication.Core.csproj @@ -0,0 +1,19 @@ + + + + net10.0 + enable + enable + + + + + + + + + + + + + diff --git a/TimetableDesigner.Backend.Services.Authentication.DTO.WebAPI/AuthPasswordRequest.cs b/TimetableDesigner.Backend.Services.Authentication.DTO.WebAPI/AuthPasswordRequest.cs new file mode 100644 index 0000000..e207c27 --- /dev/null +++ b/TimetableDesigner.Backend.Services.Authentication.DTO.WebAPI/AuthPasswordRequest.cs @@ -0,0 +1,8 @@ +namespace TimetableDesigner.Backend.Services.Authentication.DTO.WebAPI; + +public record AuthPasswordRequest +( + string Email, + string Password, + bool RememberMe +); \ No newline at end of file diff --git a/TimetableDesigner.Backend.Services.Authentication.DTO.WebAPI/AuthResponse.cs b/TimetableDesigner.Backend.Services.Authentication.DTO.WebAPI/AuthResponse.cs new file mode 100644 index 0000000..fb0e608 --- /dev/null +++ b/TimetableDesigner.Backend.Services.Authentication.DTO.WebAPI/AuthResponse.cs @@ -0,0 +1,7 @@ +namespace TimetableDesigner.Backend.Services.Authentication.DTO.WebAPI; + +public record AuthResponse +( + string AccessToken, + string RefreshToken +); \ No newline at end of file diff --git a/TimetableDesigner.Backend.Services.Authentication.DTO.WebAPI/AuthenticateResponse.cs b/TimetableDesigner.Backend.Services.Authentication.DTO.WebAPI/AuthTokenRequest.cs similarity index 84% rename from TimetableDesigner.Backend.Services.Authentication.DTO.WebAPI/AuthenticateResponse.cs rename to TimetableDesigner.Backend.Services.Authentication.DTO.WebAPI/AuthTokenRequest.cs index e173528..b2c5817 100644 --- a/TimetableDesigner.Backend.Services.Authentication.DTO.WebAPI/AuthenticateResponse.cs +++ b/TimetableDesigner.Backend.Services.Authentication.DTO.WebAPI/AuthTokenRequest.cs @@ -1,6 +1,6 @@ namespace TimetableDesigner.Backend.Services.Authentication.DTO.WebAPI; -public class AuthenticateResponse +public class AuthTokenRequest { public string AccessToken { get; set; } = null!; public string RefreshToken { get; set; } = null!; diff --git a/TimetableDesigner.Backend.Services.Authentication.DTO.WebAPI/AuthenticatePasswordRequest.cs b/TimetableDesigner.Backend.Services.Authentication.DTO.WebAPI/AuthenticatePasswordRequest.cs deleted file mode 100644 index 7759faa..0000000 --- a/TimetableDesigner.Backend.Services.Authentication.DTO.WebAPI/AuthenticatePasswordRequest.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace TimetableDesigner.Backend.Services.Authentication.DTO.WebAPI; - -public class AuthenticatePasswordRequest -{ - public string Email { get; set; } = null!; - public string Password { get; set; } = null!; - public bool RememberMe { get; set; } -} \ No newline at end of file diff --git a/TimetableDesigner.Backend.Services.Authentication.DTO.WebAPI/AuthenticateTokenRequest.cs b/TimetableDesigner.Backend.Services.Authentication.DTO.WebAPI/AuthenticateTokenRequest.cs deleted file mode 100644 index daaaaed..0000000 --- a/TimetableDesigner.Backend.Services.Authentication.DTO.WebAPI/AuthenticateTokenRequest.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace TimetableDesigner.Backend.Services.Authentication.DTO.WebAPI; - -public class AuthenticateTokenRequest -{ - public string AccessToken { get; set; } = null!; - public string RefreshToken { get; set; } = null!; -} \ No newline at end of file diff --git a/TimetableDesigner.Backend.Services.Authentication/Database/Configuration/AccountConfiguration.cs b/TimetableDesigner.Backend.Services.Authentication.Database/Configuration/AccountConfiguration.cs similarity index 100% rename from TimetableDesigner.Backend.Services.Authentication/Database/Configuration/AccountConfiguration.cs rename to TimetableDesigner.Backend.Services.Authentication.Database/Configuration/AccountConfiguration.cs diff --git a/TimetableDesigner.Backend.Services.Authentication/Database/Configuration/RefreshTokenConfiguration.cs b/TimetableDesigner.Backend.Services.Authentication.Database/Configuration/RefreshTokenConfiguration.cs similarity index 100% rename from TimetableDesigner.Backend.Services.Authentication/Database/Configuration/RefreshTokenConfiguration.cs rename to TimetableDesigner.Backend.Services.Authentication.Database/Configuration/RefreshTokenConfiguration.cs diff --git a/TimetableDesigner.Backend.Services.Authentication/Database/DatabaseContext.cs b/TimetableDesigner.Backend.Services.Authentication.Database/DatabaseContext.cs similarity index 100% rename from TimetableDesigner.Backend.Services.Authentication/Database/DatabaseContext.cs rename to TimetableDesigner.Backend.Services.Authentication.Database/DatabaseContext.cs diff --git a/TimetableDesigner.Backend.Services.Authentication/Database/Model/Account.cs b/TimetableDesigner.Backend.Services.Authentication.Database/Model/Account.cs similarity index 100% rename from TimetableDesigner.Backend.Services.Authentication/Database/Model/Account.cs rename to TimetableDesigner.Backend.Services.Authentication.Database/Model/Account.cs diff --git a/TimetableDesigner.Backend.Services.Authentication/Database/Model/RefreshToken.cs b/TimetableDesigner.Backend.Services.Authentication.Database/Model/RefreshToken.cs similarity index 100% rename from TimetableDesigner.Backend.Services.Authentication/Database/Model/RefreshToken.cs rename to TimetableDesigner.Backend.Services.Authentication.Database/Model/RefreshToken.cs diff --git a/TimetableDesigner.Backend.Services.Authentication.Database/TimetableDesigner.Backend.Services.Authentication.Database.csproj b/TimetableDesigner.Backend.Services.Authentication.Database/TimetableDesigner.Backend.Services.Authentication.Database.csproj new file mode 100644 index 0000000..8d23535 --- /dev/null +++ b/TimetableDesigner.Backend.Services.Authentication.Database/TimetableDesigner.Backend.Services.Authentication.Database.csproj @@ -0,0 +1,13 @@ + + + + net10.0 + enable + enable + + + + + + + diff --git a/TimetableDesigner.Backend.Services.Authentication.slnx b/TimetableDesigner.Backend.Services.Authentication.slnx index cd3af9e..855f193 100644 --- a/TimetableDesigner.Backend.Services.Authentication.slnx +++ b/TimetableDesigner.Backend.Services.Authentication.slnx @@ -1,4 +1,6 @@ + + diff --git a/TimetableDesigner.Backend.Services.Authentication/Application/Commands/Register/RegisterCommand.cs b/TimetableDesigner.Backend.Services.Authentication/Application/Commands/Register/RegisterCommand.cs deleted file mode 100644 index 4354ba3..0000000 --- a/TimetableDesigner.Backend.Services.Authentication/Application/Commands/Register/RegisterCommand.cs +++ /dev/null @@ -1,8 +0,0 @@ -using MediatR; - -namespace TimetableDesigner.Backend.Services.Authentication.Application.Commands.Register; - -public record RegisterCommand( - string Email, - string Password -) : IRequest; \ No newline at end of file diff --git a/TimetableDesigner.Backend.Services.Authentication/Application/Commands/Register/RegisterMappers.cs b/TimetableDesigner.Backend.Services.Authentication/Application/Commands/Register/RegisterMappers.cs deleted file mode 100644 index 5fc87f7..0000000 --- a/TimetableDesigner.Backend.Services.Authentication/Application/Commands/Register/RegisterMappers.cs +++ /dev/null @@ -1,20 +0,0 @@ -using TimetableDesigner.Backend.Services.Authentication.Database.Model; -using TimetableDesigner.Backend.Services.Authentication.DTO.Events; -using TimetableDesigner.Backend.Services.Authentication.DTO.WebAPI; - -namespace TimetableDesigner.Backend.Services.Authentication.Application.Commands.Register; - -public static class RegisterMappers -{ - public static RegisterCommand ToCommand(this RegisterRequest request) => - new RegisterCommand(request.Email, request.Password); - - public static RegisterResult ToResult(this Account account) => - new RegisterResult(account.Id, account.Email); - - public static RegisterResponse ToResponse(this RegisterResult result) => - new RegisterResponse(result.Id, result.Email); - - public static RegisterEvent ToEvent(this Account account) => - new RegisterEvent(account.Id, account.Email); -} \ No newline at end of file diff --git a/TimetableDesigner.Backend.Services.Authentication/Application/Commands/Register/RegisterResult.cs b/TimetableDesigner.Backend.Services.Authentication/Application/Commands/Register/RegisterResult.cs deleted file mode 100644 index 411b422..0000000 --- a/TimetableDesigner.Backend.Services.Authentication/Application/Commands/Register/RegisterResult.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace TimetableDesigner.Backend.Services.Authentication.Application.Commands.Register; - -public record RegisterResult( - long Id, - string Email -); \ No newline at end of file diff --git a/TimetableDesigner.Backend.Services.Authentication/Application/Helpers/PasswordHashData.cs b/TimetableDesigner.Backend.Services.Authentication/Application/Helpers/PasswordHashData.cs deleted file mode 100644 index fb97d35..0000000 --- a/TimetableDesigner.Backend.Services.Authentication/Application/Helpers/PasswordHashData.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace TimetableDesigner.Backend.Services.Authentication.Application.Helpers; - -public record PasswordHashData( - byte[] Hash, - string Salt -); \ No newline at end of file diff --git a/TimetableDesigner.Backend.Services.Authentication/Program.cs b/TimetableDesigner.Backend.Services.Authentication/Program.cs index 1274bb9..f972cc9 100644 --- a/TimetableDesigner.Backend.Services.Authentication/Program.cs +++ b/TimetableDesigner.Backend.Services.Authentication/Program.cs @@ -6,7 +6,7 @@ using TimetableDesigner.Backend.Events.Extensions.AspNetCore.OpenApi; using TimetableDesigner.Backend.Events.Providers.RabbitMQ; using TimetableDesigner.Backend.Services.Authentication.API; using TimetableDesigner.Backend.Services.Authentication.API.Validators; -using TimetableDesigner.Backend.Services.Authentication.Application.Helpers; +using TimetableDesigner.Backend.Services.Authentication.Core.Helpers; using TimetableDesigner.Backend.Services.Authentication.Database; using TimetableDesigner.Backend.Services.Authentication.Events; using TimetableDesigner.Backend.Services.Authentication.WebAPI; diff --git a/TimetableDesigner.Backend.Services.Authentication/TimetableDesigner.Backend.Services.Authentication.csproj b/TimetableDesigner.Backend.Services.Authentication/TimetableDesigner.Backend.Services.Authentication.csproj index 51d4d15..b89be32 100644 --- a/TimetableDesigner.Backend.Services.Authentication/TimetableDesigner.Backend.Services.Authentication.csproj +++ b/TimetableDesigner.Backend.Services.Authentication/TimetableDesigner.Backend.Services.Authentication.csproj @@ -23,8 +23,13 @@ + + + + + diff --git a/TimetableDesigner.Backend.Services.Authentication/WebAPI/Endpoints.cs b/TimetableDesigner.Backend.Services.Authentication/WebAPI/Endpoints.cs index 7cf031b..65ee374 100644 --- a/TimetableDesigner.Backend.Services.Authentication/WebAPI/Endpoints.cs +++ b/TimetableDesigner.Backend.Services.Authentication/WebAPI/Endpoints.cs @@ -2,62 +2,64 @@ using FluentValidation.Results; using MediatR; using Microsoft.AspNetCore.Http.HttpResults; -using TimetableDesigner.Backend.Events; -using TimetableDesigner.Backend.Services.Authentication.Application.Commands.Register; +using TimetableDesigner.Backend.Services.Authentication.Core.Commands.AuthPassword; +using TimetableDesigner.Backend.Services.Authentication.Core.Commands.Register; using TimetableDesigner.Backend.Services.Authentication.DTO.WebAPI; +using TimetableDesigner.Backend.Services.Authentication.WebAPI.Mappers; namespace TimetableDesigner.Backend.Services.Authentication.WebAPI; public static class Endpoints { - public static IEndpointRouteBuilder MapWebAPIEndpoints(this IEndpointRouteBuilder app) + public static IEndpointRouteBuilder MapWebAPIEndpoints(this IEndpointRouteBuilder builder) { - app.MapPost("/register", Register) - .AllowAnonymous() - .Produces(201) - .Produces(400) - .Produces(500) - .WithName("Register"); - app.MapPost("/authenticate_password", AuthenticatePassword) - .WithName("AuthenticatePassword"); - app.MapPost("/authenticate_token", AuthenticateToken) - .WithName("AuthenticateToken"); - app.MapPost("/test", Test) - .AllowAnonymous() - .WithName("Test"); + builder.MapPost("/register", Register) + .AllowAnonymous() + .Produces(201) + .Produces(400) + .Produces(500) + .WithName("Register"); + builder.MapPost("/auth/password", AuthPassword) + .AllowAnonymous() + .Produces() + .Produces(401) + .Produces(500) + .WithName("AuthPassword"); + builder.MapPost("/auth/token", AuthToken) + .WithName("AuthToken"); - return app; + return builder; } - private static async Task, ValidationProblem>> Register(IMediator mediator, IValidator validator, RegisterRequest request, CancellationToken cancellationToken) + private static async Task, ValidationProblem, InternalServerError>> Register(IMediator mediator, IValidator validator, RegisterRequest request, CancellationToken cancellationToken) { - ValidationResult validationResult = await validator.ValidateAsync(request); - if (!validationResult.IsValid) + ValidationResult validationResult = await validator.ValidateAsync(request, cancellationToken); + if (!validationResult.IsValid) { return TypedResults.ValidationProblem(validationResult.ToDictionary()); } - RegisterCommand registerCommand = request.ToCommand(); - RegisterResult result = await mediator.Send(registerCommand, cancellationToken); - RegisterResponse response = result.ToResponse(); + RegisterResult result = await mediator.Send(request.ToCommand(), cancellationToken); + RegisterResponse response = result.ToResponse(); return TypedResults.Created($"accounts/{response.Id}", response); } - public static async Task, ProblemHttpResult>> AuthenticatePassword(AuthenticatePasswordRequest request) + private static async Task, UnauthorizedHttpResult, InternalServerError>> AuthPassword(IMediator mediator, AuthPasswordRequest request, CancellationToken cancellationToken) { - return null; + AuthPasswordResult result = await mediator.Send(request.ToCommand(), cancellationToken); + + if (!result.IsSuccess) + { + return TypedResults.Unauthorized(); + } + + AuthResponse response = result.ToResponse(); + return TypedResults.Ok(response); } - public static async Task, ProblemHttpResult>> AuthenticateToken(AuthenticateTokenRequest request) + public static async Task, ProblemHttpResult>> AuthToken(AuthTokenRequest request) { return null; } - - public static async Task> Test(IEventQueuePublisher publisher) - { - await publisher.PublishAsync(new RegisterRequest("aaaa", "bbbb", "ccccc")); - - return TypedResults.Ok(); - } } \ No newline at end of file diff --git a/TimetableDesigner.Backend.Services.Authentication/WebAPI/Mappers/AuthPasswordMappers.cs b/TimetableDesigner.Backend.Services.Authentication/WebAPI/Mappers/AuthPasswordMappers.cs new file mode 100644 index 0000000..37ab907 --- /dev/null +++ b/TimetableDesigner.Backend.Services.Authentication/WebAPI/Mappers/AuthPasswordMappers.cs @@ -0,0 +1,13 @@ +using TimetableDesigner.Backend.Services.Authentication.Core.Commands.AuthPassword; +using TimetableDesigner.Backend.Services.Authentication.DTO.WebAPI; + +namespace TimetableDesigner.Backend.Services.Authentication.WebAPI.Mappers; + +public static class AuthPasswordMappers +{ + public static AuthPasswordCommand ToCommand(this AuthPasswordRequest request) => + new AuthPasswordCommand(request.Email, request.Password, request.RememberMe); + + public static AuthResponse ToResponse(this AuthPasswordResult result) => + new AuthResponse(result.AccessToken!, result.RefreshToken!); +} \ No newline at end of file diff --git a/TimetableDesigner.Backend.Services.Authentication/WebAPI/Mappers/RegisterMappers.cs b/TimetableDesigner.Backend.Services.Authentication/WebAPI/Mappers/RegisterMappers.cs new file mode 100644 index 0000000..3bb1a16 --- /dev/null +++ b/TimetableDesigner.Backend.Services.Authentication/WebAPI/Mappers/RegisterMappers.cs @@ -0,0 +1,13 @@ +using TimetableDesigner.Backend.Services.Authentication.Core.Commands.Register; +using TimetableDesigner.Backend.Services.Authentication.DTO.WebAPI; + +namespace TimetableDesigner.Backend.Services.Authentication.WebAPI.Mappers; + +public static class RegisterMappers +{ + public static RegisterCommand ToCommand(this RegisterRequest request) => + new RegisterCommand(request.Email, request.Password); + + public static RegisterResponse ToResponse(this RegisterResult result) => + new RegisterResponse(result.Id, result.Email); +} \ No newline at end of file