Skip to content

Commit 52028d3

Browse files
author
Roland Hedberg
committed
Merge pull request #221 from SpamapS/master
More python3 fixes
2 parents dc65c85 + 95ecd51 commit 52028d3

29 files changed

+131
-53
lines changed

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def run_tests(self):
4242
'python-memcached >= 1.51',
4343
'pytest',
4444
'mako',
45+
'webob',
4546
#'pytest-coverage',
4647
]
4748

src/idp_test/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import types
77
import argparse
88
import sys
9+
import six
910

1011
import logging
1112
import imp
@@ -356,7 +357,7 @@ def list_operations(self):
356357
item = {"id": key, "name": val["name"]}
357358
try:
358359
_desc = val["descr"]
359-
if isinstance(_desc, basestring):
360+
if isinstance(_desc, six.string_types):
360361
item["descr"] = _desc
361362
else:
362363
item["descr"] = "\n".join(_desc)
@@ -377,7 +378,7 @@ def list_operations(self):
377378
item = {"id": key, "name": val["name"]}
378379
try:
379380
_desc = val["descr"]
380-
if isinstance(_desc, basestring):
381+
if isinstance(_desc, six.string_types):
381382
item["descr"] = _desc
382383
else:
383384
item["descr"] = "\n".join(_desc)

src/idp_test/interaction.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import json
44
import logging
5+
import six
56

67
from urlparse import urlparse
78
from bs4 import BeautifulSoup
@@ -34,7 +35,8 @@ def pick_interaction(interactions, _base="", content="", req=None):
3435
_match += 1
3536
else:
3637
_c = _bs.title.contents
37-
if isinstance(_c, list) and not isinstance(_c, basestring):
38+
if isinstance(_c, list) and not isinstance(
39+
_c, six.string_types):
3840
for _line in _c:
3941
if val in _line:
4042
_match += 1
@@ -165,7 +167,7 @@ def pick_form(response, url=None, **kwargs):
165167
_default = _ava["value"]
166168
try:
167169
orig_val = form[prop]
168-
if isinstance(orig_val, basestring):
170+
if isinstance(orig_val, six.string_types):
169171
if orig_val == _default:
170172
_form = form
171173
elif _default in orig_val:

src/saml2/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,11 @@ def to_string(self, nspair=None):
674674
return ElementTree.tostring(self._to_element_tree(), encoding="UTF-8")
675675

676676
def __str__(self):
677-
return self.to_string()
677+
# Yes this is confusing. http://bugs.python.org/issue10942
678+
x = self.to_string()
679+
if not isinstance(x, six.string_types):
680+
x = x.decode('utf-8')
681+
return x
678682

679683
def keyswv(self):
680684
""" Return the keys of attributes or children that has values

src/saml2/assertion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import re
77
from saml2.saml import NAME_FORMAT_URI
88
import six
9-
import xmlenc
9+
from saml2 import xmlenc
1010

1111
from saml2 import saml
1212

src/saml2/cert.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import datetime
55
import dateutil.parser
66
import pytz
7+
import six
78
from OpenSSL import crypto
89
from os.path import join
910
from os import remove
@@ -154,10 +155,13 @@ def create_certificate(self, cert_info, request=False, valid_from=0,
154155
tmp_cert = crypto.dump_certificate(crypto.FILETYPE_PEM, cert)
155156
tmp_key = None
156157
if cipher_passphrase is not None:
158+
passphrase = cipher_passphrase["passphrase"]
159+
if isinstance(cipher_passphrase["passphrase"],
160+
six.string_types):
161+
passphrase = passphrase.encode('utf-8')
157162
tmp_key = crypto.dump_privatekey(crypto.FILETYPE_PEM, k,
158163
cipher_passphrase["cipher"],
159-
cipher_passphrase[
160-
"passphrase"])
164+
passphrase)
161165
else:
162166
tmp_key = crypto.dump_privatekey(crypto.FILETYPE_PEM, k)
163167
if write_to_file:
@@ -190,7 +194,7 @@ def write_str_to_file(self, file, str_data):
190194
f.close()
191195

192196
def read_str_from_file(self, file, type="pem"):
193-
f = open(file)
197+
f = open(file, 'rt')
194198
str_data = f.read()
195199
f.close()
196200

@@ -257,7 +261,10 @@ def create_cert_signed_certificate(self, sign_cert_str, sign_key_str,
257261
cert.set_pubkey(req_cert.get_pubkey())
258262
cert.sign(ca_key, hash_alg)
259263

260-
return crypto.dump_certificate(crypto.FILETYPE_PEM, cert)
264+
cert_dump = crypto.dump_certificate(crypto.FILETYPE_PEM, cert)
265+
if isinstance(cert_dump, six.string_types):
266+
return cert_dump
267+
return cert_dump.decode('utf-8')
261268

262269
def verify_chain(self, cert_chain_str_list, cert_str):
263270
"""
@@ -327,6 +334,8 @@ def verify(self, signing_cert_str, cert_str):
327334
"signed certificate.")
328335

