forked from python-social-auth/social-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.py
84 lines (68 loc) · 2.69 KB
/
store.py
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
73
74
75
76
77
78
79
80
81
82
83
84
import time
try:
import cPickle as pickle
except ImportError:
import pickle
from openid.store.interface import OpenIDStore as BaseOpenIDStore
from openid.store.nonce import SKEW
class OpenIdStore(BaseOpenIDStore):
"""Storage class"""
def __init__(self, strategy):
"""Init method"""
super(OpenIdStore, self).__init__()
self.strategy = strategy
self.storage = strategy.storage
self.assoc = self.storage.association
self.nonce = self.storage.nonce
self.max_nonce_age = 6 * 60 * 60 # Six hours
def storeAssociation(self, server_url, association):
"""Store new association if it does not exist"""
self.assoc.store(server_url, association)
def removeAssociation(self, server_url, handle):
"""Remove association"""
associations_ids = list(dict(self.assoc.oids(server_url,
handle)).keys())
if associations_ids:
self.assoc.remove(associations_ids)
def expiresIn(self, assoc):
if hasattr(assoc, 'getExpiresIn'):
return assoc.getExpiresIn()
else: # python3-openid 3.0.2
return assoc.expiresIn
def getAssociation(self, server_url, handle=None):
"""Return stored association"""
associations, expired = [], []
for assoc_id, association in self.assoc.oids(server_url, handle):
expires = self.expiresIn(association)
if expires > 0:
associations.append(association)
elif expires == 0:
expired.append(assoc_id)
if expired: # clear expired associations
self.assoc.remove(expired)
if associations: # return most recet association
return associations[0]
def useNonce(self, server_url, timestamp, salt):
"""Generate one use number and return *if* it was created"""
if abs(timestamp - time.time()) > SKEW:
return False
return self.nonce.use(server_url, timestamp, salt)
class OpenIdSessionWrapper(dict):
pickle_instances = (
'_yadis_services__openid_consumer_',
'_openid_consumer_last_token'
)
def __getitem__(self, name):
value = super(OpenIdSessionWrapper, self).__getitem__(name)
if name in self.pickle_instances:
value = pickle.loads(value)
return value
def __setitem__(self, name, value):
if name in self.pickle_instances:
value = pickle.dumps(value, 0)
super(OpenIdSessionWrapper, self).__setitem__(name, value)
def get(self, name, default=None):
try:
return self[name]
except KeyError:
return default