Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python-social-auth AuthCanceled exception

I'm using python-social-auth in my Django application for authentication via Facebook. But when a user tries to login, they have been redirected to the Facebook app page, and they click on the "Cancel" button, the following exception appears:

ERROR 2014-01-03 15:32:15,308 base :: Internal Server Error: /complete/facebook/
Traceback (most recent call last):
  File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 114, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 57, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/social/apps/django_app/utils.py", line 45, in wrapper
    return func(request, backend, *args, **kwargs)
  File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/social/apps/django_app/views.py", line 21, in complete
    redirect_name=REDIRECT_FIELD_NAME, *args, **kwargs)
  File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/social/actions.py", line 54, in do_complete
    *args, **kwargs)
  File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/social/strategies/base.py", line 62, in complete
    return self.backend.auth_complete(*args, **kwargs)
  File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/social/backends/facebook.py", line 63, in auth_complete
    self.process_error(self.data)
  File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/social/backends/facebook.py", line 56, in process_error
    super(FacebookOAuth2, self).process_error(data)
  File "/home/vera/virtualenv/myapp/local/lib/python2.7/site-packages/social/backends/oauth.py", line 312, in process_error
    raise AuthCanceled(self, data.get('error_description', ''))
AuthCanceled: Authentication process canceled

Is the any way to catch it Django?

like image 386
cansadadeserfeliz Avatar asked Jan 03 '14 15:01

cansadadeserfeliz


3 Answers

python-social-auth is a newer, derived version of django-social-auth.

AlexYar's answer can be slightly modified to work with python-social-auth by modify settings.py with following changes:

  1. Add a middleware to handle the SocialAuthException

    MIDDLEWARE_CLASSES += (
        'social.apps.django_app.middleware.SocialAuthExceptionMiddleware',
    )
    
  2. URL to redirect to, when an exception occurred

    SOCIAL_AUTH_LOGIN_ERROR_URL = '/'
    
  3. Note that you also need to set

    DEBUG = False
    

That's all or read http://python-social-auth.readthedocs.org/en/latest/configuration/django.html#exceptions-middleware

like image 121
nattster Avatar answered Oct 14 '22 15:10

nattster


you can create a middleware and catch any exceptions, exception list: https://github.com/omab/python-social-auth/blob/master/social/exceptions.py in this case your AuthCanceled Exception.

middleware.py



from social.apps.django_app.middleware import SocialAuthExceptionMiddleware
from django.shortcuts import HttpResponse
from social import exceptions as social_exceptions

class SocialAuthExceptionMiddleware(SocialAuthExceptionMiddleware):
    def process_exception(self, request, exception):
        if hasattr(social_exceptions, 'AuthCanceled'):
            return HttpResponse("I'm the Pony %s" % exception)
        else:
            raise exception


settings.py



MIDDLEWARE_CLASSES = (
        .....
        'pat_to_middleware.SocialAuthExceptionMiddleware',
)


like image 37
Nicolás Avatar answered Oct 14 '22 16:10

Nicolás


This is slight modification of @Nicolas answer and this works for me.

middleware.py

from social.apps.django_app.middleware import SocialAuthExceptionMiddleware
from django.shortcuts import render
from social.exceptions import AuthCanceled

class SocialAuthExceptionMiddleware(SocialAuthExceptionMiddleware):
    def process_exception(self, request, exception):
        if type(exception) == AuthCanceled:
            return render(request, "pysocial/authcancelled.html", {})
        else:
            pass

settings.py

MIDDLEWARE_CLASSES += (
'myapp.middleware.SocialAuthExceptionMiddleware',
)
like image 39
Ryu_hayabusa Avatar answered Oct 14 '22 14:10

Ryu_hayabusa