2017-09-01 51 views
0

我使用social-auth-app-django进行Google身份验证过程。我创建了一个自定义管道,并且我想在管道执行过程中以某种方式在某些特定条件下返回403 Forbidden响应。
django-social-auth:流水线执行期间的自定义响应

from social_core.exceptions import AuthFailed 
def check_condition_pipeline(strategy, backend, details, response, *args, **kwargs): 
    email = details['email'] 
    if email == '[email protected]': 
     return {'is_new': False} 
    else: 
     raise AuthFailed(backend) 


我试过redirect()方法,但并没有得到我预期:(


UPDATE
我包括我所看到的一些意见后进行。

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

您是否尝试过抛出一个'AuthFailed'的错误 –

+0

然后追赶? (或者一个更具体的自定义异常,如'AuthEmailNotAllowed')在社交认证自定义中间件异常,并把你的回应那里。 –

+0

试过了,但它引发了一个异常。我的要求是返回一个'json_respon se'或'http_response' –

回答

1

如下更改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))