| | | 1 | | using System.ComponentModel.DataAnnotations; |
| | | 2 | | using System.Linq.Expressions; |
| | | 3 | | using ArturRios.Data.Relational.Core.Entities; |
| | | 4 | | |
| | | 5 | | namespace ArturRios.Heimdall.Domain.Entities; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// A secret an <see cref="Application" /> exchanges for an application token (UC-53, UC-56, |
| | | 9 | | /// FR-AP-10). Only the secret's SHA-256 is stored; the secret itself is shown once, in UC-53's |
| | | 10 | | /// response, and never again. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// <para> |
| | | 14 | | /// An application holds at most <see cref="MaxLive" /> live credentials, so a new one can be |
| | | 15 | | /// deployed before the old one is revoked (FR-AP-11). Each is revoked on its own (UC-55) and |
| | | 16 | | /// may carry an expiry. |
| | | 17 | | /// </para> |
| | | 18 | | /// <para> |
| | | 19 | | /// No personal data: the row names an application, a hash and four instants. Logically |
| | | 20 | | /// deleting the application or its scope leaves the rows alone — UC-56 and UC-57 refuse a |
| | | 21 | | /// deleted application or scope, so its credentials are inert — and hard-deleting the |
| | | 22 | | /// application removes them by cascade. Revoked and expired rows are purged after the |
| | | 23 | | /// single-use-token grace by the token retention pass. |
| | | 24 | | /// </para> |
| | | 25 | | /// </remarks> |
| | | 26 | | public class ApplicationCredential : Entity<long> |
| | | 27 | | { |
| | | 28 | | /// <summary>The most live credentials one application may hold at once (FR-AP-11).</summary> |
| | | 29 | | public const int MaxLive = 2; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// External identifier — the credential's reference in UC-54's listing, UC-55's route, the |
| | | 33 | | /// token's <c>credentialId</c> claim and the audit trail. The internal |
| | | 34 | | /// <see cref="Entity{TKey}.Id" /> is never exposed. |
| | | 35 | | /// </summary> |
| | 250 | 36 | | public Guid PublicId { get; set; } = Guid.NewGuid(); |
| | | 37 | | |
| | | 38 | | /// <summary>Foreign key to the owning <see cref="Application" /> (internal Id). Required.</summary> |
| | 144 | 39 | | public long ApplicationId { get; set; } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// SHA-256 of the secret, as lowercase hex (<c>ApplicationCredentialSecret.HashOf</c>). The |
| | | 43 | | /// secret itself is never stored: whoever could read this table could otherwise authenticate |
| | | 44 | | /// as every application (Threat Model TH-33). |
| | | 45 | | /// </summary> |
| | | 46 | | [MaxLength(64)] |
| | 258 | 47 | | public string SecretHash { get; set; } = string.Empty; |
| | | 48 | | |
| | | 49 | | /// <summary>When the credential was issued (UC-53).</summary> |
| | 98 | 50 | | public DateTime CreatedAt { get; set; } |
| | | 51 | | |
| | | 52 | | /// <summary>When the credential stops being accepted, or <c>null</c> for never.</summary> |
| | 153 | 53 | | public DateTime? ExpiresAt { get; set; } |
| | | 54 | | |
| | | 55 | | /// <summary>When the credential was revoked (UC-55), or <c>null</c> while it has not been.</summary> |
| | 136 | 56 | | public DateTime? RevokedAt { get; set; } |
| | | 57 | | |
| | | 58 | | /// <summary>When a token was last issued from this credential (UC-56), or <c>null</c> if never.</summary> |
| | 21 | 59 | | public DateTime? LastUsedAt { get; set; } |
| | | 60 | | |
| | | 61 | | // Navigation properties |
| | | 62 | | |
| | | 63 | | /// <summary>The application this credential authenticates.</summary> |
| | 163 | 64 | | public Application Application { get; set; } = null!; |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// The one definition of a live credential — not revoked, and either without expiry or |
| | | 68 | | /// expiring after <paramref name="now" /> — as an expression, so a query can filter on it in |
| | | 69 | | /// the database. |
| | | 70 | | /// </summary> |
| | | 71 | | public static Expression<Func<ApplicationCredential, bool>> LiveAt(DateTime now) => |
| | 92 | 72 | | credential => credential.RevokedAt == null && |
| | 92 | 73 | | (credential.ExpiresAt == null || credential.ExpiresAt > now); |
| | | 74 | | |
| | | 75 | | /// <summary><see cref="LiveAt" />, evaluated for this instance.</summary> |
| | 14 | 76 | | public bool IsLiveAt(DateTime now) => LiveAt(now).Compile()(this); |
| | | 77 | | } |