4 #see: http://meta.osqa.net/question/25/installation-issue-importerror-cannot-import-name-auth_providers#43
6 from hashlib import md5 as md
8 from md5 import new as md
10 from openid.store import nonce as oid_nonce
11 from openid.store.interface import OpenIDStore
12 from openid.association import Association as OIDAssociation
13 from django.conf import settings
15 from models import OpenIdNonce as Nonce, OpenIdAssociation as Association
17 class OsqaOpenIDStore(OpenIDStore):
19 self.max_nonce_age = 6 * 60 * 60 # Six hours
21 def storeAssociation(self, server_url, association):
23 server_url = server_url,
24 handle = association.handle,
25 secret = base64.encodestring(association.secret),
26 issued = association.issued,
27 lifetime = association.lifetime,
28 assoc_type = association.assoc_type
32 def getAssociation(self, server_url, handle=None):
34 if handle is not None:
35 assocs = Association.objects.filter(
36 server_url = server_url, handle = handle
39 assocs = Association.objects.filter(
40 server_url = server_url
46 association = OIDAssociation(
47 assoc.handle, base64.decodestring(assoc.secret), assoc.issued,
48 assoc.lifetime, assoc.assoc_type
50 if association.getExpiresIn() == 0:
51 self.removeAssociation(server_url, assoc.handle)
53 associations.append((association.issued, association))
56 return associations[-1][1]
58 def removeAssociation(self, server_url, handle):
59 assocs = list(Association.objects.filter(
60 server_url = server_url, handle = handle
62 assocs_exist = len(assocs) > 0
67 def storeNonce(self, nonce):
68 nonce, created = Nonce.objects.get_or_create(
69 nonce = nonce, defaults={'expires': int(time.time())}
72 def useNonce(self, server_url, timestamp, salt):
73 if abs(timestamp - time.time()) > oid_nonce.SKEW:
77 nonce = Nonce( server_url=server_url, timestamp=timestamp, salt=salt)
85 # Use first AUTH_KEY_LEN characters of md5 hash of SECRET_KEY
86 return md(settings.SECRET_KEY).hexdigest()[:self.AUTH_KEY_LEN]