File size: 966 Bytes
667aacd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""
BackupCode model — recovery codes for MFA.

Maps to AGENT.md DBML: `backup_codes` table (Lines 237-243).
"""
from django.conf import settings
from django.db import models

from core.models import BaseModel


class BackupCode(BaseModel):
    """
    One-time backup codes for MFA recovery.

    Codes are stored as hashes. Once used, `used_at` is set and the code
    cannot be reused.
    """

    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        related_name="backup_codes",
    )
    code_hash = models.CharField(max_length=255)
    used_at = models.DateTimeField(null=True, blank=True)

    class Meta:
        db_table = "backup_codes"
        ordering = ["created_at"]

    def __str__(self) -> str:
        status = "used" if self.used_at else "available"
        return f"BackupCode for {self.user_id} [{status}]"

    @property
    def is_used(self) -> bool:
        return self.used_at is not None