From 994d11b35828ffde261b7589ca9628bcca942427 Mon Sep 17 00:00:00 2001 From: Clint Byrum Date: Tue, 26 May 2015 11:11:18 -0700 Subject: [PATCH] Fix assertion ID tests for python3 Fixing basic renames reveals that some assumptions about the XML produced by etree need fixing, and there is a need to coerce some strings into bytes before base64. --- src/saml2/httpbase.py | 19 ++++++++++++------- src/saml2/pack.py | 5 ++++- tests/test_68_assertion_id.py | 4 ++-- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/saml2/httpbase.py b/src/saml2/httpbase.py index 2da045f..5998df2 100644 --- a/src/saml2/httpbase.py +++ b/src/saml2/httpbase.py @@ -5,6 +5,7 @@ import copy import re import urllib from six.moves.urllib.parse import urlparse +from six.moves.urllib.parse import urlencode import requests import time from six.moves.http_cookies import SimpleCookie @@ -269,10 +270,10 @@ class HTTPBase(object): @staticmethod def use_http_artifact(message, destination="", relay_state=""): if relay_state: - query = urllib.urlencode({"SAMLart": message, - "RelayState": relay_state}) + query = urlencode({"SAMLart": message, + "RelayState": relay_state}) else: - query = urllib.urlencode({"SAMLart": message}) + query = urlencode({"SAMLart": message}) info = { "data": "", "url": "%s?%s" % (destination, query) @@ -281,9 +282,13 @@ class HTTPBase(object): @staticmethod def use_http_uri(message, typ, destination="", relay_state=""): + if "\n" in message: + data = message.split("\n")[1] + else: + data = message.strip() if typ == "SAMLResponse": info = { - "data": message.split("\n")[1], + "data": data, "headers": [ ("Content-Type", "application/samlassertion+xml"), ("Cache-Control", "no-cache, no-store"), @@ -293,10 +298,10 @@ class HTTPBase(object): elif typ == "SAMLRequest": # msg should be an identifier if relay_state: - query = urllib.urlencode({"ID": message, - "RelayState": relay_state}) + query = urlencode({"ID": message, + "RelayState": relay_state}) else: - query = urllib.urlencode({"ID": message}) + query = urlencode({"ID": message}) info = { "data": "", "url": "%s?%s" % (destination, query) diff --git a/src/saml2/pack.py b/src/saml2/pack.py index 57d69df..43cfadc 100644 --- a/src/saml2/pack.py +++ b/src/saml2/pack.py @@ -59,12 +59,15 @@ def http_form_post_message(message, location, relay_state="", response = ["", """SAML 2.0 POST""", ""] if not isinstance(message, six.string_types): - message = "%s" % (message,) + message = str(message) + if not isinstance(message, six.binary_type): + message = message.encode('utf-8') if typ == "SAMLRequest" or typ == "SAMLResponse": _msg = base64.b64encode(message) else: _msg = message + _msg = _msg.decode('ascii') response.append(FORM_SPEC % (location, typ, _msg, relay_state)) diff --git a/tests/test_68_assertion_id.py b/tests/test_68_assertion_id.py index 9bef25f..52959f3 100644 --- a/tests/test_68_assertion_id.py +++ b/tests/test_68_assertion_id.py @@ -1,6 +1,6 @@ from contextlib import closing -from urlparse import parse_qs -from urlparse import urlparse +from six.moves.urllib.parse import parse_qs +from six.moves.urllib.parse import urlparse from saml2.authn_context import INTERNETPROTOCOLPASSWORD from saml2.samlp import AuthnRequest from saml2.samlp import NameIDPolicy