Skip to content

Commit b8c1ae7

Browse files
authoredOct 26, 2016
Merge pull request #375 from dsully/master
Use importlib in place of backports.test.support
2 parents a58318e + 73d5897 commit b8c1ae7

File tree

3 files changed

+20
-90
lines changed

3 files changed

+20
-90
lines changed
 

‎src/saml2/config.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#!/usr/bin/env python
2+
23
import copy
3-
import sys
4-
import os
5-
import re
4+
import importlib
65
import logging
76
import logging.handlers
8-
import six
7+
import os
8+
import re
9+
import sys
910

10-
from future.backports.test.support import import_module
11+
import six
1112

1213
from saml2 import root_logger, BINDING_URI, SAMLError
1314
from saml2 import BINDING_SOAP
@@ -359,7 +360,7 @@ def _load(self, fil):
359360
else:
360361
sys.path.insert(0, head)
361362

362-
return import_module(tail)
363+
return importlib.import_module(tail)
363364

364365
def load_file(self, config_file, metadata_construction=False):
365366
if config_file.endswith(".py"):

‎src/saml2/mdstore.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
from __future__ import print_function
22
import hashlib
3+
import importlib
4+
import json
35
import logging
46
import os
57
import sys
6-
import json
7-
import requests
8-
import six
98

109
from hashlib import sha1
1110
from os.path import isfile
1211
from os.path import join
1312

14-
from future.backports.test.support import import_module
13+
import requests
14+
import six
1515

1616
from saml2 import md
1717
from saml2 import saml
@@ -694,7 +694,7 @@ def get_metadata_loader(func):
694694
i = func.rfind('.')
695695
module, attr = func[:i], func[i + 1:]
696696
try:
697-
mod = import_module(module)
697+
mod = importlib.import_module(module)
698698
except Exception as e:
699699
raise RuntimeError(
700700
'Cannot find metadata provider function %s: "%s"' % (func, e))
@@ -930,7 +930,7 @@ def imp(self, spec):
930930
raise SAMLError("Misconfiguration in metadata %s" % item)
931931
mod, clas = key.rsplit('.', 1)
932932
try:
933-
mod = import_module(mod)
933+
mod = importlib.import_module(mod)
934934
MDloader = getattr(mod, clas)
935935
except (ImportError, AttributeError):
936936
raise SAMLError("Unknown metadata loader %s" % key)

‎src/saml2/s_utils.py

+7-78
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,23 @@
11
#!/usr/bin/env python
2-
import logging
3-
import random
42

5-
import time
63
import base64
7-
import six
8-
import sys
4+
import hashlib
95
import hmac
6+
import logging
7+
import random
108
import string
11-
12-
# from python 2.5
13-
import imp
9+
import sys
10+
import time
1411
import traceback
12+
import zlib
1513

16-
if sys.version_info >= (2, 5):
17-
import hashlib
18-
else: # before python 2.5
19-
import sha
14+
import six
2015

2116
from saml2 import saml
2217
from saml2 import samlp
2318
from saml2 import VERSION
2419
from saml2.time_util import instant
2520

26-
try:
27-
from hashlib import md5
28-
except ImportError:
29-
from md5 import md5
30-
import zlib
3121

3222
logger = logging.getLogger(__name__)
3323

@@ -407,67 +397,6 @@ def verify_signature(secret, parts):
407397
return False
408398

409399

410-
FTICKS_FORMAT = "F-TICKS/SWAMID/2.0%s#"
411-
412-
413-
def fticks_log(sp, logf, idp_entity_id, user_id, secret, assertion):
414-
"""
415-
'F-TICKS/' federationIdentifier '/' version *('#' attribute '=' value) '#'
416-
Allowed attributes:
417-
TS the login time stamp
418-
RP the relying party entityID
419-
AP the asserting party entityID (typcially the IdP)
420-
PN a sha256-hash of the local principal name and a unique key
421-
AM the authentication method URN
422-
423-
:param sp: Client instance
424-
:param logf: The log function to use
425-
:param idp_entity_id: IdP entity ID
426-
:param user_id: The user identifier
427-
:param secret: A salt to make the hash more secure
428-
:param assertion: A SAML Assertion instance gotten from the IdP
429-
"""
430-
csum = hmac.new(secret, digestmod=hashlib.sha1)
431-
csum.update(user_id)
432-
ac = assertion.AuthnStatement[0].AuthnContext[0]
433-
434-
info = {
435-
"TS": time.time(),
436-
"RP": sp.entity_id,
437-
"AP": idp_entity_id,
438-
"PN": csum.hexdigest(),
439-
"AM": ac.AuthnContextClassRef.text
440-
}
441-
logf.info(FTICKS_FORMAT % "#".join(["%s=%s" % (a, v) for a, v in info]))
442-
443-
444-
def dynamic_importer(name, class_name=None):
445-
"""
446-
Dynamically imports modules / classes
447-
"""
448-
try:
449-
fp, pathname, description = imp.find_module(name)
450-
except ImportError:
451-
print("unable to locate module: " + name)
452-
return None, None
453-
454-
try:
455-
package = imp.load_module(name, fp, pathname, description)
456-
except Exception:
457-
raise
458-
459-
if class_name:
460-
try:
461-
_class = imp.load_module("%s.%s" % (name, class_name), fp,
462-
pathname, description)
463-
except Exception:
464-
raise
465-
466-
return package, _class
467-
else:
468-
return package, None
469-
470-
471400
def exception_trace(exc):
472401
message = traceback.format_exception(*sys.exc_info())
473402

0 commit comments

Comments
 (0)