Skip to content

Commit 2b4c3ed

Browse files
committed
django 5.1: support LoginRequiredMiddleware
1 parent 072e8db commit 2b4c3ed

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

mfa/admin.py

+2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
from django.urls import reverse
55

66
from .models import MFAKey
7+
from .decorators import login_not_required
78

89

10+
@login_not_required
911
def custom_login(self, request, extra_context=None):
1012
next_url = (
1113
request.GET.get(REDIRECT_FIELD_NAME)

mfa/decorators.py

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
try:
2+
from stronghold.decorators import public as stronghold_login_not_required
3+
except ImportError:
4+
def stronghold_login_not_required(view_func):
5+
return view_func
6+
7+
try:
8+
from django.contrib.auth.decorators import login_not_required
9+
except ImportError:
10+
def login_not_required(view_func):
11+
return view_func
12+
13+
114
def public(view_func):
215
view_func.mfa_public = True
316
return view_func

mfa/views.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,14 @@
1515
from django.views.generic import ListView
1616

1717
from . import settings
18+
from .decorators import login_not_required
19+
from .decorators import stronghold_login_not_required
1820
from .forms import MFAAuthForm
1921
from .forms import MFACreateForm
2022
from .mail import send_mail
2123
from .mixins import MFAFormView
2224
from .models import MFAKey
2325

24-
try:
25-
from stronghold.decorators import public as stronghold_public
26-
except ImportError:
27-
def stronghold_public(view_func):
28-
return view_func
29-
3026

3127
class LoginView(DjangoLoginView):
3228
def no_key_exists(self, form):
@@ -90,7 +86,8 @@ def form_valid(self, form):
9086
return super().form_valid(form)
9187

9288

93-
@method_decorator(stronghold_public, name='dispatch')
89+
@method_decorator(login_not_required, name='dispatch')
90+
@method_decorator(stronghold_login_not_required, name='dispatch')
9491
class MFAAuthView(MFAFormView):
9592
form_class = MFAAuthForm
9693

tests/settings.py

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from pathlib import Path
22

3+
import django
4+
35
DATABASES = {
46
'default': {
57
'ENGINE': 'django.db.backends.sqlite3',
@@ -21,6 +23,9 @@
2123
'mfa.middleware.MFAEnforceMiddleware',
2224
]
2325

26+
if django.VERSION >= (5, 1):
27+
MIDDLEWARE.append('django.contrib.auth.middleware.LoginRequiredMiddleware')
28+
2429
AUTHENTICATION_BACKENDS = [
2530
'django.contrib.auth.backends.ModelBackend',
2631
]

0 commit comments

Comments
 (0)