Skip to content

Commit 695e7b0

Browse files
committed
Deal with stricter bytes/strings in py3
Several more instances of test failures in python3 caused by incompatible use of bytes vs. strings. Notable difference from other similar patches is that ascii can be used for encoding certificate strings since they are base64 encoded.
1 parent 7c84f6f commit 695e7b0

File tree

5 files changed

+8
-6
lines changed

5 files changed

+8
-6
lines changed

src/saml2/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ def get_xml_string_with_self_contained_assertion_within_advice_encrypted_asserti
602602
if assertion is not None:
603603
self.set_prefixes(assertion, prefix_map)
604604

605-
return ElementTree.tostring(tree, encoding="UTF-8")
605+
return ElementTree.tostring(tree, encoding="UTF-8").decode('utf-8')
606606

607607
def get_xml_string_with_self_contained_assertion_within_encrypted_assertion(self, assertion_tag):
608608
""" Makes a encrypted assertion only containing self contained namespaces.

src/saml2/entity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ def _encrypt_assertion(self, encrypt_cert, sp_entity_id, response, node_xpath=No
540540
_cert = "%s%s" % (begin_cert, _cert)
541541
if end_cert not in _cert:
542542
_cert = "%s%s" % (_cert, end_cert)
543-
_, cert_file = make_temp(_cert, decode=False)
543+
_, cert_file = make_temp(_cert.encode('ascii'), decode=False)
544544
response = cbxs.encrypt_assertion(response, cert_file,
545545
pre_encryption_part(), node_xpath=node_xpath)
546546
return response

src/saml2/s_utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ def deflate_and_base64_encode(string_val):
152152
:param string_val: The string to deflate and encode
153153
:return: The deflated and encoded string
154154
"""
155+
if not isinstance(string_val, six.binary_type):
156+
string_val = string_val.encode('utf-8')
155157
return base64.b64encode(zlib.compress(string_val)[2:-4])
156158

157159

src/saml2/sigver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ def signed_instance_factory(instance, seccont, elements_to_sign=None):
321321
#print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
322322
#print("%s" % signed_xml)
323323
#print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
324-
return signed_xml
324+
return signed_xml.decode('utf-8')
325325
else:
326326
return instance
327327

tests/test_50_server.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ def test_encrypted_signed_response_1(self):
560560

561561
assert valid
562562

563-
_, key_file = make_temp("%s" % cert_key_str, decode=False)
563+
_, key_file = make_temp(str(cert_key_str).encode('ascii'), decode=False)
564564

565565
decr_text = self.server.sec.decrypt(signed_resp, key_file)
566566

@@ -644,7 +644,7 @@ def test_encrypted_signed_response_3(self):
644644
id_attr="")
645645
assert valid
646646

647-
_, key_file = make_temp("%s" % cert_key_str, decode=False)
647+
_, key_file = make_temp(str(cert_key_str).encode('ascii'), decode=False)
648648

649649
decr_text = self.server.sec.decrypt(signed_resp, key_file)
650650

@@ -1138,7 +1138,7 @@ def test_slo_http_post(self):
11381138
issuer_entity_id="urn:mace:example.com:saml:roland:idp",
11391139
reason="I'm tired of this")
11401140

1141-
intermed = base64.b64encode("%s" % logout_request)
1141+
intermed = base64.b64encode(str(logout_request).encode('utf-8'))
11421142

11431143
#saml_soap = make_soap_enveloped_saml_thingy(logout_request)
11441144
request = self.server.parse_logout_request(intermed, BINDING_HTTP_POST)

0 commit comments

Comments
 (0)