summaryrefslogtreecommitdiff
path: root/src/saml2/sigver.py
diff options
context:
space:
mode:
authorRoland Hedberg <roland.hedberg@adm.umu.se>2015-10-04 10:15:13 -0400
committerRoland Hedberg <roland.hedberg@adm.umu.se>2015-10-04 10:15:13 -0400
commita12cc2a979f5203e4c136684aa35741f4a73d1ce (patch)
treec55741de298f26c40057f6871633749281b85cad /src/saml2/sigver.py
parentdfad000305c3e2897806301d2fd24ff8c049380b (diff)
downloadpysaml2-a12cc2a979f5203e4c136684aa35741f4a73d1ce.tar.gz
Fixed various errors.
Diffstat (limited to 'src/saml2/sigver.py')
-rw-r--r--src/saml2/sigver.py52
1 files changed, 31 insertions, 21 deletions
diff --git a/src/saml2/sigver.py b/src/saml2/sigver.py
index 8c4301ee..c8d2daab 100644
--- a/src/saml2/sigver.py
+++ b/src/saml2/sigver.py
@@ -353,7 +353,10 @@ def make_temp(string, suffix="", decode=True, delete=True):
xmlsec function).
"""
ntf = NamedTemporaryFile(suffix=suffix, delete=delete)
- assert isinstance(string, six.binary_type)
+ # Python3 tempfile requires byte-like object
+ if not isinstance(string, six.binary_type):
+ string = string.encode("utf8")
+
if decode:
ntf.write(base64.b64decode(string))
else:
@@ -657,6 +660,12 @@ LOG_LINE = 60 * "=" + "\n%s\n" + 60 * "-" + "\n%s" + 60 * "="
LOG_LINE_2 = 60 * "=" + "\n%s\n%s\n" + 60 * "-" + "\n%s" + 60 * "="
+def make_str(txt):
+ if isinstance(txt, six.string_types):
+ return txt
+ else:
+ return txt.decode("utf8")
+
# ---------------------------------------------------------------------------
@@ -674,29 +683,32 @@ def read_cert_from_file(cert_file, cert_type):
return ""
if cert_type == "pem":
- line = open(cert_file).read().replace("\r\n", "\n").split("\n")
-
- if line[0] == "-----BEGIN CERTIFICATE-----":
- line = line[1:]
- elif line[0] == "-----BEGIN PUBLIC KEY-----":
- line = line[1:]
+ _a = read_file(cert_file, 'rb').decode("utf8")
+ _b = _a.replace("\r\n", "\n")
+ lines = _b.split("\n")
+
+ for pattern in ("-----BEGIN CERTIFICATE-----",
+ "-----BEGIN PUBLIC KEY-----"):
+ if pattern in lines:
+ lines = lines[lines.index(pattern)+1:]
+ break
else:
raise CertificateError("Strange beginning of PEM file")
- while line[-1] == "":
- line = line[:-1]
-
- if line[-1] == "-----END CERTIFICATE-----":
- line = line[:-1]
- elif line[-1] == "-----END PUBLIC KEY-----":
- line = line[:-1]
+ for pattern in ("-----END CERTIFICATE-----",
+ "-----END PUBLIC KEY-----"):
+ if pattern in lines:
+ lines = lines[:lines.index(pattern)]
+ break
else:
raise CertificateError("Strange end of PEM file")
- return "".join(line)
+ return make_str("".join(lines).encode("utf8"))
+
if cert_type in ["der", "cer", "crt"]:
- data = read_file(cert_file)
- return base64.b64encode(str(data))
+ data = read_file(cert_file, 'rb')
+ _cert = base64.b64encode(data)
+ return make_str(_cert)
class CryptoBackend():
@@ -850,8 +862,8 @@ class CryptoBackendXmlSec1(CryptoBackend):
'id','Id' or 'ID'
:return: The signed statement
"""
- if not isinstance(statement, six.binary_type):
- statement = str(statement).encode('utf-8')
+ if isinstance(statement, SamlBase):
+ statement = str(statement)
_, fil = make_temp(statement, suffix=".xml",
decode=False, delete=self._xmlsec_delete_tmpfiles)
@@ -1284,8 +1296,6 @@ class SecurityContext(object):
self.encryption_keypairs = encryption_keypairs
self.enc_cert_type = enc_cert_type
-
-
self.my_cert = read_cert_from_file(cert_file, cert_type)
self.cert_handler = CertHandler(self, cert_file, cert_type, key_file,