-# coding=utf-8
++from django.utils.translation import ugettext
+
-class StopPipeline(Exception):
+class SocialAuthBaseException(ValueError):
+ """Base class for pipeline exceptions."""
+ pass
+
+
+class StopPipeline(SocialAuthBaseException):
"""Stop pipeline process exception.
Raise this exception to stop the rest of the pipeline process.
"""
- pass
+ def __unicode__(self):
+ return u'Stop pipeline'
-class DSAException(ValueError):
- """
- django-social-auth exception. This exception can be showed to user. It is thrown in normal situations – user declined
- access, access token expired, etc.
- """
- pass
+class AuthException(SocialAuthBaseException):
+ """Auth process exception."""
+ def __init__(self, backend, *args, **kwargs):
+ self.backend = backend
+ super(AuthException, self).__init__(*args, **kwargs)
+
+
+class AuthFailed(AuthException):
+ """Auth process failed for some reason."""
+ def __unicode__(self):
- msg = super(AuthFailed, self).__unicode__()
- return u'Authentication process failed %s' % msg
++
++ if self.message == 'access_denied':
++ return ugettext(u'Authentication process was cancelled')
++ else:
++ return ugettext(u'Authentication failed: %s') % super(AuthFailed, self).__unicode__()
+
+
+class AuthCanceled(AuthException):
+ """Auth process was canceled by user."""
+ def __unicode__(self):
+ return u'Authentication process canceled'
+
+
+class AuthUnknownError(AuthException):
+ """Unknown auth process error."""
+ def __unicode__(self):
+ msg = super(AuthFailed, self).__unicode__()
+ return u'An unknown error happened while authenticating %s' % msg
+
+
+class AuthTokenError(AuthException):
+ """Auth token error."""
+ def __unicode__(self):
+ msg = super(AuthFailed, self).__unicode__()
+ return u'Token error: %s' % msg
+
+
+class AuthMissingParameter(AuthException):
+ """Missing parameter needed to start or complete the process."""
+ def __init__(self, backend, parameter, *args, **kwargs):
+ self.parameter = parameter
+ super(AuthMissingParameter, self).__init__(backend, *args, **kwargs)
+
+ def __unicode__(self):
+ return u'Missing needed parameter %s' % self.parameter
from social_auth.utils import setting
from social_auth.models import UserSocialAuth
from social_auth.backends.pipeline import warn_setting
-from social_auth.backends.exceptions import DSAException
+from social_auth.backends.exceptions import AuthException
++from django.utils.translation import ugettext
def social_auth_user(backend, uid, user=None, *args, **kwargs):
if social_user:
if user and social_user.user != user:
- raise AuthException(backend, 'Account already in use.')
- raise DSAException(ugettext('This %(provider)s account already in use.') % {
++ raise AuthException(backend, ugettext('This %(provider)s account already in use.') % {
+ 'provider':backend.name,
+ })
elif not user:
user = social_user.user
return {'social_user': social_user, 'user': user}
from django.views.decorators.csrf import csrf_exempt
from social_auth.backends import get_backend
-from social_auth.utils import sanitize_redirect, setting, log
-from social_auth.backends.exceptions import DSAException
+from social_auth.utils import sanitize_redirect, setting, log, \
+ backend_setting, clean_partial_pipeline
++from social_auth.backends.exceptions import AuthFailed
DEFAULT_REDIRECT = setting('SOCIAL_AUTH_LOGIN_REDIRECT_URL') or \
setting('LOGIN_REDIRECT_URL')
try:
return func(request, backend, *args, **kwargs)
- except DSAException, e:
++ except AuthFailed, e:
+ backend_name = backend.AUTH_BACKEND.name
+ if 'django.contrib.messages' in setting('INSTALLED_APPS'):
+ from django.contrib.messages.api import error
+ error(request, unicode(e), extra_tags=backend_name)
+ else:
+ log('warn', 'Messages framework not in place, some '+
+ 'errors have not been shown to the user.')
+ url = setting('SOCIAL_AUTH_BACKEND_ERROR_URL', LOGIN_ERROR_URL)
+ return HttpResponseRedirect(url)
except Exception, e: # some error ocurred
if RAISE_EXCEPTIONS:
raise