ApiCredential.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System.ComponentModel.DataAnnotations;
  2. using System.ComponentModel.DataAnnotations.Schema;
  3. using Domain.Entities.Developers.ValueObject;
  4. namespace Domain.Entities.Developers;
  5. /// <summary>
  6. /// OAuth2 Client Credentials (ClientID + SecretHash).
  7. /// Secret 회전을 위해 한 앱이 여러 credential 을 가질 수 있고 구 credential 은 Revoked 처리.
  8. /// </summary>
  9. public class ApiCredential
  10. {
  11. [ForeignKey(nameof(ApplicationID))]
  12. public virtual ApiApplication? Application { get; private set; }
  13. [Key]
  14. public int ID { get; private set; }
  15. public int ApplicationID { get; private set; }
  16. public string ClientID { get; private set; } = default!;
  17. public string SecretHash { get; private set; } = default!;
  18. public string SecretHint { get; private set; } = default!;
  19. public CredentialStatus Status { get; private set; } = CredentialStatus.Active;
  20. public DateTime CreatedAt { get; private set; } = DateTime.UtcNow;
  21. public DateTime? LastUsedAt { get; private set; }
  22. public DateTime? RevokedAt { get; private set; }
  23. private ApiCredential() { }
  24. public static ApiCredential Create(int applicationID, string clientID, string secretHash, string secretHint)
  25. {
  26. return new ApiCredential
  27. {
  28. ApplicationID = applicationID,
  29. ClientID = clientID,
  30. SecretHash = secretHash,
  31. SecretHint = secretHint
  32. };
  33. }
  34. public void Revoke()
  35. {
  36. Status = CredentialStatus.Revoked;
  37. RevokedAt = DateTime.UtcNow;
  38. }
  39. public void TouchLastUsed()
  40. {
  41. LastUsedAt = DateTime.UtcNow;
  42. }
  43. public bool IsActive => Status == CredentialStatus.Active && RevokedAt == null;
  44. }