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

django-social-auth : custom response during pipeline execution

我正在使用 social-auth-app-django 进行 Google 身份验证过程。我创建了一个自定义管道,我想在管道执行期间或以某种方式在某些特定条件下 return 响应 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.Changed自定义管道函数(请看上面)。
2.Added 自定义中间件 class。

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 :(

如下更改您的 MySocialAuthExceptionMiddleware class ;

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