From edb09ed8fd9fafc68e0369ad9a6cf1ba101f7782 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Tue, 29 May 2018 12:21:13 +0200 Subject: [PATCH] use force_text for python2/3 compatibility (#24139) --- mellon/adapters.py | 5 +++-- mellon/views.py | 7 ++++--- tests/test_default_adapter.py | 6 +++--- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/mellon/adapters.py b/mellon/adapters.py index e7bdece..460d1a6 100644 --- a/mellon/adapters.py +++ b/mellon/adapters.py @@ -10,6 +10,7 @@ from django.core.exceptions import PermissionDenied from django.contrib import auth from django.contrib.auth.models import Group from django.utils import six +from django.utils.encoding import force_text from . import utils, app_settings, models @@ -83,7 +84,7 @@ class DefaultAdapter(object): realm = utils.get_setting(idp, 'REALM') username_template = utils.get_setting(idp, 'USERNAME_TEMPLATE') try: - username = six.u(username_template).format( + username = force_text(username_template).format( realm=realm, attributes=saml_attributes, idp=idp)[:30] except ValueError: self.logger.error(u'invalid username template %r', username_template) @@ -161,7 +162,7 @@ class DefaultAdapter(object): attribute_set = False for field, tpl in attribute_mapping.items(): try: - value = six.u(tpl).format(realm=realm, attributes=saml_attributes, idp=idp) + value = force_text(tpl).format(realm=realm, attributes=saml_attributes, idp=idp) except ValueError: self.logger.warning(u'invalid attribute mapping template %r', tpl) except (AttributeError, KeyError, IndexError, ValueError) as e: diff --git a/mellon/views.py b/mellon/views.py index 263fbad..f4ee9f7 100644 --- a/mellon/views.py +++ b/mellon/views.py @@ -14,6 +14,7 @@ from django.views.decorators.csrf import csrf_exempt from django.shortcuts import render, resolve_url from django.utils.http import urlencode from django.utils import six +from django.utils.encoding import force_text from django.contrib.auth import REDIRECT_FIELD_NAME from django.db import transaction from django.utils.translation import ugettext as _ @@ -167,16 +168,16 @@ class LoginView(ProfileMixin, LogMixin, View): attributes['issuer'] = login.remoteProviderId if login.nameIdentifier: name_id = login.nameIdentifier - name_id_format = six.u(name_id.format + name_id_format = force_text(name_id.format or lasso.SAML2_NAME_IDENTIFIER_FORMAT_UNSPECIFIED) attributes.update({ 'name_id_content': lasso_decode(name_id.content), 'name_id_format': name_id_format }) if name_id.nameQualifier: - attributes['name_id_name_qualifier'] = six.u(name_id.nameQualifier) + attributes['name_id_name_qualifier'] = force_text(name_id.nameQualifier) if name_id.spNameQualifier: - attributes['name_id_sp_name_qualifier'] = six.u(name_id.spNameQualifier) + attributes['name_id_sp_name_qualifier'] = force_text(name_id.spNameQualifier) authn_statement = login.assertion.authnStatement[0] if authn_statement.authnInstant: attributes['authn_instant'] = utils.iso8601_to_datetime(authn_statement.authnInstant) diff --git a/tests/test_default_adapter.py b/tests/test_default_adapter.py index d52b432..1b891bf 100644 --- a/tests/test_default_adapter.py +++ b/tests/test_default_adapter.py @@ -77,9 +77,9 @@ def test_lookup_user_transaction(transactional_db, concurrency): def test_provision_user_attributes(settings, django_user_model, caplog): settings.MELLON_IDENTITY_PROVIDERS = [idp] settings.MELLON_ATTRIBUTE_MAPPING = { - 'email': '{attributes[email][0]}', - 'first_name': '{attributes[first_name][0]}', - 'last_name': '{attributes[last_name][0]}', + 'email': u'{attributes[email][0]}', + 'first_name': u'{attributes[first_name][0]}', + 'last_name': u'{attributes[last_name][0]}', } user = SAMLBackend().authenticate(saml_attributes=saml_attributes) assert user.username == 'x' * 30