Child: [dcb73d] (diff)

Download this file

openid_model.py    110 lines (90 with data), 3.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
import time
from copy import deepcopy
from datetime import datetime, timedelta
from openid.store import nonce
from openid.association import Association
from ming import Document, Session, Field
from ming.orm.mapped_class import MappedClass
from ming.orm.property import FieldProperty
from .session import main_doc_session, main_orm_session
from .session import project_doc_session, project_orm_session
class OpenIdAssociation(MappedClass):
class __mongometa__:
name='oid_store_assoc'
session = main_orm_session
_id = FieldProperty(str) # server url
assocs = FieldProperty([dict(
key=str, value=str)])
# Mimic openid.store.memstore.ServerAssocs
def set_assoc(self, assoc):
for a in self.assocs:
if a['key'] == assoc.handle:
a['value'] = assoc.serialize()
return
self.assocs.append(dict(key=assoc.handle, value=assoc.serialize()))
def get_assoc(self, handle):
for a in self.assocs:
if a['key'] == handle:
return Association.deserialize(a['value'])
return None
def remove_assoc(self, handle):
old_len = len(self.assocs)
self.assocs = [
a for a in self.assocs
if a['key'] != handle ]
return old_len != len(self.assocs)
def best_assoc(self):
best = None
for assoc in self.assocs:
assoc = Association.deserialize(assoc['value'])
if best is None or best.issued < assoc.issued:
best = assoc
if best:
return best
else:
return None
def cleanup_assocs(self):
old_len = len(self.assocs)
self.assocs = [ a for a in self.assocs
if Association.deserialize(a['value']).getExpiresIn() != 0 ]
new_len = len(self.assocs)
return (old_len - new_len), new_len
class OpenIdNonce(MappedClass):
class __mongometa__:
name='oid_store_nonce'
session = main_orm_session
_id = FieldProperty(str) # Nonce value
timestamp = FieldProperty(datetime, if_missing=datetime.utcnow)
class OpenIdStore(object):
def _get_assocs(self, server_url):
assoc = OpenIdAssociation.query.get(_id=server_url)
if assoc is None:
assoc = OpenIdAssociation(_id=server_url)
return assoc
def storeAssociation(self, server_url, association):
assocs = self._get_assocs(server_url)
assocs.set_assoc(deepcopy(association))
def getAssociation(self, server_url, handle=None):
assocs = self._get_assocs(server_url)
if handle is None:
return assocs.best_assoc()
else:
return assocs.get_assoc(handle)
def removeAssociation(self, server_url, handle):
assocs = self._get_assocs(server_url)
return assocs.remove_assoc(handle)
def useNonce(self, server_url, timestamp, salt):
if abs(timestamp - time.time()) > nonce.SKEW:
return False
key = str((server_url, timestamp, salt))
if OpenIdNonce.query.get(_id=key) is None:
OpenIdNonce(_id=key)
return True
else:
return False
def cleanupNonces(self):
now = datetime.utcnow()
cutoff = now - timedelta(seconds=nonce.SKEW)
num_removed = OpenIdNonce.query.remove(dict(
timestamp={'$lt': cutoff}))
return num_removed