我正在使用social-auth-app-django进行Google身份验证过程。我创建了一个自定义管道,我想在管道执行期间或某种程度上在某些条件下返回403 Forbidden
响应。
from social_core.exceptions import AuthFailed
def check_condition_pipeline(strategy, backend, details, response, *args, **kwargs):
email = details['email']
if email == 'check_email@domail.com':
return {'is_new': False}
else:
raise AuthFailed(backend)
我尝试了redirect()
方法,但没达到我的预期:(
的更新
我看到了一些评论之后我已经做了我的工作。
1.更改自定义管道功能(请参见上文) 2.增加了自定义中间件类。
from social_django.middleware import SocialAuthExceptionMiddleware
from django.http import HttpResponse
class MySocialAuthExceptionMiddleware(SocialAuthExceptionMiddleware):
def process_exception(self, request, exception):
if hasattr(exception, 'AuthFailed'):
return HttpResponse("Not Autherised - Custom Response")
else:
raise exception
但是,我仍然没有得到任何HttpResponse
:(
答案 0 :(得分:0)
更改您的MySocialAuthExceptionMiddleware
课程,如下所示;
from social_core.exceptions import AuthFailed
class MySocialAuthExceptionMiddleware(SocialAuthExceptionMiddleware):
def process_exception(self, request, exception):
if isinstance(exception, AuthFailed):
return HttpResponse("Not Autherised - Custom Response", status=403))