annotate MoinMoin/web/session.py @ 4211:dde44d6e24ae

Removed MoinMoin.web.api, use inheritance and abstract classes
author Florian Krupicka <florian.krupicka@googlemail.com>
date Wed, 09 Jul 2008 21:48:04 +0200
parents 988b2b672219
children 42352c0b2a51
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
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
5 Session handling
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
6
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
7 @copyright: 2008 MoinMoin:FlorianKrupicka
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
8 @license: GNU GPL, see COPYING for details.
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
9 """
4190
a030da8b2ec9 Fixed: missing imports and falsely ported session middleware code
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4188
diff changeset
10 import time
4188
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
11
4190
a030da8b2ec9 Fixed: missing imports and falsely ported session middleware code
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4188
diff changeset
12 from werkzeug.utils import dump_cookie
4188
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
13 from werkzeug.contrib.sessions import FilesystemSessionStore, Session
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
14
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
15 from MoinMoin import caching
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
16
4193
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4190
diff changeset
17 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
18 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
19
4188
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
20 class MoinSession(Session):
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
21 """ Compatibility interface to Werkzeug-sessions for old Moin-code. """
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
22 is_new = property(lambda s: s.new)
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
23 is_stored = property(lambda s: True)
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
24
4211
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
25 class SessionService(object):
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
26 """
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
27 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
28 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
29 """
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
30 def get_session(self, request):
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
31 """ 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
32 raise NotImplementedError
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
33
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
34 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
35 """ 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
36 raise NotImplementedError
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 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
39 """
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
40 Do final modifications to the request and/or session before sending
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
41 headers and body to the cliebt.
dde44d6e24ae Removed MoinMoin.web.api, use inheritance and abstract classes
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4198
diff changeset
42 """
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 class FileSessionService(SessionService):
4188
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
46 """
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
47 This sample session service stores session information in a temporary
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
48 directory and identifis the session via a cookie in the request/response
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
49 cycle.
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
50 """
4198
988b2b672219 Fix tests: source code (indentation, whitespace) mostly
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4195
diff changeset
51
4188
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
52 def __init__(self, cookie_name='MOIN_SESSION'):
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
53 self.store = FilesystemSessionStore(session_class=MoinSession)
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
54 self.cookie_name = cookie_name
4198
988b2b672219 Fix tests: source code (indentation, whitespace) mostly
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4195
diff changeset
55
4188
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
56 def get_session(self, request):
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
57 sid = request.cookies.get(self.cookie_name, None)
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
58 if sid is None:
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
59 session = self.store.new()
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
60 else:
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
61 session = self.store.get(sid)
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
62 return session
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
63
4195
2d8a9f98382b Added 'destroy_session' to session service api
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4193
diff changeset
64 def destroy_session(self, request, session):
2d8a9f98382b Added 'destroy_session' to session service api
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4193
diff changeset
65 session.clear()
2d8a9f98382b Added 'destroy_session' to session service api
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4193
diff changeset
66 self.store.delete(session)
2d8a9f98382b Added 'destroy_session' to session service api
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4193
diff changeset
67 session.modified = session.new = False
2d8a9f98382b Added 'destroy_session' to session service api
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4193
diff changeset
68
4188
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
69 def finalize(self, request, session):
4193
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4190
diff changeset
70 userobj = request.user
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4190
diff changeset
71 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
72 if 'user.id' in session and 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
73 request.cfg.session_service.delete(session)
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4190
diff changeset
74 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
75 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
76 session['user.auth_attribs'] = userobj.auth_attribs
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4190
diff changeset
77 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
78 else:
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4190
diff changeset
79 if 'user.id' in session:
4195
2d8a9f98382b Added 'destroy_session' to session service api
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4193
diff changeset
80 self.destroy_session(request, session)
4193
1e954e802ed2 Start to make auth work again with the new session layer
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4190
diff changeset
81
4188
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
82 if session.should_save:
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
83 self.store.save(session)
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
84 cookie_lifetime = request.cfg.cookie_lifetime * 3600
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
85 cookie_expires = time.time() + cookie_lifetime
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
86 cookie = dump_cookie(self.cookie_name, session.sid,
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
87 cookie_lifetime, cookie_expires,
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
88 request.cfg.cookie_domain,
a2709307da3d New (sample) session service
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
89 request.cfg.cookie_path)
4190
a030da8b2ec9 Fixed: missing imports and falsely ported session middleware code
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4188
diff changeset
90 request.headers.add('Set-Cookie', cookie)