From 5ff4a16fc6b27ee15f2e259326cd648a2ae1b8ae Mon Sep 17 00:00:00 2001 From: jiangweidong <1053570670@qq.com> Date: Wed, 13 Nov 2024 17:04:55 +0800 Subject: [PATCH] fix: Solve the problem that mfa cannot jump to the original URL --- apps/authentication/views/login.py | 1 + apps/authentication/views/mfa.py | 18 ++++++++++++------ apps/authentication/views/utils.py | 8 -------- 3 files changed, 13 insertions(+), 14 deletions(-) delete mode 100644 apps/authentication/views/utils.py diff --git a/apps/authentication/views/login.py b/apps/authentication/views/login.py index 4dd1236a4..d540d96e6 100644 --- a/apps/authentication/views/login.py +++ b/apps/authentication/views/login.py @@ -311,6 +311,7 @@ class UserLoginView(mixins.AuthMixin, UserLoginContextMixin, FormView): class UserLoginGuardView(mixins.AuthMixin, RedirectView): + query_string = True redirect_field_name = 'next' login_url = reverse_lazy('authentication:login') login_mfa_url = reverse_lazy('authentication:login-mfa') diff --git a/apps/authentication/views/mfa.py b/apps/authentication/views/mfa.py index c297a3261..bf1ada999 100644 --- a/apps/authentication/views/mfa.py +++ b/apps/authentication/views/mfa.py @@ -3,11 +3,10 @@ from __future__ import unicode_literals from django.views.generic.edit import FormView -from django.shortcuts import redirect +from django.shortcuts import redirect, reverse from common.utils import get_logger from .. import forms, errors, mixins -from .utils import redirect_to_guard_view logger = get_logger(__name__) __all__ = ['UserLoginMFAView'] @@ -22,7 +21,7 @@ class UserLoginMFAView(mixins.AuthMixin, FormView): try: user = self.get_user_from_session() except errors.SessionEmptyError: - return redirect_to_guard_view('session_empty') + return self.redirect_to_guard_view('session_empty') try: self._check_if_no_active_mfa(user) @@ -31,6 +30,13 @@ class UserLoginMFAView(mixins.AuthMixin, FormView): return super().get(*args, **kwargs) + def redirect_to_guard_view(self, comment=''): + guard_url = reverse('authentication:login-guard') + '?_=' + comment + args = self.request.META.get('QUERY_STRING', '') + if args: + guard_url = "%s&%s" % (guard_url, args) + return redirect(guard_url) + def form_valid(self, form): from users.utils import MFABlockUtils code = form.cleaned_data.get('code') @@ -40,17 +46,17 @@ class UserLoginMFAView(mixins.AuthMixin, FormView): self._do_check_user_mfa(code, mfa_type) user, ip = self.get_user_from_session(), self.get_request_ip() MFABlockUtils(user.username, ip).clean_failed_count() - return redirect_to_guard_view('mfa_ok') + return self.redirect_to_guard_view('mfa_ok') except (errors.MFAFailedError, errors.BlockMFAError) as e: form.add_error('code', e.msg) return super().form_invalid(form) except errors.SessionEmptyError: - return redirect_to_guard_view('session_empty') + return self.redirect_to_guard_view('session_empty') except Exception as e: logger.error(e) import traceback traceback.print_exc() - return redirect_to_guard_view('unexpect') + return self.redirect_to_guard_view('unexpect') def get_context_data(self, **kwargs): user = self.get_user_from_session() diff --git a/apps/authentication/views/utils.py b/apps/authentication/views/utils.py deleted file mode 100644 index 63a1d76c6..000000000 --- a/apps/authentication/views/utils.py +++ /dev/null @@ -1,8 +0,0 @@ -# -*- coding: utf-8 -*- -# -from django.shortcuts import reverse, redirect - - -def redirect_to_guard_view(comment=''): - continue_url = reverse('authentication:login-guard') + '?_=' + comment - return redirect(continue_url)