]> git.openstreetmap.org Git - osqa.git/blob - forum_modules/openidauth/store.py
Merge pull request #65 from geraldcombs/patch-1
[osqa.git] / forum_modules / openidauth / store.py
1 import time, base64
2
3 #thanks to alexlavr
4 #see: http://meta.osqa.net/question/25/installation-issue-importerror-cannot-import-name-auth_providers#43
5 try:
6     from hashlib import md5 as md
7 except ImportError:
8     from md5 import new as md
9
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
14
15 from models import OpenIdNonce as Nonce, OpenIdAssociation as Association
16
17 class OsqaOpenIDStore(OpenIDStore):
18     def __init__(self):
19         self.max_nonce_age = 6 * 60 * 60 # Six hours
20
21     def storeAssociation(self, server_url, association):
22         assoc = 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
29         )
30         assoc.save()
31
32     def getAssociation(self, server_url, handle=None):
33         assocs = []
34         if handle is not None:
35             assocs = Association.objects.filter(
36                 server_url = server_url, handle = handle
37             )
38         else:
39             assocs = Association.objects.filter(
40                 server_url = server_url
41             )
42         if not assocs:
43             return None
44         associations = []
45         for assoc in assocs:
46             association = OIDAssociation(
47                 assoc.handle, base64.decodestring(assoc.secret), assoc.issued,
48                 assoc.lifetime, assoc.assoc_type
49             )
50             if association.getExpiresIn() == 0:
51                 self.removeAssociation(server_url, assoc.handle)
52             else:
53                 associations.append((association.issued, association))
54         if not associations:
55             return None
56         return associations[-1][1]
57
58     def removeAssociation(self, server_url, handle):
59         assocs = list(Association.objects.filter(
60             server_url = server_url, handle = handle
61         ))
62         assocs_exist = len(assocs) > 0
63         for assoc in assocs:
64             assoc.delete()
65         return assocs_exist
66
67     def storeNonce(self, nonce):
68         nonce, created = Nonce.objects.get_or_create(
69             nonce = nonce, defaults={'expires': int(time.time())}
70         )
71
72     def useNonce(self, server_url, timestamp, salt):
73         if abs(timestamp - time.time()) > oid_nonce.SKEW:
74             return False
75
76         try:
77             nonce = Nonce( server_url=server_url, timestamp=timestamp, salt=salt)
78             nonce.save()
79         except:
80             raise
81         else:
82             return 1
83
84     def getAuthKey(self):
85         # Use first AUTH_KEY_LEN characters of md5 hash of SECRET_KEY
86         return md(settings.SECRET_KEY).hexdigest()[:self.AUTH_KEY_LEN]