[英]Python Social Auth for Django raises Authforbidden exception
I am using Django 1.10 with python 2.7 and social-auth-app-django (1.2.0).我将 Django 1.10 与 python 2.7 和 social-auth-app-django (1.2.0) 一起使用。 It is part of the Python Social Auth library.它是 Python Social Auth 库的一部分。
I wish to restrict login to only the domain ID of my company I've therefore used this setting from the library.我希望仅登录我公司的域 ID,因此我使用了库中的此设置。
SOCIAL_AUTH_GOOGLE_OAUTH2_WHITELISTED_DOMAINS=['mycompany.in']
Now if you try to login with any other domain as expected it throws an error.现在,如果您尝试按预期使用任何其他域登录,则会引发错误。
My goal is to catch this exception and show a custom page to the user.我的目标是捕获此异常并向用户显示自定义页面。 But for the life of me I am unable to do so.但是对于我的生活,我无法做到这一点。
If I set Debug to False it redirects the user to my如果我将 Debug 设置为 False 它会将用户重定向到我的
LOGIN_ERROR_URL='/'
page but am unable to pass my custom message to the user页面,但无法将我的自定义消息传递给用户
This is part of my setting.py这是我的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
In my view I've this code to handle the exception在我看来,我有这段代码来处理异常
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
I've even tried with我什至尝试过
def process_template_response(self):
print "response"'
def get_redirect_uri(request, exception):
print "URL"
But to no avail.但无济于事。
I've followed these link python-social-auth AuthCanceled exception我已经关注了这些链接python-social-auth AuthCanceled exception
and和
http://python-social-auth-docs.readthedocs.io/en/latest/configuration/django.html#exceptions-middleware http://python-social-auth-docs.readthedocs.io/en/latest/configuration/django.html#exceptions-middleware
This is the output when debug is set to False:这是调试设置为 False 时的输出:
"AuthForbidden at /app/oauth/complete/google-oauth2/ Your credentials aren't allowed" “AuthForbidden at /app/oauth/complete/google-oauth2/ 您的凭据不被允许”
SOCIAL_AUTH_LOGIN_ERROR_URL
in settings.py需要在 settings.py 中设置SOCIAL_AUTH_LOGIN_ERROR_URL
的值For example例如
Middleware.py中间件.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."
settings.py设置.py
SOCIAL_AUTH_LOGIN_ERROR_URL = '/error_page/'
MIDDLEWARE = [
'...',
'path.to.custom.middleware.CustomSocialAuthExceptionMiddleware',
]
URL.py网址.py
from django.conf.urls import url
from views import ErrorPage
urlpatterns = [
url(r'^error_page/$', ErrorPage.as_view(), name="error-page"),
]
view.py视图.py
from django.views.generic.base import TemplateView
class ErrorPage(TemplateView):
template_name = 'error.html'
error.html(template)错误.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>
....
If you are using django message framework.如果您使用的是 django 消息框架。 In case of not using django message framework, Middleware add message into GET parameter which you can display on error page.在不使用 django 消息框架的情况下,中间件将消息添加到 GET 参数中,您可以在错误页面上显示该消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.