annotate MoinMoin/web/session.py @ 5802:dba48cb280f9

delete some code we had copied from werkzeug 0.6.1 while we bundled 0.5.1 (it is contained in the bundled werkzeug 0.8.1)
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Thu, 01 Dec 2011 02:05:15 +0100
parents af56baebf4d1
children
rev   line source
4188
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
2 """
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
3 MoinMoin - WSGI session handling
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
4
4317
371bf2615ea1 Code review: added some more documentation
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4278
diff changeset
5 To provide sessions, the MoinMoin WSGI application interacts with an
371bf2615ea1 Code review: added some more documentation
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4278
diff changeset
6 object implementing the `SessionService` API. The interface is quite
371bf2615ea1 Code review: added some more documentation
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4278
diff changeset
7 straight forward. For documentation of the expected methods, refer
371bf2615ea1 Code review: added some more documentation
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4278
diff changeset
8 to the documentation of `SessionService` in this module.
4188
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
9
4628
3c6980b5e938 fix new session code. remove old session code. details below.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4607
diff changeset
10 @copyright: 2008 MoinMoin:FlorianKrupicka,
3c6980b5e938 fix new session code. remove old session code. details below.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4607
diff changeset
11 2009 MoinMoin:ThomasWaldmann
4188
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
12 @license: GNU GPL, see COPYING for details.
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
13 """
5548
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
14 import time
4188
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
15
5802
dba48cb280f9 delete some code we had copied from werkzeug 0.6.1 while we bundled 0.5.1 (it is contained in the bundled werkzeug 0.8.1)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5580
diff changeset
16 from werkzeug.contrib.sessions import Session, FilesystemSessionStore
4188
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
17
5468
be7c57d8e2a3 fix another werkzeug session problem (details below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5448
diff changeset
18 from MoinMoin import config
4531
83666cc9dc31 new cfg.session_dir setting, store sessions into cache_dir/__session__ by default
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4530
diff changeset
19 from MoinMoin.util import filesys
4188
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
20
4193
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4190
diff changeset
21 from MoinMoin import log
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4190
diff changeset
22 logging = log.getLogger(__name__)
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4190
diff changeset
23
5548
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
24
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
25 class MoinSession(Session):
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
26 """ Compatibility interface to Werkzeug-sessions for old Moin-code.
5554
0dea6dbebafb web.session: PEP8 whitespace fix
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5548
diff changeset
27
5548
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
28 is_new is DEPRECATED and will go away soon.
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
29 """
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
30 def _get_is_new(self):
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
31 logging.warning("Deprecated use of MoinSession.is_new, please use .new")
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
32 return self.new
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
33 is_new = property(_get_is_new)
5448
5518d41fc686 move SID listing functionality to FixedFilesystemSessionStore
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5447
diff changeset
34
5447
fed925dfdc0d improve moin's session handling / fix werkzeug by subclassing (see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5433
diff changeset
35
4211
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
36 class SessionService(object):
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
37 """
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
38 A session service returns a session object given a request object and
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
39 provides services like persisting sessions and cleaning up occasionally.
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
40 """
4529
002c21b10561 fix / finish xmlrpc auth token (session) code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4317
diff changeset
41 def get_session(self, request, sid=None):
4211
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
42 """ Return a session object pertaining to the particular request."""
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
43 raise NotImplementedError
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
44
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
45 def destroy_session(self, request, session):
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
46 """ Destroy an existing session (make it unusable). """
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
47 raise NotImplementedError
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
48
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
49 def finalize(self, request, session):
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
50 """
4317
371bf2615ea1 Code review: added some more documentation
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4278
diff changeset
51 If the service needs to do anything to the session and/or request,
371bf2615ea1 Code review: added some more documentation
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4278
diff changeset
52 before it is sent back to the client, he can chose to do so here.
371bf2615ea1 Code review: added some more documentation
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4278
diff changeset
53 Typical examples would be setting cookies for the client.
4211
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
54 """
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
55 raise NotImplementedError
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
56
5425
bd55be7c3886 store expiry into sessions, moin maint cleansessions script, session enumeration support
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5414
diff changeset
57 def get_all_session_ids(self, request):
bd55be7c3886 store expiry into sessions, moin maint cleansessions script, session enumeration support
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5414
diff changeset
58 """
bd55be7c3886 store expiry into sessions, moin maint cleansessions script, session enumeration support
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5414
diff changeset
59 Return a list of all session ids known to the SessionService.
bd55be7c3886 store expiry into sessions, moin maint cleansessions script, session enumeration support
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5414
diff changeset
60 """
bd55be7c3886 store expiry into sessions, moin maint cleansessions script, session enumeration support
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5414
diff changeset
61 raise NotImplementedError
bd55be7c3886 store expiry into sessions, moin maint cleansessions script, session enumeration support
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5414
diff changeset
62
bd55be7c3886 store expiry into sessions, moin maint cleansessions script, session enumeration support
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5414
diff changeset
63
4705
5ee532645444 web.session: bug fix for MoinMoinBugs/1.9_remember_me_broken.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 4704
diff changeset
64 def _get_session_lifetime(request, userobj):
4706
0fd171596794 web.session: PEP8 whitespace fix
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 4705
diff changeset
65 """ Get session lifetime for the user object userobj
0fd171596794 web.session: PEP8 whitespace fix
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 4705
diff changeset
66 Cookie lifetime in hours, can be fractional. First tuple element is for anonymous sessions,
0fd171596794 web.session: PEP8 whitespace fix
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 4705
diff changeset
67 second tuple element is for logged-in sessions. For anonymous sessions,
0fd171596794 web.session: PEP8 whitespace fix
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 4705
diff changeset
68 t=0 means that they are disabled, t>0 means that many hours.
0fd171596794 web.session: PEP8 whitespace fix
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 4705
diff changeset
69 For logged-in sessions, t>0 means that many hours,
0fd171596794 web.session: PEP8 whitespace fix
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 4705
diff changeset
70 or forever if user checked 'remember_me', t<0 means -t hours and
4705
5ee532645444 web.session: bug fix for MoinMoinBugs/1.9_remember_me_broken.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 4704
diff changeset
71 ignore user 'remember_me' setting - you usually don't want to use t=0, it disables logged-in sessions."""
5ee532645444 web.session: bug fix for MoinMoinBugs/1.9_remember_me_broken.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 4704
diff changeset
72 lifetime = int(float(request.cfg.cookie_lifetime[userobj and userobj.valid]) * 3600)
5ee532645444 web.session: bug fix for MoinMoinBugs/1.9_remember_me_broken.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 4704
diff changeset
73 forever = 10 * 365 * 24 * 3600 # 10 years
5ee532645444 web.session: bug fix for MoinMoinBugs/1.9_remember_me_broken.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 4704
diff changeset
74
5ee532645444 web.session: bug fix for MoinMoinBugs/1.9_remember_me_broken.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 4704
diff changeset
75 if userobj and userobj.valid and userobj.remember_me and lifetime > 0:
5ee532645444 web.session: bug fix for MoinMoinBugs/1.9_remember_me_broken.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 4704
diff changeset
76 return forever
5ee532645444 web.session: bug fix for MoinMoinBugs/1.9_remember_me_broken.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 4704
diff changeset
77 return abs(lifetime)
5ee532645444 web.session: bug fix for MoinMoinBugs/1.9_remember_me_broken.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 4704
diff changeset
78
5575
17cf01154e12 sessions: remove code not needed any more due to new werkzeug session code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5572
diff changeset
79
5408
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
80 def get_cookie_name(request, name, usage, software='MOIN'):
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
81 """
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
82 Determine the full cookie name for some software (usually 'MOIN') using
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
83 it for some usage (e.g. 'SESSION') for some wiki (or group of wikis)
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
84 determined by name.
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
85
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
86 Note:
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
87 -----
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
88 We do not use the path=... information in the cookie any more, because it can
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
89 easily cause confusion if there are multiple cookies with same name, but
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
90 different pathes (like e.g. / and /foo).
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
91
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
92 Instead of using the cookie path, we use differently named cookies, so we get
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
93 the right cookie no matter at what URL the wiki currently is "mounted".
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
94
5414
b0c8c2f225f3 Changed default of cfg.cookie_name to use some URL components to make up some name.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5408
diff changeset
95 If name is None, we use some URL components to make up some name.
b0c8c2f225f3 Changed default of cfg.cookie_name to use some URL components to make up some name.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5408
diff changeset
96 For example the cookie name for the default desktop wiki: MOIN_SESSION_8080_ROOT
b0c8c2f225f3 Changed default of cfg.cookie_name to use some URL components to make up some name.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5408
diff changeset
97
b0c8c2f225f3 Changed default of cfg.cookie_name to use some URL components to make up some name.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5408
diff changeset
98 If name is siteidmagic, we just use cfg.siteid, which is unique within a wiki farm
5408
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
99 created by a single farmconfig. If you only run ONE(!) wikiconfig wiki, it
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
100 is also unique, of course, but not if you run multiple wikiconfig wikis under
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
101 same domain.
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
102
5414
b0c8c2f225f3 Changed default of cfg.cookie_name to use some URL components to make up some name.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5408
diff changeset
103 If name is not None (and not 'siteidmagic'), we just use the given name (you
5408
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
104 want to use that to share stuff between several wikis - just give same name
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
105 and it will use the same cookie. same thing if you don't want to share, just
5414
b0c8c2f225f3 Changed default of cfg.cookie_name to use some URL components to make up some name.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5408
diff changeset
106 give a different name then [e.g. if cfg.siteid or 'siteidmagic' doesn't work
5408
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
107 for you]).
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
108
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
109 Moving a wiki to a different URL will break all sessions. Exchanging URLs
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
110 of wikis might lead to confusion (requiring the client to purge the cookies).
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
111 """
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
112 if name is None:
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
113 url_components = [
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
114 # cookies do not store the port, thus we add it to the cookie name:
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
115 request.environ['SERVER_PORT'],
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
116 # we always store path=/ into cookie, thus we add the path to the name:
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
117 ('ROOT' + request.script_root).replace('/', '_'),
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
118 ]
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
119 name = '_'.join(url_components)
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
120
5414
b0c8c2f225f3 Changed default of cfg.cookie_name to use some URL components to make up some name.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5408
diff changeset
121 elif name is 'siteidmagic':
b0c8c2f225f3 Changed default of cfg.cookie_name to use some URL components to make up some name.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5408
diff changeset
122 name = request.cfg.siteid # == config name, unique per farm
b0c8c2f225f3 Changed default of cfg.cookie_name to use some URL components to make up some name.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 5408
diff changeset
123
5408
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
124 return "%s_%s_%s" % (software, usage, name)
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
125
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
126
4211
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
127 class FileSessionService(SessionService):
4188
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
128 """
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
129 This sample session service stores session information in a temporary
4317
371bf2615ea1 Code review: added some more documentation
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4278
diff changeset
130 directory and identifies the session via a cookie in the request/response
371bf2615ea1 Code review: added some more documentation
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4278
diff changeset
131 cycle. It is based on werkzeug's FilesystemSessionStore, that implements
371bf2615ea1 Code review: added some more documentation
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4278
diff changeset
132 the whole logic for creating the actual session objects (which are
371bf2615ea1 Code review: added some more documentation
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4278
diff changeset
133 inherited from the builtin `dict`)
4188
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
134 """
5408
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
135 def __init__(self, cookie_usage='SESSION'):
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
136 self.cookie_usage = cookie_usage
4531
83666cc9dc31 new cfg.session_dir setting, store sessions into cache_dir/__session__ by default
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4530
diff changeset
137
83666cc9dc31 new cfg.session_dir setting, store sessions into cache_dir/__session__ by default
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4530
diff changeset
138 def _store_get(self, request):
5371
437558fff184 Fix FileSessionService - use session_dir from CURRENT request.cfg.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4706
diff changeset
139 path = request.cfg.session_dir
437558fff184 Fix FileSessionService - use session_dir from CURRENT request.cfg.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4706
diff changeset
140 try:
437558fff184 Fix FileSessionService - use session_dir from CURRENT request.cfg.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4706
diff changeset
141 filesys.mkdir(path)
437558fff184 Fix FileSessionService - use session_dir from CURRENT request.cfg.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4706
diff changeset
142 except OSError:
437558fff184 Fix FileSessionService - use session_dir from CURRENT request.cfg.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4706
diff changeset
143 pass
5575
17cf01154e12 sessions: remove code not needed any more due to new werkzeug session code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5572
diff changeset
144 return FilesystemSessionStore(path=path, filename_template='%s',
17cf01154e12 sessions: remove code not needed any more due to new werkzeug session code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5572
diff changeset
145 session_class=MoinSession, mode=0666 & config.umask)
4198
988b2b672219 Fix tests: source code (indentation, whitespace) mostly
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4195
diff changeset
146
4529
002c21b10561 fix / finish xmlrpc auth token (session) code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4317
diff changeset
147 def get_session(self, request, sid=None):
002c21b10561 fix / finish xmlrpc auth token (session) code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4317
diff changeset
148 if sid is None:
5408
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
149 cookie_name = get_cookie_name(request, name=request.cfg.cookie_name, usage=self.cookie_usage)
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
150 sid = request.cookies.get(cookie_name, None)
5447
fed925dfdc0d improve moin's session handling / fix werkzeug by subclassing (see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5433
diff changeset
151 logging.debug("get_session for sid %r" % sid)
4531
83666cc9dc31 new cfg.session_dir setting, store sessions into cache_dir/__session__ by default
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4530
diff changeset
152 store = self._store_get(request)
4188
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
153 if sid is None:
4531
83666cc9dc31 new cfg.session_dir setting, store sessions into cache_dir/__session__ by default
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4530
diff changeset
154 session = store.new()
4188
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
155 else:
5575
17cf01154e12 sessions: remove code not needed any more due to new werkzeug session code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5572
diff changeset
156 session = store.get(sid)
5580
af56baebf4d1 sessions: fix expiry check
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5579
diff changeset
157 expiry = session.get('expires')
af56baebf4d1 sessions: fix expiry check
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5579
diff changeset
158 if expiry is not None:
af56baebf4d1 sessions: fix expiry check
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5579
diff changeset
159 now = time.time()
af56baebf4d1 sessions: fix expiry check
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5579
diff changeset
160 if expiry < now:
af56baebf4d1 sessions: fix expiry check
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5579
diff changeset
161 # the browser should've killed that cookie already.
af56baebf4d1 sessions: fix expiry check
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5579
diff changeset
162 # clock not in sync? trying to cheat?
af56baebf4d1 sessions: fix expiry check
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5579
diff changeset
163 logging.debug("session has expired (expiry: %r now: %r)" % (expiry, now))
af56baebf4d1 sessions: fix expiry check
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5579
diff changeset
164 self.destroy_session(request, session)
af56baebf4d1 sessions: fix expiry check
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5579
diff changeset
165 session = store.new()
5447
fed925dfdc0d improve moin's session handling / fix werkzeug by subclassing (see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5433
diff changeset
166 logging.debug("get_session returns session %r" % session)
4188
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
167 return session
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
168
5425
bd55be7c3886 store expiry into sessions, moin maint cleansessions script, session enumeration support
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5414
diff changeset
169 def get_all_session_ids(self, request):
5448
5518d41fc686 move SID listing functionality to FixedFilesystemSessionStore
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5447
diff changeset
170 store = self._store_get(request)
5548
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
171 return store.list()
5425
bd55be7c3886 store expiry into sessions, moin maint cleansessions script, session enumeration support
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5414
diff changeset
172
4195
2d8a9f98382b Added 'destroy_session' to session service api
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4193
diff changeset
173 def destroy_session(self, request, session):
2d8a9f98382b Added 'destroy_session' to session service api
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4193
diff changeset
174 session.clear()
4531
83666cc9dc31 new cfg.session_dir setting, store sessions into cache_dir/__session__ by default
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4530
diff changeset
175 store = self._store_get(request)
83666cc9dc31 new cfg.session_dir setting, store sessions into cache_dir/__session__ by default
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4530
diff changeset
176 store.delete(session)
4195
2d8a9f98382b Added 'destroy_session' to session service api
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4193
diff changeset
177
4188
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
178 def finalize(self, request, session):
4530
0ac99fdbe65d fixed suid functionality, compute cfg.auth_methods only once
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4529
diff changeset
179 if request.user.auth_method == 'setuid':
0ac99fdbe65d fixed suid functionality, compute cfg.auth_methods only once
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4529
diff changeset
180 userobj = request._setuid_real_user
0ac99fdbe65d fixed suid functionality, compute cfg.auth_methods only once
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4529
diff changeset
181 setuid = request.user.id
0ac99fdbe65d fixed suid functionality, compute cfg.auth_methods only once
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4529
diff changeset
182 else:
0ac99fdbe65d fixed suid functionality, compute cfg.auth_methods only once
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4529
diff changeset
183 userobj = request.user
0ac99fdbe65d fixed suid functionality, compute cfg.auth_methods only once
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4529
diff changeset
184 setuid = None
0ac99fdbe65d fixed suid functionality, compute cfg.auth_methods only once
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4529
diff changeset
185 logging.debug("finalize userobj = %r, setuid = %r" % (userobj, setuid))
4193
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4190
diff changeset
186
5579
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
187 cfg = request.cfg
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
188 # we use different cookie names for different wikis:
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
189 cookie_name = get_cookie_name(request, name=cfg.cookie_name, usage=self.cookie_usage)
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
190 # we always use path='/' except if explicitly overridden by configuration,
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
191 # which is usually not needed and not recommended:
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
192 cookie_path = cfg.cookie_path or '/'
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
193 # a secure cookie is not transmitted over unsecure connections:
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
194 cookie_secure = (cfg.cookie_secure or # True means: force secure cookies
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
195 cfg.cookie_secure is None and request.is_secure) # None means: https -> secure cookie
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
196
4705
5ee532645444 web.session: bug fix for MoinMoinBugs/1.9_remember_me_broken.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 4704
diff changeset
197 cookie_lifetime = _get_session_lifetime(request, userobj)
5579
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
198 # we use 60s granularity, so we don't trigger session storage updates too often
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
199 cookie_expires = int(time.time() / 60) * 60 + cookie_lifetime
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
200 # when transiting logged-in -> logged out we want to kill the session
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
201 # to protect privacy (do not show trail, even if anon sessions are on)
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
202 kill_session = not userobj.valid and 'user.id' in session
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
203 if kill_session:
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
204 logging.debug("logout detected, will kill session")
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
205 if cookie_lifetime and not kill_session:
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
206 logging.debug("setting session cookie: %r" % (session.sid, ))
5408
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
207 request.set_cookie(cookie_name, session.sid,
4704
621d9dcc6b00 web.session: bug fix for MoinMoinBugs/1.9_session_lifetime_for_user. We do refresh the session cookie now on each request.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 4637
diff changeset
208 max_age=cookie_lifetime, expires=cookie_expires,
5408
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
209 path=cookie_path, domain=cfg.cookie_domain,
4704
621d9dcc6b00 web.session: bug fix for MoinMoinBugs/1.9_session_lifetime_for_user. We do refresh the session cookie now on each request.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 4637
diff changeset
210 secure=cookie_secure, httponly=cfg.cookie_httponly)
5579
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
211 elif not session.new:
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
212 # we still got a cookie, but we don't want it. kill it.
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
213 logging.debug("deleting session cookie!")
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
214 request.delete_cookie(cookie_name, path=cookie_path, domain=cfg.cookie_domain)
4278
7a6d93a96a98 Fixed: wrong cookie path made session restricted to login-action-path
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4232
diff changeset
215
5579
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
216 def update_session(key, val):
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
217 """ put key/val into session, avoid writing if it is unchanged """
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
218 try:
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
219 current_val = session[key]
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
220 except KeyError:
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
221 session[key] = val
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
222 else:
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
223 if val != current_val:
5577
092ce221f03a session finalize: refactor, don't save cleared session on logout
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5576
diff changeset
224 session[key] = val
092ce221f03a session finalize: refactor, don't save cleared session on logout
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5576
diff changeset
225
5579
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
226 if not session.new:
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
227 # add some info about expiry to the sessions, so we can purge them.
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
228 # also, make sure we notice server-side if a session is expired, do
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
229 # not rely on the client to expire the cookie.
5576
6b94d284ad11 avoid writing key/value to session dict if value doesn't change
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5575
diff changeset
230 update_session('expires', cookie_expires)
5579
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
231
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
232 if cookie_lifetime and not kill_session:
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
233 # we have set the cookie, now update the session store
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
234
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
235 if userobj.valid:
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
236 # we have a logged-in user
5577
092ce221f03a session finalize: refactor, don't save cleared session on logout
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5576
diff changeset
237 update_session('user.id', userobj.id)
092ce221f03a session finalize: refactor, don't save cleared session on logout
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5576
diff changeset
238 update_session('user.auth_method', userobj.auth_method)
092ce221f03a session finalize: refactor, don't save cleared session on logout
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5576
diff changeset
239 update_session('user.auth_attribs', userobj.auth_attribs)
092ce221f03a session finalize: refactor, don't save cleared session on logout
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5576
diff changeset
240 if setuid:
092ce221f03a session finalize: refactor, don't save cleared session on logout
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5576
diff changeset
241 update_session('setuid', setuid)
092ce221f03a session finalize: refactor, don't save cleared session on logout
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5576
diff changeset
242 elif 'setuid' in session:
092ce221f03a session finalize: refactor, don't save cleared session on logout
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5576
diff changeset
243 del session['setuid']
5579
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
244 logging.debug("storing valid user into session: %r" % userobj.name)
5577
092ce221f03a session finalize: refactor, don't save cleared session on logout
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5576
diff changeset
245 else:
5579
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
246 # no logged-in user (not logged in or just has logged out)
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
247 for key in ['user.id', 'user.auth_method', 'user.auth_attribs',
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
248 'setuid', ]:
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
249 if key in session:
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
250 del session[key]
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
251 logging.debug("no valid user, cleaned user info from session")
5576
6b94d284ad11 avoid writing key/value to session dict if value doesn't change
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5575
diff changeset
252
5447
fed925dfdc0d improve moin's session handling / fix werkzeug by subclassing (see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5433
diff changeset
253 if ((not userobj.valid and not session.new # anon users with a cookie (not first request)
fed925dfdc0d improve moin's session handling / fix werkzeug by subclassing (see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5433
diff changeset
254 or
fed925dfdc0d improve moin's session handling / fix werkzeug by subclassing (see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5433
diff changeset
255 userobj.valid) # logged-in users, even if THIS was the first request (no cookie yet)
fed925dfdc0d improve moin's session handling / fix werkzeug by subclassing (see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5433
diff changeset
256 # XXX if UA doesn't support cookies, this creates 1 session file per request
fed925dfdc0d improve moin's session handling / fix werkzeug by subclassing (see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5433
diff changeset
257 and
5577
092ce221f03a session finalize: refactor, don't save cleared session on logout
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5576
diff changeset
258 session.should_save # only if we really have something (modified) to save
092ce221f03a session finalize: refactor, don't save cleared session on logout
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5576
diff changeset
259 ):
4637
ff5be6bb7a49 only save session data if we also have a cookie establishing a session
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4629
diff changeset
260 store = self._store_get(request)
ff5be6bb7a49 only save session data if we also have a cookie establishing a session
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4629
diff changeset
261 logging.debug("saving session: %r" % session)
ff5be6bb7a49 only save session data if we also have a cookie establishing a session
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4629
diff changeset
262 store.save(session)
5579
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
263 elif not session.new:
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
264 # we killed the cookie (see above), so we can kill the session store, too
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
265 logging.debug("destroying session: %r" % session)
e535351eab08 sessions: fix not being able to log out if anon sessions disabled, check expiry
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5577
diff changeset
266 self.destroy_session(request, session)
4529
002c21b10561 fix / finish xmlrpc auth token (session) code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4317
diff changeset
267