]> git.openstreetmap.org Git - osqa.git/blob - forum_modules/openidauth/store.py
Initial commit
[osqa.git] / forum_modules / openidauth / store.py
1 import time, base64, md5\r
2 \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
7 \r
8 from models import OpenIdNonce as Nonce, OpenIdAssociation as Association\r
9 \r
10 class OsqaOpenIDStore(OpenIDStore):\r
11     def __init__(self):\r
12         self.max_nonce_age = 6 * 60 * 60 # Six hours\r
13 \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
22         )\r
23         assoc.save()\r
24 \r
25     def getAssociation(self, server_url, handle=None):\r
26         assocs = []\r
27         if handle is not None:\r
28             assocs = Association.objects.filter(\r
29                 server_url = server_url, handle = handle\r
30             )\r
31         else:\r
32             assocs = Association.objects.filter(\r
33                 server_url = server_url\r
34             )\r
35         if not assocs:\r
36             return None\r
37         associations = []\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
42             )\r
43             if association.getExpiresIn() == 0:\r
44                 self.removeAssociation(server_url, assoc.handle)\r
45             else:\r
46                 associations.append((association.issued, association))\r
47         if not associations:\r
48             return None\r
49         return associations[-1][1]\r
50 \r
51     def removeAssociation(self, server_url, handle):\r
52         assocs = list(Association.objects.filter(\r
53             server_url = server_url, handle = handle\r
54         ))\r
55         assocs_exist = len(assocs) > 0\r
56         for assoc in assocs:\r
57             assoc.delete()\r
58         return assocs_exist\r
59 \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
63         )\r
64 \r
65     def useNonce(self, server_url, timestamp, salt):\r
66         if abs(timestamp - time.time()) > oid_nonce.SKEW:\r
67             return False\r
68 \r
69         try:\r
70             nonce = Nonce( server_url=server_url, timestamp=timestamp, salt=salt)\r
71             nonce.save()\r
72         except:\r
73             raise\r
74         else:\r
75             return 1\r
76 \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