| | | 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.Domain.Persistence; |
| | | 7 | | using ArturRios.Heimdall.Shared.Messages; |
| | | 8 | | using ArturRios.Heimdall.Shared.Services; |
| | | 9 | | using ArturRios.Heimdall.Shared.Time; |
| | | 10 | | using ArturRios.Mediator.Command.Interfaces; |
| | | 11 | | using ArturRios.Output; |
| | | 12 | | using FluentValidation; |
| | | 13 | | using Application = ArturRios.Heimdall.Domain.Entities.Application; |
| | | 14 | | |
| | | 15 | | namespace ArturRios.Heimdall.Command.Handlers; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Handles <see cref="IssueApplicationCredentialCommand" /> (UC-53, FR-AP-10/11/12): generates a |
| | | 19 | | /// secret, stores its hash, and returns the secret once. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <remarks> |
| | | 22 | | /// The limit of <see cref="ApplicationCredential.MaxLive" /> is checked and the row written by |
| | | 23 | | /// <see cref="IApplicationCredentialSlots" /> in one locked step; checking here and inserting |
| | | 24 | | /// through a repository would let two concurrent requests both pass. |
| | | 25 | | /// </remarks> |
| | 27 | 26 | | public class IssueApplicationCredentialCommandHandler( |
| | 27 | 27 | | IValidator<IssueApplicationCredentialCommand> validator, |
| | 27 | 28 | | IAsyncReadOnlyRepository<Application, long> applicationReader, |
| | 27 | 29 | | IApplicationCredentialSlots slots) |
| | | 30 | | : ICommandHandlerAsync<IssueApplicationCredentialCommand, IssueApplicationCredentialCommandOutput> |
| | | 31 | | { |
| | | 32 | | public async Task<DataOutput<IssueApplicationCredentialCommandOutput?>> HandleAsync( |
| | | 33 | | IssueApplicationCredentialCommand command, CancellationToken cancellationToken = default) |
| | | 34 | | { |
| | 27 | 35 | | var output = DataOutput<IssueApplicationCredentialCommandOutput?>.New; |
| | | 36 | | |
| | | 37 | | // AF-53c. |
| | 27 | 38 | | var validation = await validator.ValidateAsync(command, cancellationToken); |
| | | 39 | | |
| | 27 | 40 | | if (!validation.IsValid) |
| | | 41 | | { |
| | 4 | 42 | | return output.WithErrors(validation.Errors.Select(failure => failure.ErrorMessage)); |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | // AF-53a, then AF-53b. |
| | 25 | 46 | | var access = await ApplicationManagementAccess.ResolveAsync( |
| | 25 | 47 | | applicationReader.Query(), command.ScopeId, command.Id, command.ActingRole, command.ActingPersonId); |
| | | 48 | | |
| | 25 | 49 | | switch (access.Access) |
| | | 50 | | { |
| | | 51 | | case ApplicationAccess.NotFound: |
| | 3 | 52 | | return output.WithError(ApplicationMessages.ApplicationNotFound); |
| | | 53 | | case ApplicationAccess.Forbidden: |
| | 2 | 54 | | return output.WithError(ApplicationCredentialMessages.NotAuthorizedToManageCredentials); |
| | | 55 | | } |
| | | 56 | | |
| | 20 | 57 | | var now = DateTime.UtcNow; |
| | 20 | 58 | | var secret = ApplicationCredentialSecret.New(); |
| | 20 | 59 | | var credential = new ApplicationCredential |
| | 20 | 60 | | { |
| | 20 | 61 | | ApplicationId = access.Application!.Id, |
| | 20 | 62 | | SecretHash = ApplicationCredentialSecret.HashOf(secret), |
| | 20 | 63 | | CreatedAt = now, |
| | 20 | 64 | | ExpiresAt = command.ExpiresAt is { } expiresAt ? UtcInstant.Of(expiresAt) : null |
| | 20 | 65 | | }; |
| | | 66 | | |
| | | 67 | | // AF-53d. |
| | 20 | 68 | | if (!await slots.TryAddWithinLimitAsync(credential, ApplicationCredential.MaxLive, now)) |
| | | 69 | | { |
| | 8 | 70 | | return output.WithError(ApplicationCredentialMessages.CredentialLimitReached); |
| | | 71 | | } |
| | | 72 | | |
| | 12 | 73 | | return output |
| | 12 | 74 | | .WithData(new IssueApplicationCredentialCommandOutput |
| | 12 | 75 | | { |
| | 12 | 76 | | Id = credential.PublicId, |
| | 12 | 77 | | Secret = secret, |
| | 12 | 78 | | CreatedAt = credential.CreatedAt, |
| | 12 | 79 | | ExpiresAt = credential.ExpiresAt |
| | 12 | 80 | | }) |
| | 12 | 81 | | .WithMessage(ApplicationCredentialMessages.CredentialIssued); |
| | 27 | 82 | | } |
| | | 83 | | } |