From b495f7aef57c569e51686b2c88431959ca009885 Mon Sep 17 00:00:00 2001 From: Tobias Bengfort Date: Tue, 18 Jun 2024 19:26:12 +0200 Subject: [PATCH] django 5.1: support LoginRequiredMiddleware --- mfa/views.py | 7 +++++++ tests/settings.py | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/mfa/views.py b/mfa/views.py index e4178f7..7ead137 100644 --- a/mfa/views.py +++ b/mfa/views.py @@ -27,6 +27,12 @@ def stronghold_public(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 + class LoginView(DjangoLoginView): def no_key_exists(self, form): @@ -90,6 +96,7 @@ def form_valid(self, form): return super().form_valid(form) +@method_decorator(login_not_required, name='dispatch') @method_decorator(stronghold_public, 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', ]