* Generate common/djangoapps import shims for LMS * Generate common/djangoapps import shims for Studio * Stop appending project root to sys.path * Stop appending common/djangoapps to sys.path * Import from common.djangoapps.course_action_state instead of course_action_state * Import from common.djangoapps.course_modes instead of course_modes * Import from common.djangoapps.database_fixups instead of database_fixups * Import from common.djangoapps.edxmako instead of edxmako * Import from common.djangoapps.entitlements instead of entitlements * Import from common.djangoapps.pipline_mako instead of pipeline_mako * Import from common.djangoapps.static_replace instead of static_replace * Import from common.djangoapps.student instead of student * Import from common.djangoapps.terrain instead of terrain * Import from common.djangoapps.third_party_auth instead of third_party_auth * Import from common.djangoapps.track instead of track * Import from common.djangoapps.util instead of util * Import from common.djangoapps.xblock_django instead of xblock_django * Add empty common/djangoapps/__init__.py to fix pytest collection * Fix pylint formatting violations * Exclude import_shims/ directory tree from linting
52 lines
2.2 KiB
Python
52 lines
2.2 KiB
Python
"""Middleware classes for third_party_auth."""
|
|
|
|
|
|
import six.moves.urllib.parse
|
|
from django.contrib import messages
|
|
from django.shortcuts import redirect
|
|
from django.urls import reverse
|
|
from django.utils.deprecation import MiddlewareMixin
|
|
from django.utils.translation import ugettext as _
|
|
from requests import HTTPError
|
|
from social_django.middleware import SocialAuthExceptionMiddleware
|
|
|
|
from common.djangoapps.student.helpers import get_next_url_for_login_page
|
|
|
|
from . import pipeline
|
|
|
|
|
|
class ExceptionMiddleware(SocialAuthExceptionMiddleware, MiddlewareMixin):
|
|
"""Custom middleware that handles conditional redirection."""
|
|
|
|
def get_redirect_uri(self, request, exception):
|
|
# Fall back to django settings's SOCIAL_AUTH_LOGIN_ERROR_URL.
|
|
redirect_uri = super(ExceptionMiddleware, self).get_redirect_uri(request, exception)
|
|
|
|
# Safe because it's already been validated by
|
|
# pipeline.parse_query_params. If that pipeline step ever moves later
|
|
# in the pipeline stack, we'd need to validate this value because it
|
|
# would be an injection point for attacker data.
|
|
auth_entry = request.session.get(pipeline.AUTH_ENTRY_KEY)
|
|
|
|
# Check if we have an auth entry key we can use instead
|
|
if auth_entry and auth_entry in pipeline.AUTH_DISPATCH_URLS:
|
|
redirect_uri = pipeline.AUTH_DISPATCH_URLS[auth_entry]
|
|
|
|
return redirect_uri
|
|
|
|
def process_exception(self, request, exception):
|
|
"""Handles specific exception raised by Python Social Auth eg HTTPError."""
|
|
|
|
referer_url = request.META.get('HTTP_REFERER', '')
|
|
if (referer_url and isinstance(exception, HTTPError) and
|
|
exception.response.status_code == 502):
|
|
referer_url = six.moves.urllib.parse.urlparse(referer_url).path
|
|
if referer_url == reverse('signin_user'):
|
|
messages.error(request, _('Unable to connect with the external provider, please try again'),
|
|
extra_tags='social-auth')
|
|
|
|
redirect_url = get_next_url_for_login_page(request)
|
|
return redirect('/login?next=' + redirect_url)
|
|
|
|
return super(ExceptionMiddleware, self).process_exception(request, exception)
|