root/trunk/djangoid/djangoidstore.py

Revision 10, 4.8 kB (checked in by nicolast, 5 years ago)

Refactored some copy-paste code

Line 
1 #Djangoid - Django-based OpenID server/provider
2 #Copyright (C) 2006  Nicolas Trangez <ikke nicolast be>
3 #
4 #This program is free software; you can redistribute it and/or modify
5 #it under the terms of the GNU General Public License as published by
6 #the Free Software Foundation; either version 2 of the License.
7 #
8 #This program is distributed in the hope that it will be useful,
9 #but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #GNU General Public License for more details.
12 #
13 #You should have received a copy of the GNU General Public License
14 #along with this program; if not, write to the Free Software
15 #Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16 #
17 #EOL
18 import time, base64
19 from djangoid.server.models import OidStoreNonce, OidStoreAssociation, OidStoreSetting
20 from djangoid.openid.store.interface import OpenIDStore
21 from djangoid.openid.association import Association
22 from djangoid.openid import cryptutil
23
24 #This is a pretty dumb rewrite of the original SqlStore
25 class DjangoidStore(OpenIDStore):
26         def __init__(self):
27                 self.max_nonce_age = 6 * 60 * 60 # Six hours, in seconds
28
29         #Use base64 in a TextField, as this can be non-text data, and django got no BlobField.
30         #Storing binary data in a TextField makes django choke because the "text" can't be converted to eg UTF8
31         def blobDecode(self, blob):
32                 return base64.decodestring(blob)
33
34         def blobEncode(self, s):
35                 return base64.encodestring(s)
36
37         def storeAssociation(self, server_url, association):
38                 OidStoreAssociation.objects.filter(server_url = server_url, handle = association.handle).delete()
39                 a = OidStoreAssociation(server_url = server_url, handle = association.handle, secret = self.blobEncode(association.secret), issued = association.issued, lifetime = association.lifetime, assoc_type = association.assoc_type)
40                 a.save()
41
42         def getAssociation(self, server_url, handle = None):
43                 associations = None
44                 if handle is not None:
45                         associations = OidStoreAssociation.objects.filter(server_url = server_url, handle = handle)
46                 else:
47                         associations = OidStoreAssociation.objects.filter(server_url = server_url)
48                 if associations.count() == 0:
49                         return None
50                 else:
51                         assocs = []
52                         for a in associations:
53                                 adata = [a.handle, self.blobDecode(a.secret), a.issued, a.lifetime, a.assoc_type]
54
55                                 assoc = Association(*adata)
56
57                                 if assoc.getExpiresIn() == 0:
58                                         self.removeAssociation(server_url, assoc.handle)
59                                 else:
60                                         assocs.append((assoc.issued, assoc))
61                          
62                         if assocs:
63                                 assocs.sort()
64                                 return assocs[-1][1]
65                         else:
66                                 return None
67
68         def removeAssociation(self, server_url, handle):
69                 assocs = OidStoreAssociation.objects.filter(server_url = server_url, handle = handle)
70                 cnt = assocs.count()
71                 assocs.delete()
72                 return cnt > 0
73
74         def storeNonce(self, nonce):
75                 now = int(time.time())
76                 nonce = OidStoreNonce(nonce = nonce, expires = now)
77                 nonce.save()
78
79         def useNonce(self, nonce):
80                 nonce = OidStoreNonce.objects.get(nonce = nonce)
81                 if nonce.count() <= 0:
82                         present = 0
83                 else:
84                         nonce_age = int(time.time()) - nonce.timestamp
85                         if nonce_age > self.max_nonce_age:
86                                 present = 0
87                         else:
88                                 present = 1
89
90                         nonce.delete()
91
92                 return present
93
94         def getAuthKey(self):
95                 key = OidStoreSetting.objects.get(setting = "auth_key")
96                 if key.count() == 0:
97                         auth_key = cryptutil.randomString(self.AUTH_KEY_LEN)
98                         s = OidStoreSetting(setting = "auth_key", value = self.blobEncode(auth_key))
99                         s.save()
100                 else:
101                         auth_key = self.blobDecode(key.value)
102
103                 if len(auth_key) != self.AUTH_KEY_LEN:
104                         fmt = "Expected %d-byte string for auth key, got %r"
105                         raise ValueError(fmt % (self.AUTH_KEY_LEN, auth_key))
106
107                 return auth_key
108
109         def isDumb(self):
110                 return False
Note: See TracBrowser for help on using the browser.