329336
cert_algorithm = cert.get_signature_algorithm()
337+
if six.PY3:
338+
cert_algorithm = cert_algorithm.decode('ascii')
330339

331340
cert_asn1 = crypto.dump_certificate(crypto.FILETYPE_ASN1, cert)
332341

@@ -342,7 +351,9 @@ def verify(self, signing_cert_str, cert_str):
342351

343352
signature_payload = cert_signature_decoded.payload
344353

345-
if signature_payload[0] != '\x00':
354+
sig_pay0 = signature_payload[0]
355+
if ((isinstance(sig_pay0, int) and sig_pay0 != 0) or
356+
(isinstance(sig_pay0, str) and sig_pay0 != '\x00')):
346357
return (False,
347358
"The certificate should not contain any unused bits.")
348359

@@ -355,4 +366,4 @@ def verify(self, signing_cert_str, cert_str):
355366
except crypto.Error as e:
356367
return False, "Certificate is incorrectly signed."
357368
except Exception as e:
358-
return False, "Certificate is not valid for an unknown reason."
369+
return False, "Certificate is not valid for an unknown reason. %s" % str(e)

src/saml2/client_base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
to conclude its tasks.
77
"""
88
import threading
9-
from urllib import urlencode
10-
from urlparse import urlparse
9+
from six.moves.urllib.parse import urlencode
10+
from six.moves.urllib.parse import urlparse
1111
import six
1212

1313
from saml2.entity import Entity
@@ -25,7 +25,7 @@
2525
import time
2626
from saml2.soap import make_soap_enveloped_saml_thingy
2727

28-
from urlparse import parse_qs
28+
from six.moves.urllib.parse import parse_qs
2929

3030
from saml2.s_utils import signature, UnravelError, exception_trace
3131
from saml2.s_utils import do_attributes

src/saml2/config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import re
99
import logging
1010
import logging.handlers
11+
import six
1112

1213
from importlib import import_module
1314

@@ -296,7 +297,7 @@ def load_complex(self, cnf, typ="", metadata_construction=False):
296297

297298
def unicode_convert(self, item):
298299
try:
299-
return unicode(item, "utf-8")
300+
return six.text_type(item, "utf-8")
300301
except TypeError:
301302
_uc = self.unicode_convert
302303
if isinstance(item, dict):

src/saml2/entity.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from hashlib import sha1
55
from Crypto.PublicKey import RSA
66
import requests
7+
import six
78
from saml2.metadata import ENDPOINTS
89
from saml2.profile import paos, ecp
910
from saml2.soap import parse_soap_enveloped_saml_artifact_resolve
@@ -156,7 +157,7 @@ def __init__(self, entity_type, config=None, config_file="",
156157
self.sec = security_context(self.config)
157158

158159
if virtual_organization:
159-
if isinstance(virtual_organization, basestring):
160+
if isinstance(virtual_organization, six.string_types):
160161
self.vorg = self.config.vorg[virtual_organization]
161162
elif isinstance(virtual_organization, VirtualOrg):
162163
self.vorg = virtual_organization

src/saml2/eptid.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import shelve
99

1010
import logging
11+
import six
1112

1213
logger = logging.getLogger(__name__)
1314

@@ -21,13 +22,23 @@ def make(self, idp, sp, args):
2122
md5 = hashlib.md5()
2223
for arg in args:
2324
md5.update(arg.encode("utf-8"))
24-
md5.update(sp)
25-
md5.update(self.secret)
25+
if isinstance(sp, six.binary_type):
26+
md5.update(sp)
27+
else:
28+
md5.update(sp.encode('utf-8'))
29+
if isinstance(self.secret, six.binary_type):
30+
md5.update(self.secret)
31+
else:
32+
md5.update(self.secret.encode('utf-8'))
2633
md5.digest()
2734
hashval = md5.hexdigest()
35+
if isinstance(hashval, six.binary_type):
36+
hashval = hashval.decode('ascii')
2837
return "!".join([idp, sp, hashval])
2938

3039
def __getitem__(self, key):
40+
if six.PY3 and isinstance(key, six.binary_type):
41+
key = key.decode('utf-8')
3142
return self._db[key]
3243

3344
def __setitem__(self, key, value):
@@ -47,4 +58,7 @@ def get(self, idp, sp, *args):
4758
class EptidShelve(Eptid):
4859
def __init__(self, secret, filename):
4960
Eptid.__init__(self, secret)
50-
self._db = shelve.open(filename, writeback=True)
61+
if six.PY3:
62+
if filename.endswith('.db'):
63+
filename = filename.rsplit('.db', 1)[0]
64+
self._db = shelve.open(filename, writeback=True, protocol=2)

0 commit comments

Comments
 (0)