From 670eac49b6b7c1b0bd6ba95e5a94e05cf3e948c2 Mon Sep 17 00:00:00 2001 From: jiangweidong Date: Wed, 6 Mar 2024 14:49:20 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96oauth2=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E6=9C=AA=E6=BF=80=E6=B4=BB=E7=94=A8=E6=88=B7=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E4=BC=9A=E9=87=8D=E5=A4=8D=E8=B7=B3=E8=BD=AC=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/authentication/backends/oauth2/views.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/apps/authentication/backends/oauth2/views.py b/apps/authentication/backends/oauth2/views.py index e3b919fff..88f82dfa3 100644 --- a/apps/authentication/backends/oauth2/views.py +++ b/apps/authentication/backends/oauth2/views.py @@ -4,10 +4,13 @@ from django.contrib import auth from django.http import HttpResponseRedirect from django.urls import reverse from django.utils.http import urlencode +from django.utils.translation import gettext_lazy as _ from authentication.utils import build_absolute_uri -from common.utils import get_logger +from authentication.views.mixins import FlashMessageMixin from authentication.mixins import authenticate +from common.utils import get_logger + logger = get_logger(__file__) @@ -39,7 +42,7 @@ class OAuth2AuthRequestView(View): return HttpResponseRedirect(redirect_url) -class OAuth2AuthCallbackView(View): +class OAuth2AuthCallbackView(View, FlashMessageMixin): http_method_names = ['get', ] def get(self, request): @@ -51,6 +54,11 @@ class OAuth2AuthCallbackView(View): if 'code' in callback_params: logger.debug(log_prompt.format('Process authenticate')) user = authenticate(code=callback_params['code'], request=request) + + if err_msg := getattr(request, 'error_message', ''): + login_url = reverse('authentication:login') + '?admin=1' + return self.get_failed_response(login_url, title=_('Authentication failed'), msg=err_msg) + if user and user.is_valid: logger.debug(log_prompt.format('Login: {}'.format(user))) auth.login(self.request, user)