Commits

Anonymous committed a9b4fd7

[svn r22] maxlength -> max_length
commit missing files

Comments (0)

Files changed (3)

+# -*- coding: utf-8 -*-
+class OpenIDMiddleware(object):
+    """
+    Populate request.openid and request.openids with their openid. This comes 
+    eithen from their cookie or from their session, depending on the presence 
+    of OPENID_USE_SESSIONS.
+    """
+    def process_request(self, request):
+        request.openids = request.session.get('openids', [])
+        if request.openids:
+            request.openid = request.openids[-1] # Last authenticated OpenID
+        else:
+            request.openid = None
 import md5, random, sys, os, time
 
 class Nonce(models.Model):
-    server_url = models.CharField(maxlength=255)
+    server_url = models.CharField(max_length=255)
     timestamp = models.IntegerField()
     salt = models.CharField(max_length=40)
     
 
     
 class Association(models.Model):
-    server_url = models.TextField(maxlength=2047)
-    handle = models.CharField(maxlength=255)
-    secret = models.TextField(maxlength=255) # Stored base64 encoded
+    server_url = models.TextField(max_length=2047)
+    handle = models.CharField(max_length=255)
+    secret = models.TextField(max_length=255) # Stored base64 encoded
     issued = models.IntegerField()
     lifetime = models.IntegerField()
-    assoc_type = models.TextField(maxlength=64)
+    assoc_type = models.TextField(max_length=64)
     
     def __unicode__(self):
         return u"Association: %s, %s" % (self.server_url, self.handle)
     """ 
     model to manage association between openid and user 
     """
-    openid_url = models.CharField(blank=False, maxlength=255)
+    openid_url = models.CharField(blank=False, max_length=255)
     user = models.ForeignKey(User,unique=True)
     
     def __unicode__(self):
     model for new password queue.
     """
     user = models.ForeignKey(User, unique=True)
-    new_password = models.CharField(maxlength=30)
+    new_password = models.CharField(max_length=30)
     confirm_key = models.CharField(max_length=40)
 
     objects = UserPasswordQueueManager()
+# -*- coding: utf-8 -*-
+from openid.store.interface import OpenIDStore
+from openid.association import Association as OIDAssociation
+from django.db.models.query import Q
+from django.conf import settings
+
+import openid.store
+
+# needed for some linux distributions like debian
+try:
+    from openid.yadis import xri
+except:
+    from yadis import xri
+
+import time, base64, md5, operator
+
+from models import Association, Nonce
+
+class OpenID:
+    def __init__(self, openid, issued, attrs=None, sreg=None):
+        self.openid = openid
+        self.issued = issued
+        self.attrs = attrs or {}
+        self.sreg = sreg or {}
+        self.is_iname = (xri.identifierScheme(openid) == 'XRI')
+    
+    def __repr__(self):
+        return '<OpenID: %s>' % self.openid
+    
+    def __str__(self):
+        return self.openid
+
+class DjangoOpenIDStore(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.issued,
+            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 useNonce(self, server_url, timestamp, salt):
+        if abs(timestamp - time.time()) > openid.store.nonce.SKEW:
+            return False
+        
+        query =[
+                Q(server_url__exact=server_url),
+                Q(timestamp__exact=timestamp),
+                Q(salt__exact=salt),
+        ]
+        try:
+            ononce = Nonce.objects.get(reduce(operator.and_, query))
+        except Nonce.DoesNotExist:
+            ononce = Nonce(
+                    server_url=server_url,
+                    timestamp=timestamp,
+                    salt=salt
+            );
+            ononce.save()
+            return True
+        
+        ononce.delete()
+
+        return False
+   
+    def cleanupNonce(self):
+        Nonce.objects.filter(timestamp<int(time.time()) - nonce.SKEW).delete()
+
+    def cleaupAssociations(self):
+        Association.objects.extra(where=['issued + lifetimeint<(%s)' % time.time()]).delete()
+
+    def getAuthKey(self):
+        # Use first AUTH_KEY_LEN characters of md5 hash of SECRET_KEY
+        return md5.new(settings.SECRET_KEY).hexdigest()[:self.AUTH_KEY_LEN]
+    
+    def isDumb(self):
+        return False
+
+def from_openid_response(openid_response):
+    issued = int(time.time())
+    return OpenID(
+        openid_response.identity_url, issued, openid_response.signed_fields, 
+        openid_response.extensionResponse('sreg', False)
+    )