79 lines
1.9 KiB
C#
79 lines
1.9 KiB
C#
using System;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
using System.Text.Json;
|
|
using DysonNetwork.Common.Models;
|
|
using DysonNetwork.Common.Models.Auth;
|
|
using NodaTime;
|
|
|
|
namespace DysonNetwork.Pass.Features.Auth.Models;
|
|
|
|
public class AccountAuthFactor : ModelBase
|
|
{
|
|
[Required]
|
|
public Guid AccountId { get; set; }
|
|
|
|
[ForeignKey(nameof(AccountId))]
|
|
public virtual Account Account { get; set; } = null!;
|
|
|
|
[Required]
|
|
public AuthFactorType FactorType { get; set; }
|
|
|
|
[Required]
|
|
[MaxLength(100)]
|
|
public string Name { get; set; } = string.Empty;
|
|
|
|
[MaxLength(500)]
|
|
public string? Description { get; set; }
|
|
|
|
[Required]
|
|
public string Secret { get; set; } = string.Empty;
|
|
|
|
[Required]
|
|
public bool IsDefault { get; set; }
|
|
|
|
[Required]
|
|
public bool IsBackup { get; set; }
|
|
|
|
[Required]
|
|
public bool IsEnabled { get; set; } = true;
|
|
|
|
public Instant? LastUsedAt { get; set; }
|
|
|
|
public Instant? EnabledAt { get; set; }
|
|
|
|
public Instant? DisabledAt { get; set; }
|
|
|
|
[Column(TypeName = "jsonb")]
|
|
public JsonDocument? Metadata { get; set; }
|
|
|
|
// Navigation property for related AuthSessions
|
|
public virtual ICollection<AuthSession> Sessions { get; set; } = new List<AuthSession>();
|
|
|
|
public void UpdateMetadata(Action<JsonDocument> updateAction)
|
|
{
|
|
if (Metadata == null)
|
|
{
|
|
Metadata = JsonSerializer.SerializeToDocument(new { });
|
|
}
|
|
|
|
updateAction(Metadata);
|
|
}
|
|
|
|
public void MarkAsUsed()
|
|
{
|
|
LastUsedAt = SystemClock.Instance.GetCurrentInstant();
|
|
}
|
|
|
|
public void Enable()
|
|
{
|
|
EnabledAt = SystemClock.Instance.GetCurrentInstant();
|
|
DisabledAt = null;
|
|
}
|
|
|
|
public void Disable()
|
|
{
|
|
DisabledAt = SystemClock.Instance.GetCurrentInstant();
|
|
}
|
|
}
|