annotate MoinMoin/util/moinoid.py @ 6103:500f68d3e2fd

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