1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# An eduPersonTargetedID comprises
# the entity name of the identity provider, the entity name of the service
# provider, and a opaque string value.
# These strings are separated by "!" symbols. This form is advocated by
# Internet2 and may overtake the other form in due course.
import hashlib
import shelve
import logging
import six
logger = logging.getLogger(__name__)
class Eptid(object):
def __init__(self, secret):
self._db = {}
self.secret = secret
def make(self, idp, sp, args):
md5 = hashlib.md5()
for arg in args:
md5.update(arg.encode("utf-8"))
if isinstance(sp, six.binary_type):
md5.update(sp)
else:
md5.update(sp.encode('utf-8'))
if isinstance(self.secret, six.binary_type):
md5.update(self.secret)
else:
md5.update(self.secret.encode('utf-8'))
md5.digest()
hashval = md5.hexdigest()
if isinstance(hashval, six.binary_type):
hashval = hashval.decode('ascii')
return "!".join([idp, sp, hashval])
def __getitem__(self, key):
if six.PY3 and isinstance(key, six.binary_type):
key = key.decode('utf-8')
return self._db[key]
def __setitem__(self, key, value):
if six.PY3 and isinstance(key, six.binary_type):
key = key.decode('utf-8')
self._db[key] = value
def get(self, idp, sp, *args):
# key is a combination of sp_entity_id and object id
key = ("__".join([sp, args[0]])).encode("utf-8")
try:
return self[key]
except KeyError:
val = self.make(idp, sp, args)
self[key] = val
return val
def close(self):
pass
class EptidShelve(Eptid):
def __init__(self, secret, filename):
Eptid.__init__(self, secret)
if six.PY3:
if filename.endswith('.db'):
filename = filename.rsplit('.db', 1)[0]
self._db = shelve.open(filename, writeback=True, protocol=2)
def close(self):
self._db.close()
|