簡體   English   中英

Django 的 Python Social Auth 引發 Authforbidden 異常

[英]Python Social Auth for Django raises Authforbidden exception

我將 Django 1.10 與 python 2.7 和 social-auth-app-django (1.2.0) 一起使用。 它是 Python Social Auth 庫的一部分。

我希望僅登錄我公司的域 ID,因此我使用了庫中的此設置。

SOCIAL_AUTH_GOOGLE_OAUTH2_WHITELISTED_DOMAINS=['mycompany.in']

現在,如果您嘗試按預期使用任何其他域登錄,則會引發錯誤。

我的目標是捕獲此異常並向用戶顯示自定義頁面。 但是對於我的生活,我無法做到這一點。

如果我將 Debug 設置為 False 它會將用戶重定向到我的

LOGIN_ERROR_URL='/'

頁面,但無法將我的自定義消息傳遞給用戶

這是我的setting.py的一部分

DEBUG = False

    MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'social_django.middleware.SocialAuthExceptionMiddleware',
]

#social auth
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY= "9******6.apps.googleusercontent.com"
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET="W*****x"
SOCIAL_AUTH_GOOGLE_OAUTH2_WHITELISTED_DOMAINS=['mycompany.in']
LOGIN_URL = 'login'
LOGIN_REDIRECT_URL = 'upload_file'
LOGOUT_URL = 'logout'
LOGIN_ERROR_URL='logout

在我看來,我有這段代碼來處理異常

from social_django.middleware import SocialAuthExceptionMiddleware
from social_core.exceptions import AuthForbidden


    class SocialAuthExceptionMiddleware(SocialAuthExceptionMiddleware):
      def process_exception(self, request, exception):
        print "exception occured"
        if hasattr(social_exceptions, 'AuthForbidden'):
          print "hello two"
          return HttpResponse("I'm a exception %s" % exception)
        else:
          print "other exception"
          raise exception

我什至嘗試過

def process_template_response(self):
    print "response"'

def get_redirect_uri(request, exception):
    print "URL"

但無濟於事。

我已經關注了這些鏈接python-social-auth AuthCanceled exception

http://python-social-auth-docs.readthedocs.io/en/latest/configuration/django.html#exceptions-middleware

這是調試設置為 False 時的輸出:

“AuthForbidden at /app/oauth/complete/google-oauth2/ 您的憑據不被允許”

  1. 需要在 settings.py 中設置SOCIAL_AUTH_LOGIN_ERROR_URL的值
  2. 擴展 SocialAuthExceptionMiddleware 類 & 需要覆蓋“get_message”方法
  3. 處理錯誤 URL 向用戶顯示消息。

例如

中間件.py

from social_django.middleware import SocialAuthExceptionMiddleware

class CustomSocialAuthExceptionMiddleware(SocialAuthExceptionMiddleware):
    def get_message(self, request, exception):
       default_msg = super(CustomSocialAuthExceptionMiddleware).get_message(request, exception) # in case of display default message
       return "Custom messages text write here."

設置.py

SOCIAL_AUTH_LOGIN_ERROR_URL = '/error_page/'
MIDDLEWARE = [
'...',
'path.to.custom.middleware.CustomSocialAuthExceptionMiddleware',
]

網址.py

from django.conf.urls import url

from views import ErrorPage

urlpatterns = [
    url(r'^error_page/$', ErrorPage.as_view(), name="error-page"),
]

視圖.py

from django.views.generic.base import TemplateView

class ErrorPage(TemplateView):
    template_name = 'error.html'

錯誤.html(模板)

....
   <body>
     {% if messages %}
        <ul class="messages">
           {% for message in messages %}
               <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }} </li>
        {% endfor %}
        </ul>
    {% endif %}
   </body>
....

如果您使用的是 django 消息框架。 在不使用 django 消息框架的情況下,中間件將消息添加到 GET 參數中,您可以在錯誤頁面上顯示該消息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM