django-social-auth:管道执行期间的自定义响应

时间:2017-09-01 06:19:14

标签: python django python-social-auth django-socialauth

我正在使用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 :(

1 个答案:

答案 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))