Skip to content

Commit 691e981

Browse files
Merge pull request #550 from johanlundberg/non_ascii_ava_encryption_decryption
Support non-ascii attribute values for encryption and decryption
2 parents c5c7e2d + 35fc1dc commit 691e981

File tree

6 files changed

+2976
-94
lines changed

6 files changed

+2976
-94
lines changed

src/saml2/response.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33
#
4-
4+
from future.utils import python_2_unicode_compatible
55
import calendar
66
import logging
77
import six
8+
89
from saml2.samlp import STATUS_VERSION_MISMATCH
910
from saml2.samlp import STATUS_AUTHN_FAILED
1011
from saml2.samlp import STATUS_INVALID_ATTR_NAME_OR_VALUE
@@ -460,7 +461,7 @@ def __init__(self, sec_context, return_addrs=None, timeslack=0,
460461

461462
# ----------------------------------------------------------------------------
462463

463-
464+
@python_2_unicode_compatible
464465
class AuthnResponse(StatusResponse):
465466
""" This is where all the profile compliance is checked.
466467
This one does saml2int compliance. """
@@ -1081,9 +1082,9 @@ def session_info(self):
10811082
"session_index": authn_statement.session_index}
10821083

10831084
def __str__(self):
1084-
if not isinstance(self.xmlstr, six.string_types):
1085-
return "%s" % self.xmlstr.decode("utf-8")
1086-
return "%s" % self.xmlstr
1085+
if isinstance(self.xmlstr, six.string_types):
1086+
return self.xmlstr
1087+
return str(self.xmlstr)
10871088

10881089
def verify_recipient(self, recipient):
10891090
"""

src/saml2/saml.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def _wrong_type_value(xsd, value):
171171
# only work with six.string_types
172172
_str = unicode if six.PY2 else str
173173
if isinstance(value, six.binary_type):
174-
value = value.decode()
174+
value = value.decode('utf-8')
175175

176176
type_to_xsd = {
177177
_str: 'string',

src/saml2/sigver.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,9 @@ def signed_instance_factory(instance, seccont, elements_to_sign=None):
310310
:return: A class instance if not signed otherwise a string
311311
"""
312312
if elements_to_sign:
313-
signed_xml = str(instance)
313+
signed_xml = instance
314+
if not isinstance(instance, six.string_types):
315+
signed_xml = instance.to_string()
314316
for (node_name, nodeid) in elements_to_sign:
315317
signed_xml = seccont.sign_statement(
316318
signed_xml, node_name=node_name, node_id=nodeid)
@@ -336,7 +338,7 @@ def make_temp(string, suffix='', decode=True, delete=True):
336338
ntf = NamedTemporaryFile(suffix=suffix, delete=delete)
337339
# Python3 tempfile requires byte-like object
338340
if not isinstance(string, six.binary_type):
339-
string = string.encode()
341+
string = string.encode('utf-8')
340342

341343
if decode:
342344
ntf.write(base64.b64decode(string))
@@ -712,7 +714,7 @@ def encrypt(self, text, recv_key, template, session_key_type, xpath=''):
712714
:return:
713715
"""
714716
logger.debug('Encryption input len: %d', len(text))
715-
_, fil = make_temp(str(text).encode(), decode=False)
717+
_, fil = make_temp(text, decode=False)
716718

717719
com_list = [
718720
self.xmlsec,
@@ -743,13 +745,17 @@ def encrypt_assertion(self, statement, enc_key, template, key_type='des-192', no
743745
:param key_type: The type of session key to use.
744746
:return: The encrypted text
745747
"""
748+
if six.PY2:
749+
_str = unicode
750+
else:
751+
_str = str
746752

747753
if isinstance(statement, SamlBase):
748754
statement = pre_encrypt_assertion(statement)
749755

750-
_, fil = make_temp(str(statement).encode(), decode=False,
756+
_, fil = make_temp(_str(statement), decode=False,
751757
delete=False)
752-
_, tmpl = make_temp(str(template).encode(), decode=False)
758+
_, tmpl = make_temp(_str(template), decode=False)
753759

754760
if not node_xpath:
755761
node_xpath = ASSERT_XPATH
@@ -776,7 +782,7 @@ def encrypt_assertion(self, statement, enc_key, template, key_type='des-192', no
776782
if not output:
777783
raise EncryptError(_stderr)
778784

779-
return output.decode()
785+
return output.decode('utf-8')
780786

781787
def decrypt(self, enctext, key_file, id_attr):
782788
"""
@@ -787,7 +793,7 @@ def decrypt(self, enctext, key_file, id_attr):
787793
"""
788794

789795
logger.debug('Decrypt input len: %d', len(enctext))
790-
_, fil = make_temp(str(enctext).encode(), decode=False)
796+
_, fil = make_temp(enctext, decode=False)
791797

792798
com_list = [
793799
self.xmlsec,
@@ -802,8 +808,7 @@ def decrypt(self, enctext, key_file, id_attr):
802808
[fil],
803809
exception=DecryptError,
804810
validate_output=False)
805-
806-
return output.decode()
811+
return output.decode('utf-8')
807812

808813
def sign_statement(self, statement, node_name, key_file, node_id, id_attr):
809814
"""
@@ -846,7 +851,7 @@ def sign_statement(self, statement, node_name, key_file, node_id, id_attr):
846851
# this doesn't work if --store-signatures are used
847852
if stdout == '':
848853
if signed_statement:
849-
return signed_statement.decode()
854+
return signed_statement.decode('utf-8')
850855

851856
logger.error('Signing operation failed :\nstdout : %s\nstderr : %s', stdout, stderr)
852857
raise SigverError(stderr)
@@ -866,7 +871,7 @@ def validate_signature(self, signedtext, cert_file, cert_type, node_name, node_i
866871
:return: Boolean True if the signature was correct otherwise False.
867872
"""
868873
if not isinstance(signedtext, six.binary_type):
869-
signedtext = signedtext.encode()
874+
signedtext = signedtext.encode('utf-8')
870875

871876
_, fil = make_temp(
872877
signedtext,
@@ -1409,7 +1414,6 @@ def decrypt(self, enctext, key_file=None, id_attr=''):
14091414
_enctext = self.crypto.decrypt(enctext, key_file, id_attr)
14101415
if _enctext is not None and len(_enctext) > 0:
14111416
return _enctext
1412-
14131417
return enctext
14141418

14151419
def verify_signature(self, signedtext, cert_file=None, cert_type='pem', node_name=NODE_NAME, node_id=None, id_attr=''):

0 commit comments

Comments
 (0)