Package madrona :: Package openid :: Module openid_store
[hide private]

Source Code for Module madrona.openid.openid_store

  1  # -*- coding: utf-8 -*- 
  2  # Copyright 2007, 2008, 2009 by Benoît Chesneau <benoitc@e-engura.org> 
  3  #  
  4  # Licensed under the Apache License, Version 2.0 (the "License"); 
  5  # you may not use this file except in compliance with the License. 
  6  # You may obtain a copy of the License at 
  7  # 
  8  #     http://www.apache.org/licenses/LICENSE-2.0 
  9  # 
 10  # Unless required by applicable law or agreed to in writing, software 
 11  # distributed under the License is distributed on an "AS IS" BASIS, 
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 13  # See the License for the specific language governing permissions and 
 14  # limitations under the License. 
 15  # 
 16   
 17  import base64 
 18  import operator 
 19  import time 
 20  import urllib 
 21  try: 
 22      from hashlib import md5 as _md5 
 23  except ImportError: 
 24      import md5 
 25      _md5 = md5.new 
 26   
 27  from django.db.models.query import Q 
 28  from django.conf import settings 
 29  from openid.association import Association as OIDAssociation 
 30  import openid.store.interface 
 31  import openid.store 
 32   
 33  from madrona.openid.models import Association, Nonce 
 34  from madrona.openid.utils import OpenID 
 35   
36 -class DjangoOpenIDStore(openid.store.interface.OpenIDStore):
37 - def __init__(self):
38 self.max_nonce_age = 6 * 60 * 60 # Six hours
39
40 - def storeAssociation(self, server_url, association):
41 assoc = Association( 42 server_url=server_url, 43 handle=association.handle, 44 secret=base64.encodestring(association.secret), 45 issued=association.issued, 46 lifetime=association.lifetime, 47 assoc_type=association.assoc_type 48 ) 49 assoc.save()
50
51 - def getAssociation(self, server_url, handle=None):
52 assocs = [] 53 if handle is not None: 54 assocs = Association.objects.filter( 55 server_url=server_url, 56 handle=handle 57 ) 58 else: 59 assocs = Association.objects.filter( 60 server_url=server_url 61 ) 62 if not assocs: 63 return None 64 associations = [] 65 expired = [] 66 for assoc in assocs: 67 association = OIDAssociation( 68 assoc.handle, base64.decodestring(assoc.secret), assoc.issued, 69 assoc.lifetime, assoc.assoc_type 70 ) 71 if association.getExpiresIn() == 0: 72 expired.append(assoc) 73 else: 74 associations.append((association.issued, association)) 75 76 for assoc in expired: 77 assoc.delete() 78 if not associations: 79 return None 80 associations.sort() 81 return associations[-1][1]
82
83 - def removeAssociation(self, server_url, handle):
84 assocs = list(Association.objects.filter( 85 server_url=server_url, 86 handle=handle 87 )) 88 assocs_exist = len(assocs) > 0 89 for assoc in assocs: 90 assoc.delete() 91 return assocs_exist
92
93 - def useNonce(self, server_url, timestamp, salt):
94 if abs(timestamp - time.time()) > openid.store.nonce.SKEW: 95 return False 96 97 query = [ 98 Q(server_url__exact=server_url), 99 Q(timestamp__exact=timestamp), 100 Q(salt__exact=salt), 101 ] 102 try: 103 ononce = Nonce.objects.get(reduce(operator.and_, query)) 104 except Nonce.DoesNotExist: 105 ononce = Nonce( 106 server_url=server_url, 107 timestamp=timestamp, 108 salt=salt 109 ) 110 ononce.save() 111 return True 112 113 return False
114
115 - def cleanupNonces(self, _now=None):
116 if _now is None: 117 _now = int(time.time()) 118 expired = Nonce.objects.filter(timestamp__lt=(_now - openid.store.nonce.SKEW)) 119 count = expired.count() 120 if count: 121 expired.delete() 122 return count
123
124 - def cleanupAssociations(self):
125 now = int(time.time()) 126 expired = Association.objects.extra( 127 where=['issued + lifetime < %d' % now]) 128 count = expired.count() 129 if count: 130 expired.delete() 131 return count
132
133 - def getAuthKey(self):
134 # Use first AUTH_KEY_LEN characters of md5 hash of SECRET_KEY 135 return _md5(settings.SECRET_KEY).hexdigest()[:self.AUTH_KEY_LEN]
136
137 - def isDumb(self):
138 return False
139