author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 24 Jul 2009 17:47:45 +0200 | |
changeset 2492 | c51be1cf8317 |
parent 1977 | 606923dff11b |
child 2706 | 09baf5175196 |
permissions | -rw-r--r-- |
0 | 1 |
"""web session component: by dfault the session is actually the db connection |
2 |
object :/ |
|
3 |
||
4 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
5 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 6 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
7 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 8 |
""" |
9 |
__docformat__ = "restructuredtext en" |
|
10 |
||
1133 | 11 |
from cubicweb.web import InvalidSession |
0 | 12 |
from cubicweb.web.application import AbstractSessionManager |
13 |
||
14 |
||
15 |
class InMemoryRepositorySessionManager(AbstractSessionManager): |
|
16 |
"""manage session data associated to a session identifier""" |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
17 |
|
0 | 18 |
def __init__(self): |
19 |
AbstractSessionManager.__init__(self) |
|
20 |
# XXX require a RepositoryAuthenticationManager which violates |
|
21 |
# authenticate interface by returning a session instead of a user |
|
22 |
#assert isinstance(self.authmanager, RepositoryAuthenticationManager) |
|
23 |
self._sessions = {} |
|
24 |
||
25 |
def current_sessions(self): |
|
26 |
return self._sessions.values() |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
27 |
|
0 | 28 |
def get_session(self, req, sessionid): |
29 |
"""return existing session for the given session identifier""" |
|
30 |
if not sessionid in self._sessions: |
|
31 |
raise InvalidSession() |
|
32 |
session = self._sessions[sessionid] |
|
33 |
if self.has_expired(session): |
|
34 |
self.close_session(session) |
|
35 |
raise InvalidSession() |
|
36 |
# give an opportunity to auth manager to hijack the session |
|
37 |
# (necessary with the RepositoryAuthenticationManager in case |
|
38 |
# the connection to the repository has expired) |
|
39 |
try: |
|
40 |
session = self.authmanager.validate_session(req, session) |
|
41 |
# necessary in case session has been hijacked |
|
42 |
self._sessions[session.sessionid] = session |
|
43 |
except InvalidSession: |
|
44 |
# invalid session |
|
45 |
del self._sessions[sessionid] |
|
46 |
raise |
|
47 |
return session |
|
48 |
||
49 |
def open_session(self, req): |
|
50 |
"""open and return a new session for the given request |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
51 |
|
0 | 52 |
:raise ExplicitLogin: if authentication is required |
53 |
""" |
|
54 |
session = self.authmanager.authenticate(req) |
|
55 |
self._sessions[session.sessionid] = session |
|
56 |
return session |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1133
diff
changeset
|
57 |
|
0 | 58 |
def close_session(self, session): |
59 |
"""close session on logout or on invalid session detected (expired out, |
|
60 |
corrupted...) |
|
61 |
""" |
|
62 |
self.info('closing http session %s' % session) |
|
63 |
del self._sessions[session.sessionid] |
|
64 |
try: |
|
65 |
session.close() |
|
66 |
except: |
|
67 |
# already closed, may occurs if the repository session expired but |
|
68 |
# not the web session |
|
69 |
pass |