2017-09-01 52 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))