Skip to content

Commit 68344ab

Browse files
committed
Fix artifact code for python3
Strings/bytes issues abound when hashing/encoding things.
1 parent f896df8 commit 68344ab

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

src/saml2/entity.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from saml2.s_utils import sid
3535
from saml2.s_utils import UnravelError
3636
from saml2.s_utils import error_status_factory
37-
from saml2.s_utils import rndstr
37+
from saml2.s_utils import rndbytes
3838
from saml2.s_utils import success_status_factory
3939
from saml2.s_utils import decode_base64_and_inflate
4040
from saml2.s_utils import UnsupportedBinding
@@ -73,7 +73,7 @@
7373

7474
__author__ = 'rolandh'
7575

76-
ARTIFACT_TYPECODE = '\x00\x04'
76+
ARTIFACT_TYPECODE = b'\x00\x04'
7777

7878
SERVICE2MESSAGE = {
7979
"single_sign_on_service": AuthnRequest,
@@ -103,11 +103,17 @@ def create_artifact(entity_id, message_handle, endpoint_index=0):
103103
:param endpoint_index:
104104
:return:
105105
"""
106+
if not isinstance(entity_id, six.binary_type):
107+
entity_id = entity_id.encode('utf-8')
106108
sourceid = sha1(entity_id)
107109

108-
ter = "%s%.2x%s%s" % (ARTIFACT_TYPECODE, endpoint_index,
109-
sourceid.digest(), message_handle)
110-
return base64.b64encode(ter)
110+
if not isinstance(message_handle, six.binary_type):
111+
message_handle = message_handle.encode('utf-8')
112+
ter = b"".join((ARTIFACT_TYPECODE,
113+
("%.2x" % endpoint_index).encode('ascii'),
114+
sourceid.digest(),
115+
message_handle))
116+
return base64.b64encode(ter).decode('ascii')
111117

112118

113119
class Entity(HTTPBase):
@@ -1115,8 +1121,8 @@ def use_artifact(self, message, endpoint_index=0):
11151121
:param endpoint_index:
11161122
:return:
11171123
"""
1118-
message_handle = sha1("%s" % message)
1119-
message_handle.update(rndstr())
1124+
message_handle = sha1(str(message).encode('utf-8'))
1125+
message_handle.update(rndbytes())
11201126
mhd = message_handle.digest()
11211127
saml_art = create_artifact(self.config.entityid, mhd, endpoint_index)
11221128
self.artifact[saml_art] = message

tests/test_64_artifact.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ def get_msg(hinfo, binding, response=False):
5454

5555
def test_create_artifact():
5656
b64art = create_artifact("http://sp.example.com/saml.xml",
57-
"aabbccddeeffgghhiijj")
57+
b"aabbccddeeffgghhiijj")
5858

59-
art = base64.b64decode(b64art)
59+
art = base64.b64decode(b64art.encode('ascii'))
6060

61-
assert art[:2] == '\x00\x04'
61+
assert art[:2] == ARTIFACT_TYPECODE
6262
assert int(art[2:4]) == 0
6363

64-
s = sha1("http://sp.example.com/saml.xml")
64+
s = sha1(b"http://sp.example.com/saml.xml")
6565
assert art[4:24] == s.digest()
6666

6767
SP = 'urn:mace:example.com:saml:roland:sp'
@@ -74,7 +74,7 @@ def test_create_artifact_resolve():
7474
#assert artifact[:2] == '\x00\x04'
7575
#assert int(artifact[2:4]) == 0
7676
#
77-
s = sha1(SP)
77+
s = sha1(SP.encode('ascii'))
7878
assert artifact[4:24] == s.digest()
7979

8080
with closing(Server(config_file="idp_all_conf")) as idp:
@@ -116,7 +116,7 @@ def test_artifact_flow():
116116
[BINDING_HTTP_ARTIFACT],
117117
entity_id=idp.config.entityid)
118118

119-
hinfo = sp.apply_binding(binding, "%s" % artifact, destination, relay_state)
119+
hinfo = sp.apply_binding(binding, artifact, destination, relay_state)
120120

121121
# ========== @IDP ============
122122

0 commit comments

Comments
 (0)