Skip to content

Commit

Permalink
Added tag for checking if user has 2FA enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismaddalena committed Aug 21, 2023
1 parent cd98df1 commit 652b14f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ghostwriter/home/templatetags/custom_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

# 3rd Party Libraries
from bs4 import BeautifulSoup
from allauth_2fa.utils import user_has_valid_totp_device

# Ghostwriter Libraries
from ghostwriter.api.utils import verify_access, verify_finding_access, verify_user_is_privileged
Expand Down Expand Up @@ -124,3 +125,9 @@ def can_create_finding(user):
def is_privileged(user):
"""Check if the user has the permission to create a finding."""
return verify_user_is_privileged(user)


@register.filter
def has_2fa(user):
"""Check if the user has a valid TOTP method configured."""
return user_has_valid_totp_device(user)
9 changes: 9 additions & 0 deletions ghostwriter/home/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
from django.test.utils import override_settings
from django.urls import reverse

# 3rd Party Libraries
from django_otp.plugins.otp_static.models import StaticToken

# Ghostwriter Libraries
from ghostwriter.factories import (
GroupFactory,
Expand Down Expand Up @@ -138,6 +141,12 @@ def test_tags(self):
self.user.save()
self.assertTrue(custom_tags.can_create_finding(self.user))

self.assertFalse(custom_tags.has_2fa(self.user))
self.user.totpdevice_set.create()
static_model = self.user.staticdevice_set.create()
static_model.token_set.create(token=StaticToken.random_token())
self.assertTrue(custom_tags.has_2fa(self.user))


class DashboardTests(TestCase):
"""Collection of tests for :view:`home.dashboard`."""
Expand Down

0 comments on commit 652b14f

Please sign in to comment.