| | | 1 | | using ArturRios.Heimdall.Domain.Entities; |
| | | 2 | | using Microsoft.EntityFrameworkCore; |
| | | 3 | | using Microsoft.EntityFrameworkCore.Metadata.Builders; |
| | | 4 | | |
| | | 5 | | namespace ArturRios.Heimdall.Data.EntityMaps; |
| | | 6 | | |
| | | 7 | | internal static class TwoFactorAuthDbMap |
| | | 8 | | { |
| | | 9 | | public static void Configure(this EntityTypeBuilder<TwoFactorAuth> twoFactorAuth) |
| | | 10 | | { |
| | 3 | 11 | | twoFactorAuth.ToTable("two_factor_auth"); |
| | | 12 | | |
| | | 13 | | // Internal bigint Id only; a person's own configuration is reached through their |
| | | 14 | | // authenticated identity, never addressed by ID in a path (§4.0). |
| | 3 | 15 | | twoFactorAuth.HasKey(x => x.Id); |
| | | 16 | | |
| | 3 | 17 | | twoFactorAuth.Property(x => x.AppEnabled).HasDefaultValue(false); |
| | 3 | 18 | | twoFactorAuth.Property(x => x.EmailEnabled).HasDefaultValue(false); |
| | | 19 | | |
| | 3 | 20 | | twoFactorAuth.Property(x => x.TotpSecretEncrypted); |
| | | 21 | | |
| | 3 | 22 | | twoFactorAuth.Property(x => x.IsActive).HasDefaultValue(false); |
| | | 23 | | |
| | 3 | 24 | | twoFactorAuth.Property(x => x.EmailCodeReissueCount).HasDefaultValue(0); |
| | | 25 | | |
| | | 26 | | // The outstanding challenge (FR-2F-10); null when none is outstanding. |
| | 3 | 27 | | twoFactorAuth.Property(x => x.ChallengeId); |
| | | 28 | | |
| | | 29 | | // Guesses at that challenge with an app code or a recovery code; reset with it. |
| | 3 | 30 | | twoFactorAuth.Property(x => x.ChallengeAttempts).HasDefaultValue(0); |
| | | 31 | | |
| | 3 | 32 | | twoFactorAuth.Property(x => x.CreatedAt).HasDefaultValueSql("now()"); |
| | 3 | 33 | | twoFactorAuth.Property(x => x.UpdatedAt).HasDefaultValueSql("now()"); |
| | | 34 | | |
| | | 35 | | // At most one configuration per person; hard-deleting the person cascades to it (UC-10). |
| | 3 | 36 | | twoFactorAuth.HasIndex(x => x.PersonId).IsUnique(); |
| | 3 | 37 | | twoFactorAuth.HasOne(x => x.Person) |
| | 3 | 38 | | .WithOne(p => p.TwoFactorAuth) |
| | 3 | 39 | | .HasForeignKey<TwoFactorAuth>(x => x.PersonId) |
| | 3 | 40 | | .IsRequired() |
| | 3 | 41 | | .OnDelete(DeleteBehavior.Cascade); |
| | 3 | 42 | | } |
| | | 43 | | } |