annotate MoinMoin/util/moinoid.py @ 3107:c6e39279f83b

refactor logging usage
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 23 Feb 2008 22:59:30 +0100
parents c81b9c983b1d
children a48929a5036c
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
3107
c6e39279f83b refactor logging usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2298
diff changeset
8 from sha import sha
c6e39279f83b refactor logging usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2298
diff changeset
9 from random import randint
c6e39279f83b refactor logging usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2298
diff changeset
10 import time
c6e39279f83b refactor logging usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2298
diff changeset
11
2298
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
12 from openid import oidutil
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
13 from openid.store.interface import OpenIDStore
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
14 from openid.association import Association
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
15 from openid.store import nonce
3107
c6e39279f83b refactor logging usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2298
diff changeset
16
c6e39279f83b refactor logging usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2298
diff changeset
17 from MoinMoin import caching
c6e39279f83b refactor logging usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2298
diff changeset
18
c6e39279f83b refactor logging usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2298
diff changeset
19 from MoinMoin.server import getLogger
c6e39279f83b refactor logging usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2298
diff changeset
20 logging = getLogger(__name__)
c6e39279f83b refactor logging usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2298
diff changeset
21
c6e39279f83b refactor logging usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2298
diff changeset
22 # use this to temporarily and selectively enable debug logging for this module
c6e39279f83b refactor logging usage
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2298
diff changeset
23 #logging.setLevel(logging.DEBUG)
2298
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
24
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
25 # redirect openid logging to moin log
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
26 def log(msg, level=0):
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
27 logging.log(level, msg)
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 oidutil.log = log
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
30
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
31 def strbase64(value):
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
32 from base64 import encodestring
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
33 return encodestring(str(value)).replace('\n', '')
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
34
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
35
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
36 def _cleanup_nonces(request):
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
37 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
38 # 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
39 texpired = time.time() - nonce.SKEW
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
40 for name in cachelist:
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
41 entry = caching.CacheEntry(request, 'openid-nonce', name,
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
42 scope='farm', use_pickle=False)
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
43 try:
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
44 timestamp = int(entry.content())
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
45 if timestamp < texpired:
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
46 entry.remove()
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
47 except caching.CacheError:
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
48 pass
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
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
51 class MoinOpenIDStore(OpenIDStore):
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
52 '''OpenIDStore for MoinMoin'''
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
53 def __init__(self, request):
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
54 self.request = request
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
55 OpenIDStore.__init__(self)
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
56
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
57 def key(self, url):
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
58 '''return cache key'''
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
59 return sha(url).hexdigest()
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
60
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
61 def storeAssociation(self, server_url, association):
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
62 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
63 scope='wiki', use_pickle=True)
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
64 if ce.exists():
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
65 assocs = ce.content()
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
66 else:
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
67 assocs = []
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
68 assocs += [association.serialize()]
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
69 ce.update(assocs)
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
70
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
71 def getAssociation(self, server_url, handle=None):
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
72 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
73 scope='wiki', use_pickle=True)
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
74 if not ce.exists():
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
75 return None
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
76 assocs = ce.content()
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
77 found = False
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
78 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
79 assoc_str = assocs[idx]
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
80 association = Association.deserialize(assoc_str)
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
81 if association.getExpiresIn() == 0:
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
82 del assocs[idx]
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
83 else:
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
84 if handle is None or association.handle == handle:
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
85 found = True
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
86 break
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
87 ce.update(assocs)
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
88 if found:
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
89 return association
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
90 return None
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
91
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
92 def removeAssociation(self, server_url, handle):
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
93 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
94 scope='wiki', use_pickle=True)
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
95 if not ce.exists():
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
96 return
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
97 assocs = ce.content()
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
98 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
99 assoc_str = assocs[idx]
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
100 association = Association.deserialize(assoc_str)
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
101 if association.handle == handle:
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
102 del assocs[idx]
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
103 if len(assocs):
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
104 ce.update(assocs)
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
105 else:
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
106 ce.remove()
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
107
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
108 def useNonce(self, server_url, timestamp, salt):
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
109 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
110 csum = sha(val).hexdigest()
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
111 ce = caching.CacheEntry(self.request, 'openid-nonce', csum,
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
112 scope='farm', use_pickle=False)
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
113 if ce.exists():
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
114 # nonce already used!
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
115 return False
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
116 ce.update(str(timestamp))
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
117 if randint(0, 999) == 0:
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
118 self.request.add_finisher(_cleanup_nonces)
c81b9c983b1d OpenID storage for moin session
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
119 return True