/usr/share/pyshared/social_auth/middleware.py is in python-django-social-auth 0.7.23-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | # -*- coding: utf-8 -*-
from django.conf import settings
from django.contrib import messages
from django.shortcuts import redirect
from social_auth.exceptions import SocialAuthBaseException
from social_auth.utils import backend_setting, get_backend_name
class SocialAuthExceptionMiddleware(object):
"""Middleware that handles Social Auth AuthExceptions by providing the user
with a message, logging an error, and redirecting to some next location.
By default, the exception message itself is sent to the user and they are
redirected to the location specified in the LOGIN_ERROR_URL setting.
This middleware can be extended by overriding the get_message or
get_redirect_uri methods, which each accept request and exception.
"""
def process_exception(self, request, exception):
self.backend = self.get_backend(request, exception)
if self.raise_exception(request, exception):
return
if isinstance(exception, SocialAuthBaseException):
backend_name = get_backend_name(self.backend)
message = self.get_message(request, exception)
url = self.get_redirect_uri(request, exception)
tags = ['social-auth']
if backend_name:
tags.append(backend_name)
try:
messages.error(request, message, extra_tags=' '.join(tags))
except messages.MessageFailure: # messages app is not installed
url += ('?' in url and '&' or '?') + 'message=' + message
if backend_name:
url += '&backend=' + backend_name
return redirect(url)
def get_backend(self, request, exception):
if not hasattr(self, 'backend'):
self.backend = getattr(request, 'backend', None) or \
getattr(exception, 'backend', None)
return self.backend
def raise_exception(self, request, exception):
backend = self.backend
return backend and \
backend_setting(backend, 'SOCIAL_AUTH_RAISE_EXCEPTIONS')
def get_message(self, request, exception):
return unicode(exception)
def get_redirect_uri(self, request, exception):
if self.backend is not None:
return backend_setting(self.backend,
'SOCIAL_AUTH_BACKEND_ERROR_URL') or \
settings.LOGIN_ERROR_URL
return settings.LOGIN_ERROR_URL
|