| | | 1 | | using ArturRios.Data.Relational.Core.Interfaces; |
| | | 2 | | using ArturRios.Heimdall.Command.Input; |
| | | 3 | | using ArturRios.Heimdall.Command.Output; |
| | | 4 | | using ArturRios.Heimdall.Command.Services; |
| | | 5 | | using ArturRios.Heimdall.Domain.Entities; |
| | | 6 | | using ArturRios.Heimdall.Shared.Messages; |
| | | 7 | | using ArturRios.Mediator.Command.Interfaces; |
| | | 8 | | using ArturRios.Output; |
| | | 9 | | using FluentValidation; |
| | | 10 | | using Microsoft.EntityFrameworkCore; |
| | | 11 | | |
| | | 12 | | namespace ArturRios.Heimdall.Command.Handlers; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Handles <see cref="RegisterCommand" /> (UC-47): validates, confirms the scope is open, hashes the |
| | | 16 | | /// password, queues the request and answers. Whether the address is free — and so whether a person |
| | | 17 | | /// is created or the existing owner is told of the attempt — is decided afterwards, in |
| | | 18 | | /// <see cref="RegistrationProcessor" />. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <remarks> |
| | | 21 | | /// <para> |
| | | 22 | | /// Every valid request against an open scope gets the same answer in the same time (AF-47b). The |
| | | 23 | | /// scope check is on the request path because it says nothing about any address: a closed |
| | | 24 | | /// scope is closed for everyone. |
| | | 25 | | /// </para> |
| | | 26 | | /// <para> |
| | | 27 | | /// The password is hashed here rather than in the processor so that the queue never holds a |
| | | 28 | | /// plaintext secret, and so that the request path costs one hash for every caller. |
| | | 29 | | /// </para> |
| | | 30 | | /// </remarks> |
| | 32 | 31 | | public class RegisterCommandHandler( |
| | 32 | 32 | | IValidator<RegisterCommand> validator, |
| | 32 | 33 | | IAsyncReadOnlyRepository<Scope, long> scopeReader, |
| | 32 | 34 | | IRegistrationQueue registrationQueue) |
| | | 35 | | : ICommandHandlerAsync<RegisterCommand, RegisterCommandOutput> |
| | | 36 | | { |
| | | 37 | | public async Task<DataOutput<RegisterCommandOutput?>> HandleAsync( |
| | | 38 | | RegisterCommand command, CancellationToken cancellationToken = default) |
| | | 39 | | { |
| | 32 | 40 | | var output = DataOutput<RegisterCommandOutput?>.New; |
| | | 41 | | |
| | | 42 | | // AF-47c: malformed input, refused on the request alone. |
| | 32 | 43 | | var validation = await validator.ValidateAsync(command, cancellationToken); |
| | | 44 | | |
| | 32 | 45 | | if (!validation.IsValid) |
| | | 46 | | { |
| | 4 | 47 | | return output.WithErrors(validation.Errors.Select(failure => failure.ErrorMessage)); |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | // AF-47a: missing, deleted and closed scopes answer alike. |
| | 30 | 51 | | var scopeOpen = await scopeReader.Query().AnyAsync( |
| | 30 | 52 | | scope => scope.PublicId == command.ScopeId && !scope.IsDeleted && scope.SelfRegistrationEnabled, |
| | 30 | 53 | | cancellationToken); |
| | | 54 | | |
| | 30 | 55 | | if (!scopeOpen) |
| | | 56 | | { |
| | 6 | 57 | | return output.WithError(AuthMessages.RegistrationUnavailable); |
| | | 58 | | } |
| | | 59 | | |
| | 24 | 60 | | var (passwordHash, salt) = await PasswordHashGate.Shared.EncodeWithRandomSaltAsync(command.Password); |
| | | 61 | | |
| | | 62 | | // UC-47 steps 4 onward, deferred. The result is deliberately ignored: a dropped request must |
| | | 63 | | // answer exactly as a queued one does. |
| | 24 | 64 | | registrationQueue.TryEnqueue(new RegistrationRequest( |
| | 24 | 65 | | command.ScopeId!.Value, command.Name.Trim(), command.Email.Trim(), passwordHash, salt)); |
| | | 66 | | |
| | 24 | 67 | | return output |
| | 24 | 68 | | .WithData(new RegisterCommandOutput()) |
| | 24 | 69 | | .WithMessage(AuthMessages.RegistrationRequested); |
| | 32 | 70 | | } |
| | | 71 | | } |