annotate MoinMoin/session.py @ 3243:1ad6243cdf06

macros: refactor for new arg parser
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Wed, 12 Mar 2008 00:23:45 +0100
parents 4cd85d841bbd
children 9a3deab96cb7
rev   line source
2009
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
1 """
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
2 MoinMoin - session handling
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
3
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
4 Session handling in MoinMoin is done mostly by the request
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
5 with help from a SessionHandler instance (see below.)
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
6
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
7
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
8 @copyright: 2007 MoinMoin:JohannesBerg
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
9
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
10 @license: GNU GPL, see COPYING for details.
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
11 """
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
12
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
13 import Cookie
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
14 from MoinMoin import caching
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
15 from MoinMoin.user import User
2031
56d8a8a14114 don't use a separate random string function in session.py
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2019
diff changeset
16 from MoinMoin.util import random_string
56d8a8a14114 don't use a separate random string function in session.py
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2019
diff changeset
17 import time, random
2009
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
18
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
19 class SessionData(object):
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
20 """
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
21 MoinMoin session data base class
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
22
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
23 An object of this class must be assigned to
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
24 request.session by the SessionHandler's start
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
25 method.
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
26
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
27 Instances conform to the dict protocol (__setitem__, __getitem__,
2911
50e0afbdc870 session handling: fix bug in docstring
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2907
diff changeset
28 __contains__, __delitem__, get) and have the additional properties
2009
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
29 is_stored and is_new.
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
30 """
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
31 def __init__(self, request):
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
32 self.is_stored = False
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
33 self.is_new = True
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
34 self.request = request
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
35
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
36 def __setitem__(self, name, value):
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
37 raise NotImplementedError
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
38
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
39 def __getitem__(self, name):
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
40 raise NotImplementedError
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
41
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
42 def __contains__(self, name):
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
43 raise NotImplementedError
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
44
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
45 def __delitem__(self, name):
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
46 raise NotImplementedError
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
47
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
48 def get(self, name, default=None):
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
49 raise NotImplementedError
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
50
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
51
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
52 class DefaultSessionData(SessionData):
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
53 """ DefaultSessionData -- session data for DefaultSessionHandler
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
54
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
55 If you wish to override just the session storage then you can
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
56 inherit from this class, implement all methods and assign the
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
57 class to the dataclass keyword parameter to the DefaultSessionHandler
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
58 constructor.
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
59
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
60 Newly created objects should have be marked as expiring right away
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
61 until set_expiry() is called.
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
62 """
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
63 def __init__(self, request, name):
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
64 """create session object
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
65
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
66 @param request: the request
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
67 @param name: the session name
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
68 """
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
69 SessionData.__init__(self, request)
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
70 self.name = name
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
71
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
72 def set_expiry(self, expires):
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
73 """reset expiry for this session object"""
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
74 raise NotImplementedError
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
75
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
76 def delete(self):
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
77 """clear session data and remove from it storage"""
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
78 raise NotImplementedError
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
79
2906
aaa550567872 integrate DefaultSessionHandler cleanup into DefaultSessionData
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2905
diff changeset
80 def cleanup(cls, request):
aaa550567872 integrate DefaultSessionHandler cleanup into DefaultSessionData
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2905
diff changeset
81 """clean up expired sessions"""
aaa550567872 integrate DefaultSessionHandler cleanup into DefaultSessionData
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2905
diff changeset
82 raise NotImplementedError
aaa550567872 integrate DefaultSessionHandler cleanup into DefaultSessionData
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2905
diff changeset
83 cleanup = classmethod(cleanup)
aaa550567872 integrate DefaultSessionHandler cleanup into DefaultSessionData
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2905
diff changeset
84
2009
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
85 class CacheSessionData(DefaultSessionData):
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
86 """ SessionData -- store data for a session
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
87
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
88 This stores session data in memory and also maintains a cache of it on
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
89 disk, so the same data will be loaded from disk cache in the next request
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
90 of the same session.
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
91
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
92 Once in a while, expired session's cache files will be automatically cleaned up.
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
93 """
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
94 def __init__(self, request, name):
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
95 DefaultSessionData.__init__(self, request, name)
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
96
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
97 # we can use farm scope since the session name is totally random
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
98 # this means that the session is kept over multiple wikis in a farm
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
99 # when they share user_dir and cookies
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
100 self._ce = caching.CacheEntry(request, 'session', name, 'farm',
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
101 use_pickle=True)
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
102 try:
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
103 self._data = self._ce.content()
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
104 if self['expires'] <= time.time():
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
105 self._ce.remove()
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
106 self._data = {'expires': 0}
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
107 except caching.CacheError:
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
108 self._data = {'expires': 0}
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
109
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
110 def __setitem__(self, name, value):
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
111 self._data[name] = value
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
112 if len(self._data) > 1 and self['expires'] > time.time():
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
113 self._ce.update(self._data)
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
114
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
115 def __getitem__(self, name):
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
116 return self._data[name]
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
117
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
118 def __contains__(self, name):
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
119 return name in self._data
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
120
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
121 def __delitem__(self, name):
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
122 del self._data[name]
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
123 if len(self._data) <= 1:
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
124 self._ce.remove()
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
125 elif self['expires'] > time.time():
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
126 self._ce.update(self._data)
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
127
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
128 def get(self, name, default=None):
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
129 return self._data.get(name, default)
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
130
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
131 def set_expiry(self, expires):
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
132 # Set 'expires' an hour later than it should actually expire.
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
133 # That way, the expiry code will delete the item an hour later
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
134 # than it has actually expired, but that is acceptable and we
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
135 # don't need to update the file all the time
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
136 if expires and self['expires'] < expires:
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
137 self['expires'] = expires + 3600
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
138
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
139 def delete(self):
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
140 try:
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
141 self._ce.remove()
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
142 except caching.CacheError:
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
143 pass
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
144 self._data = {'expires': 0}
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
145
2906
aaa550567872 integrate DefaultSessionHandler cleanup into DefaultSessionData
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2905
diff changeset
146 def cleanup(cls, request):
aaa550567872 integrate DefaultSessionHandler cleanup into DefaultSessionData
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2905
diff changeset
147 cachelist = caching.get_cache_list(request, 'session', 'farm')
aaa550567872 integrate DefaultSessionHandler cleanup into DefaultSessionData
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2905
diff changeset
148 tnow = time.time()
aaa550567872 integrate DefaultSessionHandler cleanup into DefaultSessionData
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2905
diff changeset
149 for name in cachelist:
aaa550567872 integrate DefaultSessionHandler cleanup into DefaultSessionData
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2905
diff changeset
150 entry = caching.CacheEntry(request, 'session', name, 'farm',
aaa550567872 integrate DefaultSessionHandler cleanup into DefaultSessionData
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2905
diff changeset
151 use_pickle=True)
aaa550567872 integrate DefaultSessionHandler cleanup into DefaultSessionData
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2905
diff changeset
152 try:
aaa550567872 integrate DefaultSessionHandler cleanup into DefaultSessionData
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2905
diff changeset
153 data = entry.content()
aaa550567872 integrate DefaultSessionHandler cleanup into DefaultSessionData
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2905
diff changeset
154 if 'expires' in data and data['expires'] < tnow:
aaa550567872 integrate DefaultSessionHandler cleanup into DefaultSessionData
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2905
diff changeset
155 entry.remove()
aaa550567872 integrate DefaultSessionHandler cleanup into DefaultSessionData
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2905
diff changeset
156 except caching.CacheError:
aaa550567872 integrate DefaultSessionHandler cleanup into DefaultSessionData
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2905
diff changeset
157 pass
aaa550567872 integrate DefaultSessionHandler cleanup into DefaultSessionData
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2905
diff changeset
158 cleanup = classmethod(cleanup)
2009
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
159
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
160
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
161 class SessionHandler(object):
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
162 """
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
163 MoinMoin session handler base class
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
164
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
165 SessionHandler is an abstract method defining the interface
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
166 to a session handler object.
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
167
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
168 Session handling in MoinMoin works as follows:
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
169
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
170 When a request is received, first the cookie is read into a
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
171 Cookie.SimpleCookie instance, this is passed to the selected
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
172 session handler's (cfg.session_handler) start method (see below)
2019
149573c7ecaf session must store user.auth_attribs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2009
diff changeset
173 which must return a MoinMoin.user.User instance (or None). The
149573c7ecaf session must store user.auth_attribs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2009
diff changeset
174 session handler is also responsible for string the user object's
149573c7ecaf session must store user.auth_attribs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2009
diff changeset
175 auth_method and auth_attribs fields across sessions as those are
149573c7ecaf session must store user.auth_attribs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2009
diff changeset
176 not saved to the user file.
2009
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
177
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
178 Then, all authentication methods are called with this user object,
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
179 they can modify it or return a different one.
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
180
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
181 After they have changed the user object suitably, the session
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
182 handler's after_auth method is invoked to set the cookie.
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
183
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
184 Then, the request is executed and finally the session handler's
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
185 finish method is invoked.
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
186 """
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
187 def __init__(self):
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
188 """
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
189 Session handler initialisation
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
190
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
191 Only provided for future compatibility.
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
192 """
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
193 pass
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
194
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
195 def start(self, request, cookie):
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
196 """
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
197 Session handler start
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
198
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
199 Invoked very early during request handling to preload
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
200 a user object from the session (if any.)
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
201 This method must also assign to request.session an object
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
202 derived from SessionDataInterface.
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
203
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
204 @param request: the request instance
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
205 @param cookie: a Cookie.SimpleCookie with the request cookie
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
206 @return: a MoinMoin.user.User instance or None
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
207 """
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
208 raise NotImplementedError
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
209
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
210 def after_auth(self, request, cookie, user_obj):
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
211 """
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
212 Session handler auth chain callback
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
213
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
214 Invoked after all auth items have run (or multistage was
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
215 requested by one), but before the request is actually
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
216 handled and output is made. Should set the cookie.
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
217
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
218 @param request: the request instance
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
219 @param cookie: a Cookie.SimpleCookie with the request cookie
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
220 @param user_obj: the user object returned from the auth methods
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
221 (or None)
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
222 """
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
223 raise NotImplementedError
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
224
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
225 def finish(self, request, cookie, user_obj):
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
226 """
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
227 Session handler request finish callback
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
228
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
229 Invoked after the request is completely finished.
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
230
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
231 @param request: the request instance
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
232 @param cookie: a Cookie.SimpleCookie with the request cookie
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
233 @param user_obj: the user object that was used in this request
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
234 """
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
235 raise NotImplementedError
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
236
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
237
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
238
2912
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
239
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
240 class SessionIDHandler:
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
241 """
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
242 MoinMoin session ID handling
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
243
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
244 Instances of this class are used by the session handling code
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
245 to set/get the persistent ID that is used to identify the session
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
246 which is usually stored in a cookie.
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
247 """
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
248 _SESSION_NAME_CHARS = 'abcdefghijklmnopqrstuvwxyz0123456789_-'
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
249 _SESSION_NAME_LEN = 32
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
250
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
251 def __init__(self):
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
252 """
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
253 Initialise the session ID handler.
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
254 """
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
255 pass
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
256
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
257 def get(self, request):
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
258 """
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
259 Return the persistent ID for this request.
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
260
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
261 @param request: the request instance
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
262 """
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
263 raise NotImplementedError
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
264
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
265 def set(self, request, session_id, expires):
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
266 """
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
267 Set a persistent ID for the request to be returned by the
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
268 user agent.
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
269
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
270 @param request: the request instance
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
271 @param session_id: the ID for this session
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
272 @param expires: expiry date/time in unix seconds (cf. time.time())
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
273 """
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
274 raise NotImplementedError
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
275
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
276 def generate_new_id(self, request):
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
277 """
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
278 Generate a new unique ID.
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
279
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
280 @param request: the request instance
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
281 """
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
282 return random_string(self._SESSION_NAME_LEN, self._SESSION_NAME_CHARS)
2009
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
283
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
284
2912
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
285
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
286 class MoinCookieSessionIDHandler(SessionIDHandler):
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
287 def __init__(self, cookie_name='MOIN_SESSION'):
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
288 SessionIDHandler.__init__(self)
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
289 self.cookie_name = cookie_name
2919
4cd85d841bbd Remove trailing whitespace.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2913
diff changeset
290
2912
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
291 def _make_cookie(self, request, cookie_name, cookie_string, maxage, expires):
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
292 """ create an appropriate cookie """
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
293 cookie = Cookie.SimpleCookie()
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
294 cfg = request.cfg
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
295 cookie[cookie_name] = cookie_string
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
296 cookie[cookie_name]['max-age'] = maxage
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
297 if cfg.cookie_domain:
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
298 cookie[cookie_name]['domain'] = cfg.cookie_domain
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
299 if cfg.cookie_path:
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
300 cookie[cookie_name]['path'] = cfg.cookie_path
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
301 else:
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
302 path = request.getScriptname()
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
303 if not path:
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
304 path = '/'
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
305 cookie[cookie_name]['path'] = path
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
306 # Set expires for older clients
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
307 cookie[cookie_name]['expires'] = request.httpDate(when=expires, rfc='850')
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
308 return cookie.output()
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
309
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
310 def _set_cookie(self, request, cookie_string, expires):
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
311 """ Set cookie, raw helper. """
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
312 lifetime = expires - time.time()
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
313 cookie = self._make_cookie(request, self.cookie_name, cookie_string,
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
314 lifetime, expires)
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
315 # Set cookie
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
316 request.setHttpHeader(cookie)
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
317 # IMPORTANT: Prevent caching of current page and cookie
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
318 request.disableHttpCaching()
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
319
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
320 def set(self, request, session_name, expires):
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
321 """ Set moin_session cookie """
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
322 self._set_cookie(request, session_name, expires)
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
323
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
324 def get(self, request):
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
325 session_name = None
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
326 if request.cookie and self.cookie_name in request.cookie:
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
327 session_name = request.cookie[self.cookie_name].value
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
328 session_name = ''.join([c for c in session_name
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
329 if c in self._SESSION_NAME_CHARS])
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
330 session_name = session_name[:self._SESSION_NAME_LEN]
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
331 return session_name
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
332
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
333
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
334 def _get_anon_session_lifetime(request):
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
335 if hasattr(request.cfg, 'anonymous_session_lifetime'):
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
336 return request.cfg.anonymous_session_lifetime * 3600
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
337 return 0
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
338
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
339 def _get_session_lifetime(request, user_obj):
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
340 """ Get session lifetime for the user object user_obj """
2009
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
341 lifetime = int(request.cfg.cookie_lifetime) * 3600
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
342 forever = 10 * 365 * 24 * 3600 # 10 years
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
343 if not lifetime:
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
344 return forever
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
345 elif lifetime > 0:
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
346 if user_obj.remember_me:
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
347 return forever
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
348 return lifetime
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
349 elif lifetime < 0:
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
350 return -lifetime
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
351 return lifetime
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
352
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
353
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
354 class DefaultSessionHandler(SessionHandler):
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
355 """MoinMoin default session handler
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
356
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
357 This session handler uses the MOIN_SESSION cookie and a configurable
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
358 session data class.
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
359 """
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
360 def __init__(self, dataclass=CacheSessionData):
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
361 """DefaultSessionHandler constructor
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
362
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
363 @param dataclass: class derived from DefaultSessionData or a callable
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
364 that takes parameters (request, name, expires)
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
365 and returns a DefaultSessionData instance.
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
366 """
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
367 SessionHandler.__init__(self)
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
368 self.dataclass = dataclass
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
369
2912
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
370 def start(self, request, session_id_handler):
2009
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
371 user_obj = None
2912
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
372 session_name = session_id_handler.get(request)
2009
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
373 if session_name:
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
374 sessiondata = self.dataclass(request, session_name)
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
375 sessiondata.is_new = False
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
376 sessiondata.is_stored = True
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
377 request.session = sessiondata
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
378 if 'user.id' in sessiondata:
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
379 uid = sessiondata['user.id']
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
380 method = sessiondata['user.auth_method']
2019
149573c7ecaf session must store user.auth_attribs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2009
diff changeset
381 attribs = sessiondata['user.auth_attribs']
2009
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
382 # Only allow valid methods that are still in the auth list.
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
383 # This is necessary to kick out clients who authenticated in
2905
cd9be78f15db remove a stray # in a comment
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2031
diff changeset
384 # the past with a method that was removed from the auth list!
2009
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
385 if method:
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
386 for auth in request.cfg.auth:
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
387 if auth.name == method:
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
388 user_obj = User(request, id=uid,
2019
149573c7ecaf session must store user.auth_attribs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2009
diff changeset
389 auth_method=method,
149573c7ecaf session must store user.auth_attribs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2009
diff changeset
390 auth_attribs=attribs)
2009
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
391 if user_obj:
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
392 sessiondata.is_stored = True
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
393 else:
2912
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
394 store = hasattr(request.cfg, 'anonymous_session_lifetime')
2009
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
395 sessiondata.is_stored = store
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
396 else:
2912
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
397 session_name = session_id_handler.generate_new_id(request)
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
398 store = hasattr(request.cfg, 'anonymous_session_lifetime')
2009
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
399 sessiondata = self.dataclass(request, session_name)
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
400 sessiondata.is_new = True
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
401 sessiondata.is_stored = store
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
402 request.session = sessiondata
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
403 return user_obj
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
404
2912
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
405 def after_auth(self, request, session_id_handler, user_obj):
2009
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
406 session = request.session
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
407 if user_obj and user_obj.valid:
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
408 session['user.id'] = user_obj.id
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
409 session['user.auth_method'] = user_obj.auth_method
2019
149573c7ecaf session must store user.auth_attribs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2009
diff changeset
410 session['user.auth_attribs'] = user_obj.auth_attribs
2912
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
411 lifetime = _get_session_lifetime(request, user_obj)
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
412 expires = time.time() + lifetime
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
413 session_id_handler.set(request, session.name, expires)
2913
99d5fca40c1e fix session expiry update bug
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2912
diff changeset
414 request.session.set_expiry(expires)
2009
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
415 else:
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
416 if 'user.id' in session:
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
417 session.delete()
2912
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
418 lifetime = _get_anon_session_lifetime(request)
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
419 if lifetime:
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
420 expires = time.time() + lifetime
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
421 session_id_handler.set(request, session.name, expires)
2913
99d5fca40c1e fix session expiry update bug
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2912
diff changeset
422 request.session.set_expiry(expires)
2912
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
423 else:
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
424 session.delete()
2009
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
425
2912
4be125bafc8a session handling: separate out an extra dimension for persistent IDs
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2911
diff changeset
426 def finish(self, request, session_id_handler, user_obj):
2009
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
427 # every once a while, clean up deleted sessions:
1b14cc05a54a refactor authentication and split out session handling
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
428 if random.randint(0, 999) == 0:
2906
aaa550567872 integrate DefaultSessionHandler cleanup into DefaultSessionData
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2905
diff changeset
429 self.dataclass.cleanup(request)