| | | 1 | | using ArturRios.Data.Relational.Core.Interfaces; |
| | | 2 | | using ArturRios.Heimdall.Domain.Entities; |
| | | 3 | | using ArturRios.Heimdall.Query.Input; |
| | | 4 | | using ArturRios.Heimdall.Query.Output; |
| | | 5 | | using ArturRios.Heimdall.Shared.Messages; |
| | | 6 | | using ArturRios.Heimdall.Shared.Security; |
| | | 7 | | using ArturRios.Mediator.Query.Interfaces; |
| | | 8 | | using ArturRios.Output; |
| | | 9 | | using Microsoft.EntityFrameworkCore; |
| | | 10 | | |
| | | 11 | | namespace ArturRios.Heimdall.Query.Handlers; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Handles <see cref="CheckApplicationTokenQuery" /> (UC-57, FR-AP-15/16): validates the token |
| | | 15 | | /// offline, then confirms against the database that its application and scope are live and |
| | | 16 | | /// agree with it, and that the credential it was issued from is still live. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <remarks> |
| | | 19 | | /// This is the documented revocation policy: revoking a credential (UC-55), letting it expire, or |
| | | 20 | | /// deleting the application or its scope takes effect here at once, for tokens already issued as |
| | | 21 | | /// much as for new ones. A consumer re-checks before every sensitive release. Every refusal is |
| | | 22 | | /// <see cref="ApplicationTokenMessages.TokenNotAccepted" />, with no detail. |
| | | 23 | | /// </remarks> |
| | 19 | 24 | | public class CheckApplicationTokenQueryHandler( |
| | 19 | 25 | | IApplicationTokenValidator validator, |
| | 19 | 26 | | IAsyncReadOnlyRepository<ApplicationCredential, long> credentialReader) |
| | | 27 | | : IQueryHandlerAsync<CheckApplicationTokenQuery, ApplicationTokenCheckOutput> |
| | | 28 | | { |
| | | 29 | | public async Task<DataOutput<ApplicationTokenCheckOutput?>> HandleAsync( |
| | | 30 | | CheckApplicationTokenQuery query, CancellationToken cancellationToken = default) |
| | | 31 | | { |
| | 19 | 32 | | var output = DataOutput<ApplicationTokenCheckOutput?>.New; |
| | | 33 | | |
| | 19 | 34 | | var principal = await validator.ValidateAsync(query.Token); |
| | | 35 | | |
| | 19 | 36 | | if (principal is null) |
| | | 37 | | { |
| | 5 | 38 | | return output.WithError(ApplicationTokenMessages.TokenNotAccepted); |
| | | 39 | | } |
| | | 40 | | |
| | 14 | 41 | | var now = DateTime.UtcNow; |
| | 14 | 42 | | var check = await credentialReader.Query() |
| | 14 | 43 | | .Where(ApplicationCredential.LiveAt(now)) |
| | 14 | 44 | | .Where(x => x.PublicId == principal.CredentialId && |
| | 14 | 45 | | x.Application.PublicId == principal.ApplicationId && |
| | 14 | 46 | | !x.Application.IsDeleted && |
| | 14 | 47 | | x.Application.Scope.PublicId == principal.ScopeId && |
| | 14 | 48 | | !x.Application.Scope.IsDeleted) |
| | 14 | 49 | | .Select(x => new ApplicationTokenCheckOutput |
| | 14 | 50 | | { |
| | 14 | 51 | | ApplicationId = x.Application.PublicId, |
| | 14 | 52 | | Name = x.Application.Name, |
| | 14 | 53 | | ScopeId = x.Application.Scope.PublicId, |
| | 14 | 54 | | CredentialId = x.PublicId, |
| | 14 | 55 | | ExpiresAt = principal.ExpiresAt |
| | 14 | 56 | | }) |
| | 14 | 57 | | .FirstOrDefaultAsync(cancellationToken); |
| | | 58 | | |
| | 14 | 59 | | return check is null |
| | 14 | 60 | | ? output.WithError(ApplicationTokenMessages.TokenNotAccepted) |
| | 14 | 61 | | : output.WithData(check).WithMessage(ApplicationTokenMessages.TokenAccepted); |
| | 19 | 62 | | } |
| | | 63 | | } |