【发布时间】:2018-02-10 04:01:57
【问题描述】:
我正在使用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() 方法,但没有达到我的预期:(
更新
我包括我在看到一些 cmets 后所做的事情。
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 :(
【问题讨论】:
-
您是否尝试过在那里引发
AuthFailed错误? -
然后在社交身份验证自定义中间件异常中捕获该(或更具体的自定义异常,例如
AuthEmailNotAllowed)并将您的响应放在那里。 -
试过了,但它引发了异常。我的要求是返回
json_response或http_response -
您想要管道引发异常,然后在自定义社交中间件中捕获该异常,然后返回您的 http/json 响应。
-
@JonClements 我更新了我的问题,请看一下
标签: python django python-social-auth django-socialauth