annotate MoinMoin/util/moinoid.py @ 4363:817d99d715fe

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