Search code examples
pythondjangopython-social-authdjango-socialauth

django-social-auth : custom response during pipeline execution


I'm using social-auth-app-django for Google authentication process. I created a custom pipeline and I want to return a 403 Forbidden response under some certain conditions during the pipeline execution or somehow.

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)


I tried redirect() method, but didn't get what I expected :(


UPDATE
I'm including what I've done after seeing some comments.

1.Changed custom pipeline function (Please look above).
2.Added custom Middleware 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

But, still I didn't get any HttpResponse :(


Solution

  • Change your MySocialAuthExceptionMiddleware class as below ;

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