diff --git a/shibboleth/app_settings.py b/shibboleth/app_settings.py index 8809e4d..b11caeb 100755 --- a/shibboleth/app_settings.py +++ b/shibboleth/app_settings.py @@ -30,3 +30,6 @@ #LOGOUT_REDIRECT_URL specifies a default logout page that will always be used when #users logout from Shibboleth. LOGOUT_REDIRECT_URL = getattr(settings, 'SHIBBOLETH_LOGOUT_REDIRECT_URL', None) + +# unquote URL encoded attributes +UNQUOTE_ATTRIBUTES = getattr(settings, 'SHIBBOLETH_UNQUOTE_ATTRIBUTES', None) diff --git a/shibboleth/middleware.py b/shibboleth/middleware.py index e271492..62b8ebc 100755 --- a/shibboleth/middleware.py +++ b/shibboleth/middleware.py @@ -2,9 +2,15 @@ from django.contrib.auth.models import Group from django.contrib import auth from django.core.exceptions import ImproperlyConfigured + import re -from shibboleth.app_settings import SHIB_ATTRIBUTE_MAP, GROUP_ATTRIBUTES, GROUP_DELIMITERS +try: + from urllib.parse import unquote +except ImportError: + from urlparse import unquote + +from shibboleth.app_settings import SHIB_ATTRIBUTE_MAP, GROUP_ATTRIBUTES, GROUP_DELIMITERS, UNQUOTE_ATTRIBUTES class ShibbolethRemoteUserMiddleware(RemoteUserMiddleware): @@ -25,6 +31,8 @@ def process_request(self, request): # Locate the remote user header. try: username = request.META[self.header] + if UNQUOTE_ATTRIBUTES: + username = unquote(username) except KeyError: # If specified header doesn't exist then return (leaving # request.user set to AnonymousUser by the @@ -58,7 +66,7 @@ def process_request(self, request): # by logging the user in. request.user = user auth.login(request, user) - + # Upgrade user groups if configured in the settings.py # If activated, the user will be associated with those groups. if GROUP_ATTRIBUTES: @@ -112,6 +120,8 @@ def parse_attributes(request): attr_processor = lambda x: x value = meta.get(header, None) if value: + if UNQUOTE_ATTRIBUTES: + value = unquote(value) shib_attrs[name] = attr_processor(value) elif required: error = True @@ -124,8 +134,11 @@ def parse_group_attributes(request): """ groups = [] for attr in GROUP_ATTRIBUTES: - parsed_groups = re.split('|'.join(GROUP_DELIMITERS), - request.META.get(attr, '')) + value = request.META.get(attr, '') + if UNQUOTE_ATTRIBUTES: + value = unquote(value) + + parsed_groups = re.split('|'.join(GROUP_DELIMITERS), value) groups += filter(bool, parsed_groups) return groups