| | | 1 | | using ArturRios.Data.Relational.Core.Interfaces; |
| | | 2 | | using ArturRios.Heimdall.Command.Input; |
| | | 3 | | using ArturRios.Heimdall.Command.Output; |
| | | 4 | | using ArturRios.Heimdall.Domain.Entities; |
| | | 5 | | using ArturRios.Heimdall.Domain.Persistence; |
| | | 6 | | using ArturRios.Heimdall.Shared.Messages; |
| | | 7 | | using ArturRios.Heimdall.Shared.Services; |
| | | 8 | | using ArturRios.Mediator.Command.Interfaces; |
| | | 9 | | using ArturRios.Output; |
| | | 10 | | using Microsoft.EntityFrameworkCore; |
| | | 11 | | using Application = ArturRios.Heimdall.Domain.Entities.Application; |
| | | 12 | | |
| | | 13 | | namespace ArturRios.Heimdall.Command.Handlers; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Handles <see cref="RevokeApplicationCredentialCommand" /> (UC-55, FR-AP-12): stamps |
| | | 17 | | /// <c>RevokedAt</c>. From then on UC-56 refuses the secret and UC-57 refuses every token already |
| | | 18 | | /// issued from it — revocation takes effect at the next live re-check, not at token expiry. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <remarks> |
| | | 21 | | /// The stamp is one conditional column (<see cref="IAtomicWrites.TryRevokeCredentialAsync" />), not |
| | | 22 | | /// a whole-row save: of two revocations that both read the credential live, only the first writes, |
| | | 23 | | /// and the second answers AF-55d rather than overwriting the first one's time. |
| | | 24 | | /// </remarks> |
| | 17 | 25 | | public class RevokeApplicationCredentialCommandHandler( |
| | 17 | 26 | | IAsyncReadOnlyRepository<Application, long> applicationReader, |
| | 17 | 27 | | IAsyncReadOnlyRepository<ApplicationCredential, long> credentialReader, |
| | 17 | 28 | | IAtomicWrites atomicWrites) |
| | | 29 | | : ICommandHandlerAsync<RevokeApplicationCredentialCommand, RevokeApplicationCredentialCommandOutput> |
| | | 30 | | { |
| | | 31 | | public async Task<DataOutput<RevokeApplicationCredentialCommandOutput?>> HandleAsync( |
| | | 32 | | RevokeApplicationCredentialCommand command, CancellationToken cancellationToken = default) |
| | | 33 | | { |
| | 17 | 34 | | var output = DataOutput<RevokeApplicationCredentialCommandOutput?>.New; |
| | | 35 | | |
| | | 36 | | // AF-55a, then AF-55b. |
| | 17 | 37 | | var access = await ApplicationManagementAccess.ResolveAsync( |
| | 17 | 38 | | applicationReader.Query(), command.ScopeId, command.Id, command.ActingRole, command.ActingPersonId); |
| | | 39 | | |
| | 17 | 40 | | switch (access.Access) |
| | | 41 | | { |
| | | 42 | | case ApplicationAccess.NotFound: |
| | 2 | 43 | | return output.WithError(ApplicationMessages.ApplicationNotFound); |
| | | 44 | | case ApplicationAccess.Forbidden: |
| | 2 | 45 | | return output.WithError(ApplicationCredentialMessages.NotAuthorizedToManageCredentials); |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | // AF-55c: the credential must belong to this application; one of another is not the |
| | | 49 | | // resource this route addresses. |
| | 13 | 50 | | var applicationId = access.Application!.Id; |
| | 13 | 51 | | var credential = await credentialReader.Query() |
| | 13 | 52 | | .FirstOrDefaultAsync(x => x.PublicId == command.CredentialId && x.ApplicationId == applicationId, |
| | 13 | 53 | | cancellationToken); |
| | | 54 | | |
| | 13 | 55 | | if (credential is null) |
| | | 56 | | { |
| | 3 | 57 | | return output.WithError(ApplicationCredentialMessages.CredentialNotFound); |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | // AF-55d: the first revocation's time stands — decided by the write, not by what was read, |
| | | 61 | | // so a revocation committed after the read above is not overwritten. |
| | 10 | 62 | | var revokedAt = DateTime.UtcNow; |
| | | 63 | | |
| | 10 | 64 | | if (!await atomicWrites.TryRevokeCredentialAsync(credential, revokedAt)) |
| | | 65 | | { |
| | 4 | 66 | | return output.WithError(ApplicationCredentialMessages.CredentialAlreadyRevoked); |
| | | 67 | | } |
| | | 68 | | |
| | 6 | 69 | | return output |
| | 6 | 70 | | .WithData(new RevokeApplicationCredentialCommandOutput |
| | 6 | 71 | | { |
| | 6 | 72 | | Id = credential.PublicId, |
| | 6 | 73 | | RevokedAt = revokedAt |
| | 6 | 74 | | }) |
| | 6 | 75 | | .WithMessage(ApplicationCredentialMessages.CredentialRevoked); |
| | 17 | 76 | | } |
| | | 77 | | } |