Skip to content

Commit

Permalink
django 5.1: support LoginRequiredMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
xi committed Jun 18, 2024
1 parent 072e8db commit 7be4d30
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
2 changes: 2 additions & 0 deletions mfa/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
from django.contrib.auth.views import redirect_to_login
from django.urls import reverse

from .decorators import login_not_required
from .models import MFAKey


@login_not_required
def custom_login(self, request, extra_context=None):
next_url = (
request.GET.get(REDIRECT_FIELD_NAME)
Expand Down
13 changes: 13 additions & 0 deletions mfa/decorators.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
try:
from stronghold.decorators import public as stronghold_login_not_required
except ImportError:
def stronghold_login_not_required(view_func):
return view_func

try:
from django.contrib.auth.decorators import login_not_required
except ImportError:
def login_not_required(view_func):
return view_func


def public(view_func):
view_func.mfa_public = True
return view_func
11 changes: 4 additions & 7 deletions mfa/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,14 @@
from django.views.generic import ListView

from . import settings
from .decorators import login_not_required
from .decorators import stronghold_login_not_required
from .forms import MFAAuthForm
from .forms import MFACreateForm
from .mail import send_mail
from .mixins import MFAFormView
from .models import MFAKey

try:
from stronghold.decorators import public as stronghold_public
except ImportError:
def stronghold_public(view_func):
return view_func


class LoginView(DjangoLoginView):
def no_key_exists(self, form):
Expand Down Expand Up @@ -90,7 +86,8 @@ def form_valid(self, form):
return super().form_valid(form)


@method_decorator(stronghold_public, name='dispatch')
@method_decorator(login_not_required, name='dispatch')
@method_decorator(stronghold_login_not_required, name='dispatch')
class MFAAuthView(MFAFormView):
form_class = MFAAuthForm

Expand Down
5 changes: 5 additions & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from pathlib import Path

import django

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
Expand All @@ -21,6 +23,9 @@
'mfa.middleware.MFAEnforceMiddleware',
]

if django.VERSION >= (5, 1):
MIDDLEWARE.append('django.contrib.auth.middleware.LoginRequiredMiddleware')

AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend',
]
Expand Down

0 comments on commit 7be4d30

Please sign in to comment.