~gary/python-openid/python-openid-2.2.1-patched

« back to all changes in this revision

Viewing changes to openid/test/_memstore.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2007-11-30 02:46:28 UTC
  • mfrom: (1.1.1 pyopenid-2.0)
  • Revision ID: launchpad@pqm.canonical.com-20071130024628-qktwsew3383iawmq
[rs=SteveA] upgrade to python-openid-2.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from openid import cryptutil
2
 
import copy
3
 
 
4
 
class ServerAssocs(object):
5
 
    def __init__(self):
6
 
        self.assocs = {}
7
 
 
8
 
    def set(self, assoc):
9
 
        self.assocs[assoc.handle] = assoc
10
 
 
11
 
    def get(self, handle):
12
 
        return self.assocs.get(handle)
13
 
 
14
 
    def remove(self, handle):
15
 
        try:
16
 
            del self.assocs[handle]
17
 
        except KeyError:
18
 
            return False
19
 
        else:
20
 
            return True
21
 
 
22
 
    def best(self):
23
 
        """Returns association with the oldest issued date.
24
 
 
25
 
        or None if there are no associations.
26
 
        """
27
 
        best = None
28
 
        for assoc in self.assocs.values():
29
 
            if best is None or best.issued < assoc.issued:
30
 
                best = assoc
31
 
        return best
32
 
 
33
 
class MemoryStore(object):
34
 
    """In-process memory store.
35
 
 
36
 
    Use for single long-running processes.  No persistence supplied.
37
 
    """
38
 
    AUTH_KEY_LEN = 20
39
 
 
40
 
    def __init__(self):
41
 
        self.server_assocs = {}
42
 
        self.nonces = {}
43
 
        self.auth_key = cryptutil.randomString(self.AUTH_KEY_LEN)
44
 
 
45
 
    def _getServerAssocs(self, server_url):
46
 
        try:
47
 
            return self.server_assocs[server_url]
48
 
        except KeyError:
49
 
            assocs = self.server_assocs[server_url] = ServerAssocs()
50
 
            return assocs
51
 
 
52
 
    def isDumb(self):
53
 
        return False
54
 
 
55
 
    def storeAssociation(self, server_url, assoc):
56
 
        assocs = self._getServerAssocs(server_url)
57
 
        assocs.set(copy.deepcopy(assoc))
58
 
 
59
 
    def getAssociation(self, server_url, handle=None):
60
 
        assocs = self._getServerAssocs(server_url)
61
 
        if handle is None:
62
 
            return assocs.best()
63
 
        else:
64
 
            return assocs.get(handle)
65
 
 
66
 
    def removeAssociation(self, server_url, handle):
67
 
        assocs = self._getServerAssocs(server_url)
68
 
        return assocs.remove(handle)
69
 
 
70
 
    def useNonce(self, nonce):
71
 
        try:
72
 
            del self.nonces[nonce]
73
 
        except KeyError:
74
 
            return False
75
 
        else:
76
 
            return True
77
 
 
78
 
    def storeNonce(self, nonce):
79
 
        self.nonces[nonce] = None
80
 
 
81
 
    def getAuthKey(self):
82
 
        return self.auth_key
83
 
 
84
 
    def __eq__(self, other):
85
 
        return ((self.server_assocs == other.server_assocs) and
86
 
                (self.nonces == other.nonces) and
87
 
                (self.auth_key == other.auth_key))
88
 
 
89
 
    def __ne__(self, other):
90
 
        return not (self == other)