annotate MoinMoin/util/moinoid.py @ 2298:c81b9c983b1d

OpenID storage for moin session
author Johannes Berg <johannes AT sipsolutions DOT net>
date Tue, 10 Jul 2007 19:09:14 +0200
parents
children c6e39279f83b
rev   line source
2298
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
1 """
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
2 MoinMoin - OpenID utils
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
3
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
4 @copyright: 2006, 2007 Johannes Berg <johannes@sipsolutions.net>
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
5 @license: GNU GPL, see COPYING for details.
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
6 """
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
7
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
8 from MoinMoin import caching
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
9 from openid import oidutil
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
10 from openid.store.interface import OpenIDStore
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
11 from openid.association import Association
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
12 from openid.store import nonce
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
13 import logging
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
14 from sha import sha
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
15 from random import randint
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
16 import time
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
17
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
18 # redirect openid logging to moin log
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
19 def log(msg, level=0):
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
20 logging.log(level, msg)
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
21
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
22 oidutil.log = log
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
23
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
24 def strbase64(value):
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
25 from base64 import encodestring
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
26 return encodestring(str(value)).replace('\n', '')
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
27
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
28
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
29 def _cleanup_nonces(request):
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
30 cachelist = caching.get_cache_list(request, 'openid-nonce', 'farm')
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
31 # really openid should have a method to check this...
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
32 texpired = time.time() - nonce.SKEW
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
33 for name in cachelist:
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
34 entry = caching.CacheEntry(request, 'openid-nonce', name,
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
35 scope='farm', use_pickle=False)
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
36 try:
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
37 timestamp = int(entry.content())
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
38 if timestamp < texpired:
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
39 entry.remove()
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
40 except caching.CacheError:
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
41 pass
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
42
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
43
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
44 class MoinOpenIDStore(OpenIDStore):
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
45 '''OpenIDStore for MoinMoin'''
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
46 def __init__(self, request):
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
47 self.request = request
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
48 OpenIDStore.__init__(self)
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
49
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
50 def key(self, url):
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
51 '''return cache key'''
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
52 return sha(url).hexdigest()
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
53
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
54 def storeAssociation(self, server_url, association):
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
55 ce = caching.CacheEntry(self.request, 'openid', self.key(server_url),
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
56 scope='wiki', use_pickle=True)
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
57 if ce.exists():
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
58 assocs = ce.content()
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
59 else:
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
60 assocs = []
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
61 assocs += [association.serialize()]
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
62 ce.update(assocs)
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
63
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
64 def getAssociation(self, server_url, handle=None):
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
65 ce = caching.CacheEntry(self.request, 'openid', self.key(server_url),
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
66 scope='wiki', use_pickle=True)
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
67 if not ce.exists():
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
68 return None
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
69 assocs = ce.content()
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
70 found = False
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
71 for idx in xrange(len(assocs)-1, -1, -1):
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
72 assoc_str = assocs[idx]
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
73 association = Association.deserialize(assoc_str)
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
74 if association.getExpiresIn() == 0:
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
75 del assocs[idx]
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
76 else:
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
77 if handle is None or association.handle == handle:
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
78 found = True
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
79 break
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
80 ce.update(assocs)
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
81 if found:
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
82 return association
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
83 return None
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
84
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
85 def removeAssociation(self, server_url, handle):
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
86 ce = caching.CacheEntry(self.request, 'openid', self.key(server_url),
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
87 scope='wiki', use_pickle=True)
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
88 if not ce.exists():
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
89 return
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
90 assocs = ce.content()
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
91 for idx in xrange(len(assocs)-1, -1, -1):
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
92 assoc_str = assocs[idx]
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
93 association = Association.deserialize(assoc_str)
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
94 if association.handle == handle:
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
95 del assocs[idx]
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
96 if len(assocs):
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
97 ce.update(assocs)
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
98 else:
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
99 ce.remove()
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
100
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
101 def useNonce(self, server_url, timestamp, salt):
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
102 val = ''.join([str(server_url), str(timestamp), str(salt)])
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
103 csum = sha(val).hexdigest()
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
104 ce = caching.CacheEntry(self.request, 'openid-nonce', csum,
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
105 scope='farm', use_pickle=False)
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
106 if ce.exists():
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
107 # nonce already used!
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
108 return False
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
109 ce.update(str(timestamp))
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
110 if randint(0, 999) == 0:
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
111 self.request.add_finisher(_cleanup_nonces)
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
112 return True