1 import time, base64, md5
\r
3 from openid.store import nonce as oid_nonce
\r
4 from openid.store.interface import OpenIDStore
\r
5 from openid.association import Association as OIDAssociation
\r
6 from django.conf import settings
\r
8 from models import OpenIdNonce as Nonce, OpenIdAssociation as Association
\r
10 class OsqaOpenIDStore(OpenIDStore):
\r
12 self.max_nonce_age = 6 * 60 * 60 # Six hours
\r
14 def storeAssociation(self, server_url, association):
\r
15 assoc = Association(
\r
16 server_url = server_url,
\r
17 handle = association.handle,
\r
18 secret = base64.encodestring(association.secret),
\r
19 issued = association.issued,
\r
20 lifetime = association.issued,
\r
21 assoc_type = association.assoc_type
\r
25 def getAssociation(self, server_url, handle=None):
\r
27 if handle is not None:
\r
28 assocs = Association.objects.filter(
\r
29 server_url = server_url, handle = handle
\r
32 assocs = Association.objects.filter(
\r
33 server_url = server_url
\r
38 for assoc in assocs:
\r
39 association = OIDAssociation(
\r
40 assoc.handle, base64.decodestring(assoc.secret), assoc.issued,
\r
41 assoc.lifetime, assoc.assoc_type
\r
43 if association.getExpiresIn() == 0:
\r
44 self.removeAssociation(server_url, assoc.handle)
\r
46 associations.append((association.issued, association))
\r
47 if not associations:
\r
49 return associations[-1][1]
\r
51 def removeAssociation(self, server_url, handle):
\r
52 assocs = list(Association.objects.filter(
\r
53 server_url = server_url, handle = handle
\r
55 assocs_exist = len(assocs) > 0
\r
56 for assoc in assocs:
\r
60 def storeNonce(self, nonce):
\r
61 nonce, created = Nonce.objects.get_or_create(
\r
62 nonce = nonce, defaults={'expires': int(time.time())}
\r
65 def useNonce(self, server_url, timestamp, salt):
\r
66 if abs(timestamp - time.time()) > oid_nonce.SKEW:
\r
70 nonce = Nonce( server_url=server_url, timestamp=timestamp, salt=salt)
\r
77 def getAuthKey(self):
\r
78 # Use first AUTH_KEY_LEN characters of md5 hash of SECRET_KEY
\r
79 return md5.new(settings.SECRET_KEY).hexdigest()[:self.AUTH_KEY_LEN]
\r