]> git.openstreetmap.org Git - osqa.git/blobdiff - forum_modules/openidauth/store.py
Adding Traditional Chinese translation by Deng Fei. Language code: cn.
[osqa.git] / forum_modules / openidauth / store.py
old mode 100755 (executable)
new mode 100644 (file)
index 93d38a5..f481d4f
@@ -1,79 +1,86 @@
-import time, base64, md5\r
-\r
-from openid.store import nonce as oid_nonce\r
-from openid.store.interface import OpenIDStore\r
-from openid.association import Association as OIDAssociation\r
-from django.conf import settings\r
-\r
-from models import OpenIdNonce as Nonce, OpenIdAssociation as Association\r
-\r
-class OsqaOpenIDStore(OpenIDStore):\r
-    def __init__(self):\r
-        self.max_nonce_age = 6 * 60 * 60 # Six hours\r
-\r
-    def storeAssociation(self, server_url, association):\r
-        assoc = Association(\r
-            server_url = server_url,\r
-            handle = association.handle,\r
-            secret = base64.encodestring(association.secret),\r
-            issued = association.issued,\r
-            lifetime = association.issued,\r
-            assoc_type = association.assoc_type\r
-        )\r
-        assoc.save()\r
-\r
-    def getAssociation(self, server_url, handle=None):\r
-        assocs = []\r
-        if handle is not None:\r
-            assocs = Association.objects.filter(\r
-                server_url = server_url, handle = handle\r
-            )\r
-        else:\r
-            assocs = Association.objects.filter(\r
-                server_url = server_url\r
-            )\r
-        if not assocs:\r
-            return None\r
-        associations = []\r
-        for assoc in assocs:\r
-            association = OIDAssociation(\r
-                assoc.handle, base64.decodestring(assoc.secret), assoc.issued,\r
-                assoc.lifetime, assoc.assoc_type\r
-            )\r
-            if association.getExpiresIn() == 0:\r
-                self.removeAssociation(server_url, assoc.handle)\r
-            else:\r
-                associations.append((association.issued, association))\r
-        if not associations:\r
-            return None\r
-        return associations[-1][1]\r
-\r
-    def removeAssociation(self, server_url, handle):\r
-        assocs = list(Association.objects.filter(\r
-            server_url = server_url, handle = handle\r
-        ))\r
-        assocs_exist = len(assocs) > 0\r
-        for assoc in assocs:\r
-            assoc.delete()\r
-        return assocs_exist\r
-\r
-    def storeNonce(self, nonce):\r
-        nonce, created = Nonce.objects.get_or_create(\r
-            nonce = nonce, defaults={'expires': int(time.time())}\r
-        )\r
-\r
-    def useNonce(self, server_url, timestamp, salt):\r
-        if abs(timestamp - time.time()) > oid_nonce.SKEW:\r
-            return False\r
-\r
-        try:\r
-            nonce = Nonce( server_url=server_url, timestamp=timestamp, salt=salt)\r
-            nonce.save()\r
-        except:\r
-            raise\r
-        else:\r
-            return 1\r
-\r
-    def getAuthKey(self):\r
-        # Use first AUTH_KEY_LEN characters of md5 hash of SECRET_KEY\r
-        return md5.new(settings.SECRET_KEY).hexdigest()[:self.AUTH_KEY_LEN]\r
+import time, base64
+
+#thanks to alexlavr
+#see: http://meta.osqa.net/question/25/installation-issue-importerror-cannot-import-name-auth_providers#43
+try:
+    from hashlib import md5 as md
+except ImportError:
+    from md5 import new as md
+
+from openid.store import nonce as oid_nonce
+from openid.store.interface import OpenIDStore
+from openid.association import Association as OIDAssociation
+from django.conf import settings
+
+from models import OpenIdNonce as Nonce, OpenIdAssociation as Association
+
+class OsqaOpenIDStore(OpenIDStore):
+    def __init__(self):
+        self.max_nonce_age = 6 * 60 * 60 # Six hours
+
+    def storeAssociation(self, server_url, association):
+        assoc = Association(
+            server_url = server_url,
+            handle = association.handle,
+            secret = base64.encodestring(association.secret),
+            issued = association.issued,
+            lifetime = association.lifetime,
+            assoc_type = association.assoc_type
+        )
+        assoc.save()
+
+    def getAssociation(self, server_url, handle=None):
+        assocs = []
+        if handle is not None:
+            assocs = Association.objects.filter(
+                server_url = server_url, handle = handle
+            )
+        else:
+            assocs = Association.objects.filter(
+                server_url = server_url
+            )
+        if not assocs:
+            return None
+        associations = []
+        for assoc in assocs:
+            association = OIDAssociation(
+                assoc.handle, base64.decodestring(assoc.secret), assoc.issued,
+                assoc.lifetime, assoc.assoc_type
+            )
+            if association.getExpiresIn() == 0:
+                self.removeAssociation(server_url, assoc.handle)
+            else:
+                associations.append((association.issued, association))
+        if not associations:
+            return None
+        return associations[-1][1]
+
+    def removeAssociation(self, server_url, handle):
+        assocs = list(Association.objects.filter(
+            server_url = server_url, handle = handle
+        ))
+        assocs_exist = len(assocs) > 0
+        for assoc in assocs:
+            assoc.delete()
+        return assocs_exist
+
+    def storeNonce(self, nonce):
+        nonce, created = Nonce.objects.get_or_create(
+            nonce = nonce, defaults={'expires': int(time.time())}
+        )
+
+    def useNonce(self, server_url, timestamp, salt):
+        if abs(timestamp - time.time()) > oid_nonce.SKEW:
+            return False
+
+        try:
+            nonce = Nonce( server_url=server_url, timestamp=timestamp, salt=salt)
+            nonce.save()
+        except:
+            raise
+        else:
+            return 1
+
+    def getAuthKey(self):
+        # Use first AUTH_KEY_LEN characters of md5 hash of SECRET_KEY
+        return md(settings.SECRET_KEY).hexdigest()[:self.AUTH_KEY_LEN]