diff --git a/mfa/admin.py b/mfa/admin.py index 7e9b473..4d15b2f 100644 --- a/mfa/admin.py +++ b/mfa/admin.py @@ -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) diff --git a/mfa/decorators.py b/mfa/decorators.py index 4977225..14d289f 100644 --- a/mfa/decorators.py +++ b/mfa/decorators.py @@ -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 diff --git a/mfa/views.py b/mfa/views.py index e4178f7..1864b86 100644 --- a/mfa/views.py +++ b/mfa/views.py @@ -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): @@ -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 diff --git a/tests/settings.py b/tests/settings.py index f938211..8c72aad 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -1,5 +1,7 @@ from pathlib import Path +import django + DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', @@ -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', ]