Skip to content

Commit

Permalink
chore: MFA Method debugging / support improvements (#4989)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewelwell authored Jan 17, 2025
1 parent 2e79ff4 commit fe1934b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
35 changes: 33 additions & 2 deletions api/custom_auth/mfa/trench/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,36 @@


@admin.register(MFAMethod)
class MFAMethodAdmin(admin.ModelAdmin):
pass
class MFAMethodAdmin(admin.ModelAdmin): # pragma: no cover
list_display = (
"user_email",
"name",
"is_active",
"is_primary",
"created_at",
"updated_at",
)
search_fields = ("user__email", "name")
list_filter = ("is_active", "is_primary")

list_select_related = ("user",)

fields = (
"user",
"name",
"created_at",
"updated_at",
"is_active",
"is_primary",
)

readonly_fields = (
"user",
"name",
"created_at",
"updated_at",
"is_primary",
)

def user_email(self, instance: MFAMethod) -> str:
return instance.user.email
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.2.17 on 2025-01-10 17:54

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("trench", "0003_auto_20190213_2330"),
]

operations = [
migrations.AddField(
model_name="mfamethod",
name="created_at",
field=models.DateTimeField(default=None, null=True),
),
migrations.AddField(
model_name="mfamethod",
name="updated_at",
field=models.DateTimeField(default=None, null=True),
),
]
17 changes: 15 additions & 2 deletions api/custom_auth/mfa/trench/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
CASCADE,
BooleanField,
CharField,
DateTimeField,
ForeignKey,
Manager,
Model,
QuerySet,
TextField,
)
from django.utils import timezone
from django_lifecycle import BEFORE_CREATE, BEFORE_SAVE, LifecycleModel, hook

from custom_auth.mfa.trench.exceptions import MFAMethodDoesNotExistError

Expand All @@ -35,7 +37,7 @@ def primary_exists(self, user_id: Any) -> bool:
return self.filter(user_id=user_id, is_primary=True).exists()


class MFAMethod(Model):
class MFAMethod(LifecycleModel):
_BACKUP_CODES_DELIMITER = ","

user = ForeignKey(
Expand All @@ -50,6 +52,9 @@ class MFAMethod(Model):
is_active = BooleanField("is active", default=False)
_backup_codes = TextField("backup codes", blank=True)

created_at = DateTimeField(null=True, default=None)
updated_at = DateTimeField(null=True, default=None)

class Meta:
verbose_name = "MFA Method"
verbose_name_plural = "MFA Methods"
Expand All @@ -59,3 +64,11 @@ class Meta:
@property
def backup_codes(self) -> Iterable[str]:
return self._backup_codes.split(self._BACKUP_CODES_DELIMITER)

@hook(BEFORE_CREATE)
def set_created_at(self):
self.created_at = timezone.now()

@hook(BEFORE_SAVE)
def set_updated_at(self):
self.updated_at = timezone.now()

0 comments on commit fe1934b

Please sign in to comment.