annotate MoinMoin/web/session.py @ 5548:a42e6b2cd528

sessions: implant code from werkzeug 0.6, fix it
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 20 Feb 2010 02:53:25 +0100
parents be7c57d8e2a3
children 0dea6dbebafb
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 sys, os
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
15 from os import path
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
16 import time
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
17 import tempfile
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
18 import re
5468
be7c57d8e2a3 fix another werkzeug session problem (details below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5448
diff changeset
19 try:
be7c57d8e2a3 fix another werkzeug session problem (details below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5448
diff changeset
20 from cPickle import load, dump, HIGHEST_PROTOCOL
be7c57d8e2a3 fix another werkzeug session problem (details below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5448
diff changeset
21 except ImportError:
be7c57d8e2a3 fix another werkzeug session problem (details below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5448
diff changeset
22 from pickle import load, dump, HIGHEST_PROTOCOL
4188
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
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 from werkzeug.contrib.sessions import SessionStore, ModificationTrackingDict
4188
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
25
5468
be7c57d8e2a3 fix another werkzeug session problem (details below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5448
diff changeset
26 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
27 from MoinMoin.util import filesys
4188
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
28
4193
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4190
diff changeset
29 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
30 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
31
5548
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
32
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
33 # start copy from werkzeug 0.6 - directly import this, if we require >= 0.6:
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
34
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
35 class Session(ModificationTrackingDict):
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
36 """Subclass of a dict that keeps track of direct object changes. Changes
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
37 in mutable structures are not tracked, for those you have to set
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
38 `modified` to `True` by hand.
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
39 """
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
40 __slots__ = ModificationTrackingDict.__slots__ + ('sid', 'new')
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
41
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
42 def __init__(self, data, sid, new=False):
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
43 ModificationTrackingDict.__init__(self, data)
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
44 self.sid = sid
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
45 self.new = new
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
46
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
47 @property
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
48 def should_save(self):
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
49 """True if the session should be saved.
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
50
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
51 .. versionchanged:: 0.6
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
52 By default the session is now only saved if the session is
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
53 modified, not if it is new like it was before.
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
54 """
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
55 return self.modified
4188
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
56
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
57 def __repr__(self):
fed925dfdc0d improve moin's session handling / fix werkzeug by subclassing (see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5433
diff changeset
58 return '<%s %s %s%s>' % (
fed925dfdc0d improve moin's session handling / fix werkzeug by subclassing (see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5433
diff changeset
59 self.__class__.__name__,
5548
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
60 self.sid,
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
61 dict.__repr__(self),
fed925dfdc0d improve moin's session handling / fix werkzeug by subclassing (see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5433
diff changeset
62 self.should_save and '*' 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
63 )
fed925dfdc0d improve moin's session handling / fix werkzeug by subclassing (see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5433
diff changeset
64
fed925dfdc0d improve moin's session handling / fix werkzeug by subclassing (see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5433
diff changeset
65
5548
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
66 #: used for temporary files by the filesystem session store
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
67 _fs_transaction_suffix = '.__wz_sess'
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
68
fed925dfdc0d improve moin's session handling / fix werkzeug by subclassing (see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5433
diff changeset
69
5548
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
70 class FilesystemSessionStore(SessionStore):
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
71 """Simple example session store that saves sessions in the filesystem like
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
72 PHP does.
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
73
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
74 .. versionchanged:: 0.6
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
75 `renew_missing` was added. Previously this was considered `True`,
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
76 now the default changed to `False` and it can be explicitly
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
77 deactivated.
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
78
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
79 :param path: the path to the folder used for storing the sessions.
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
80 If not provided the default temporary directory is used.
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
81 :param filename_template: a string template used to give the session
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
82 a filename. ``%s`` is replaced with the
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
83 session id.
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
84 :param session_class: The session class to use. Defaults to
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
85 :class:`Session`.
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
86 :param renew_missing: set to `True` if you want the store to
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
87 give the user a new sid if the session was
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
88 not yet saved.
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
89 """
5548
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
90
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
91 def __init__(self, path=None, filename_template='werkzeug_%s.sess',
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
92 session_class=None, renew_missing=False, mode=0644):
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
93 SessionStore.__init__(self, session_class)
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
94 if path is None:
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
95 path = gettempdir()
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
96 self.path = path
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
97 if isinstance(filename_template, unicode):
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
98 filename_template = filename_template.encode(
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
99 sys.getfilesystemencoding() or 'utf-8')
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
100 assert not filename_template.endswith(_fs_transaction_suffix), \
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
101 'filename templates may not end with %s' % _fs_transaction_suffix
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
102 self.filename_template = filename_template
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
103 self.renew_missing = renew_missing
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
104 self.mode = mode
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
105
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
106 def get_session_filename(self, sid):
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
107 if isinstance(sid, unicode):
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
108 sid = sid.encode('utf-8')
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
109 return path.join(self.path, self.filename_template % sid)
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
110
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
111 def save(self, session):
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
112 def _dump(filename):
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
113 f = file(filename, 'wb')
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
114 try:
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
115 dump(dict(session), f, HIGHEST_PROTOCOL)
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
116 finally:
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
117 f.close()
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
118 fn = self.get_session_filename(session.sid)
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
119 if os.name == 'posix':
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
120 td, tmp = tempfile.mkstemp(suffix=_fs_transaction_suffix,
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
121 dir=self.path)
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
122 _dump(tmp)
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
123 try:
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
124 os.rename(tmp, fn)
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
125 except (IOError, OSError):
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
126 pass
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
127 os.chmod(fn, self.mode)
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
128 else:
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
129 _dump(fn)
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
130 try:
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
131 os.chmod(fn, self.mode)
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
132 except OSError:
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
133 # maybe some platforms fail here, have not found
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
134 # any that do thought.
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
135 pass
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
136
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
137 def delete(self, session):
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
138 fn = self.get_session_filename(session.sid)
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
139 try:
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
140 os.unlink(fn)
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
141 except OSError:
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
142 pass
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
143
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
144 def get(self, sid):
fed925dfdc0d improve moin's session handling / fix werkzeug by subclassing (see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5433
diff changeset
145 if not self.is_valid_key(sid):
fed925dfdc0d improve moin's session handling / fix werkzeug by subclassing (see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5433
diff changeset
146 return self.new()
fed925dfdc0d improve moin's session handling / fix werkzeug by subclassing (see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5433
diff changeset
147 try:
5548
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
148 f = open(self.get_session_filename(sid), 'rb')
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
149 except IOError:
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
150 if self.renew_missing:
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
151 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
152 data = {}
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
153 else:
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
154 try:
5548
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
155 try:
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
156 data = load(f)
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
157 except Exception:
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
158 data = {}
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
159 finally:
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
160 f.close()
fed925dfdc0d improve moin's session handling / fix werkzeug by subclassing (see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5433
diff changeset
161 return self.session_class(data, sid, False)
fed925dfdc0d improve moin's session handling / fix werkzeug by subclassing (see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5433
diff changeset
162
5548
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
163 def list(self):
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
164 """Lists all sessions in the store.
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
165
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
166 .. versionadded:: 0.6
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
167 """
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
168 before, after = self.filename_template.split('%s', 1)
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
169 filename_re = re.compile(r'%s(.{5,})%s$' % (re.escape(before),
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
170 re.escape(after)))
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
171 result = []
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
172 for filename in os.listdir(self.path):
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
173 #: this is a session that is still being saved.
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
174 if filename.endswith(_fs_transaction_suffix):
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
175 continue
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
176 match = filename_re.match(filename)
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
177 if match is not None:
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
178 result.append(match.group(1))
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
179 return result
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
180
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
181 # end copy of werkzeug 0.6 code
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
182
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
183
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
184 class FixedFilesystemSessionStore(FilesystemSessionStore):
5448
5518d41fc686 move SID listing functionality to FixedFilesystemSessionStore
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5447
diff changeset
185 """
5468
be7c57d8e2a3 fix another werkzeug session problem (details below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5448
diff changeset
186 Problem: werkzeug 0.5 just directly and non-atomically writes to the session
be7c57d8e2a3 fix another werkzeug session problem (details below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5448
diff changeset
187 file when using save(). If another process or thread uses get() after
be7c57d8e2a3 fix another werkzeug session problem (details below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5448
diff changeset
188 save() opened the file for writing, it will get 0 bytes session content,
be7c57d8e2a3 fix another werkzeug session problem (details below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5448
diff changeset
189 because open(..., "wb") truncated the file already.
5548
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
190
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
191 werkzeug 0.6 save() is still broken: it reopens the file and does not
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
192 use the fd given by mkstemp. It still fails in the same way on win32
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
193 as 0.5 did for both posix and win32 (see above).
5468
be7c57d8e2a3 fix another werkzeug session problem (details below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5448
diff changeset
194 """
be7c57d8e2a3 fix another werkzeug session problem (details below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5448
diff changeset
195 def save(self, session):
5548
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
196 fd, temp_fname = tempfile.mkstemp(suffix=_fs_transaction_suffix, dir=self.path)
5468
be7c57d8e2a3 fix another werkzeug session problem (details below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5448
diff changeset
197 f = os.fdopen(fd, 'wb')
be7c57d8e2a3 fix another werkzeug session problem (details below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5448
diff changeset
198 try:
be7c57d8e2a3 fix another werkzeug session problem (details below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5448
diff changeset
199 dump(dict(session), f, HIGHEST_PROTOCOL)
be7c57d8e2a3 fix another werkzeug session problem (details below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5448
diff changeset
200 finally:
be7c57d8e2a3 fix another werkzeug session problem (details below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5448
diff changeset
201 f.close()
5548
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
202 filesys.chmod(temp_fname, self.mode) # relax restrictive mode from mkstemp
5468
be7c57d8e2a3 fix another werkzeug session problem (details below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5448
diff changeset
203 fname = self.get_session_filename(session.sid)
5548
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
204 filesys.rename(temp_fname, fname) # atomic (posix) or quick (win32)
5468
be7c57d8e2a3 fix another werkzeug session problem (details below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5448
diff changeset
205
be7c57d8e2a3 fix another werkzeug session problem (details below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5448
diff changeset
206 """
5548
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
207 Problem: werkzeug 0.6 uses inconsistent encoding for template and filename
5448
5518d41fc686 move SID listing functionality to FixedFilesystemSessionStore
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5447
diff changeset
208 """
5548
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
209 def _encode_fs(self, name): # TODO: call this from FilesystemSessionStore.__init__
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
210 if isinstance(name, unicode):
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
211 name = name.encode(sys.getfilesystemencoding() or 'utf-8')
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
212 return name
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
213
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
214 def get_session_filename(self, sid):
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
215 sid = self._encode_fs(sid)
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
216 return path.join(self.path, self.filename_template % sid)
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
217
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
218
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
219 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
220 """ Compatibility interface to Werkzeug-sessions for old Moin-code.
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
221
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
222 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
223 """
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
224 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
225 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
226 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
227 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
228
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
229
4211
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
230 class SessionService(object):
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
231 """
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
232 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
233 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
234 """
4529
002c21b10561 fix / finish xmlrpc auth token (session) code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4317
diff changeset
235 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
236 """ 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
237 raise NotImplementedError
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
238
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
239 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
240 """ 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
241 raise NotImplementedError
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
242
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
243 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
244 """
4317
371bf2615ea1 Code review: added some more documentation
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4278
diff changeset
245 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
246 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
247 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
248 """
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
249 raise NotImplementedError
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
250
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
251 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
252 """
bd55be7c3886 store expiry into sessions, moin maint cleansessions script, session enumeration support
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5414
diff changeset
253 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
254 """
bd55be7c3886 store expiry into sessions, moin maint cleansessions script, session enumeration support
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5414
diff changeset
255 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
256
bd55be7c3886 store expiry into sessions, moin maint cleansessions script, session enumeration support
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5414
diff changeset
257
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
258 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
259 """ 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
260 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
261 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
262 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
263 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
264 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
265 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
266 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
267 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
268
5ee532645444 web.session: bug fix for MoinMoinBugs/1.9_remember_me_broken.
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 4704
diff changeset
269 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
270 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
271 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
272
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
273 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
274 """
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
275 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
276 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
277 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
278
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
279 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
280 -----
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
281 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
282 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
283 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
284
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
285 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
286 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
287
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
288 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
289 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
290
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
291 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
292 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
293 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
294 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
295
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
296 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
297 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
298 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
299 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
300 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
301
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
302 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
303 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
304 """
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
305 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
306 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
307 # 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
308 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
309 # 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
310 ('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
311 ]
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
312 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
313
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
314 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
315 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
316
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
317 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
318
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
319
4211
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
320 class FileSessionService(SessionService):
4188
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
321 """
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
322 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
323 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
324 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
325 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
326 inherited from the builtin `dict`)
4188
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
327 """
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
328 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
329 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
330
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
331 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
332 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
333 try:
437558fff184 Fix FileSessionService - use session_dir from CURRENT request.cfg.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4706
diff changeset
334 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
335 except OSError:
437558fff184 Fix FileSessionService - use session_dir from CURRENT request.cfg.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4706
diff changeset
336 pass
5548
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
337 return FixedFilesystemSessionStore(path=path, filename_template='%s',
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
338 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
339
4529
002c21b10561 fix / finish xmlrpc auth token (session) code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4317
diff changeset
340 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
341 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
342 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
343 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
344 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
345 store = self._store_get(request)
4188
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
346 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
347 session = store.new()
4188
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
348 else:
5433
c800980890aa Fix for MoinMoinBugs/WerkzeugErrorWithNonAsciiPath ? making sid always ascii
Eugene Syromyatnikov <evgsyr@gmail.com>
parents: 5425
diff changeset
349 session = store.get(str(sid))
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
350 logging.debug("get_session returns session %r" % session)
4188
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
351 return session
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
352
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
353 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
354 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
355 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
356
4195
2d8a9f98382b Added 'destroy_session' to session service api
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4193
diff changeset
357 def destroy_session(self, request, session):
2d8a9f98382b Added 'destroy_session' to session service api
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4193
diff changeset
358 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
359 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
360 store.delete(session)
4195
2d8a9f98382b Added 'destroy_session' to session service api
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4193
diff changeset
361
4188
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
362 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
363 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
364 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
365 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
366 else:
0ac99fdbe65d fixed suid functionality, compute cfg.auth_methods only once
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4529
diff changeset
367 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
368 setuid = None
0ac99fdbe65d fixed suid functionality, compute cfg.auth_methods only once
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4529
diff changeset
369 logging.debug("finalize userobj = %r, setuid = %r" % (userobj, setuid))
4628
3c6980b5e938 fix new session code. remove old session code. details below.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4607
diff changeset
370 cfg = request.cfg
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
371 # we use different cookie names for different 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
372 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
373 # we always use path='/' except if explicitly overridden by configuration,
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
374 # which is usually not needed and not recommended:
4226fde63931 generate session cookie names to fix some issues (details see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5371
diff changeset
375 cookie_path = cfg.cookie_path or '/'
4193
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4190
diff changeset
376 if userobj and userobj.valid:
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4190
diff changeset
377 session['user.id'] = userobj.id
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4190
diff changeset
378 session['user.auth_method'] = userobj.auth_method
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4190
diff changeset
379 session['user.auth_attribs'] = userobj.auth_attribs
4530
0ac99fdbe65d fixed suid functionality, compute cfg.auth_methods only once
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4529
diff changeset
380 if setuid:
0ac99fdbe65d fixed suid functionality, compute cfg.auth_methods only once
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4529
diff changeset
381 session['setuid'] = setuid
0ac99fdbe65d fixed suid functionality, compute cfg.auth_methods only once
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4529
diff changeset
382 elif 'setuid' in session:
0ac99fdbe65d fixed suid functionality, compute cfg.auth_methods only once
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4529
diff changeset
383 del session['setuid']
4193
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4190
diff changeset
384 logging.debug("after auth: storing valid user into session: %r" % userobj.name)
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4190
diff changeset
385 else:
4628
3c6980b5e938 fix new session code. remove old session code. details below.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4607
diff changeset
386 logging.debug("after auth: user is invalid")
4193
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4190
diff changeset
387 if 'user.id' in session:
4628
3c6980b5e938 fix new session code. remove old session code. details below.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4607
diff changeset
388 logging.debug("after auth: destroying session: %r" % session)
4195
2d8a9f98382b Added 'destroy_session' to session service api
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4193
diff changeset
389 self.destroy_session(request, session)
4628
3c6980b5e938 fix new session code. remove old session code. details below.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4607
diff changeset
390 logging.debug("after auth: deleting session cookie!")
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
391 request.delete_cookie(cookie_name, path=cookie_path, domain=cfg.cookie_domain)
4193
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4190
diff changeset
392
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
393 cookie_lifetime = _get_session_lifetime(request, userobj)
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
394 if cookie_lifetime:
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
395 cookie_expires = time.time() + cookie_lifetime
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
396 # a secure cookie is not transmitted over unsecure connections:
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
397 cookie_secure = (cfg.cookie_secure or # True means: force secure cookies
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
398 cfg.cookie_secure is None and request.is_secure) # None means: https -> secure cookie
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
399 logging.debug("user: %r, setting session cookie: %r" % (userobj, 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
400 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
401 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
402 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
403 secure=cookie_secure, httponly=cfg.cookie_httponly)
4278
7a6d93a96a98 Fixed: wrong cookie path made session restricted to login-action-path
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4232
diff changeset
404
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
405 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
406 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
407 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
408 # 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
409 and
5548
a42e6b2cd528 sessions: implant code from werkzeug 0.6, fix it
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5468
diff changeset
410 session.should_save): # only if we really have something to save
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
411 # add some info about expiry to the sessions, so we can purge them:
fed925dfdc0d improve moin's session handling / fix werkzeug by subclassing (see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5433
diff changeset
412 session['expires'] = cookie_expires
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
413 # note: currently, every request of a logged-in user will save
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
414 # the session, even when always requesting same page.
fed925dfdc0d improve moin's session handling / fix werkzeug by subclassing (see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5433
diff changeset
415 # No big deal, as we store the trail into session and that
fed925dfdc0d improve moin's session handling / fix werkzeug by subclassing (see below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5433
diff changeset
416 # likely changes with every request anyway.
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
417 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
418 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
419 store.save(session)
4529
002c21b10561 fix / finish xmlrpc auth token (session) code
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4317
diff changeset
420