author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 23 Sep 2009 12:23:14 +0200 | |
changeset 3417 | fb17a54b358c |
parent 2887 | 1282dc6525c5 |
child 3451 | 6b46d73823f5 |
permissions | -rw-r--r-- |
0 | 1 |
"""user authentication component |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1690
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
: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:
1690
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
10 |
from logilab.common.decorators import clear_cache |
|
11 |
||
12 |
from cubicweb import AuthenticationError, BadConnectionId |
|
13 |
from cubicweb.dbapi import repo_connect, ConnectionProperties |
|
14 |
from cubicweb.web import ExplicitLogin, InvalidSession |
|
15 |
from cubicweb.web.application import AbstractAuthenticationManager |
|
1668 | 16 |
|
0 | 17 |
|
18 |
class RepositoryAuthenticationManager(AbstractAuthenticationManager): |
|
19 |
"""authenticate user associated to a request and check session validity""" |
|
1668 | 20 |
|
2887
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
21 |
def __init__(self, vreg): |
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
22 |
super(RepositoryAuthenticationManager, self).__init__(vreg) |
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
23 |
self.repo = vreg.config.repository(vreg) |
1282dc6525c5
give vreg where we need it (eg no bound request)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2267
diff
changeset
|
24 |
self.log_queries = vreg.config['query-log-file'] |
0 | 25 |
|
26 |
def validate_session(self, req, session): |
|
27 |
"""check session validity, and return eventually hijacked session |
|
28 |
||
29 |
:raise InvalidSession: |
|
30 |
if session is corrupted for a reason or another and should be closed |
|
31 |
""" |
|
32 |
# with this authentication manager, session is actually a dbapi |
|
33 |
# connection |
|
34 |
cnx = session |
|
35 |
login = req.get_authorization()[0] |
|
36 |
try: |
|
37 |
# calling cnx.user() check connection validity, raise |
|
38 |
# BadConnectionId on failure |
|
39 |
user = cnx.user(req) |
|
2267
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
40 |
# check cnx.login and not user.login, since in case of login by |
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
41 |
# email, login and cnx.login are the email while user.login is the |
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
42 |
# actual user login |
e1d2df3f1091
move login by email functionnality on the repository side to avoid buggy call to internal_session from the web interface side
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
43 |
if login and cnx.login != login: |
0 | 44 |
cnx.close() |
45 |
raise InvalidSession('login mismatch') |
|
46 |
except BadConnectionId: |
|
47 |
# check if a connection should be automatically restablished |
|
48 |
if (login is None or login == cnx.login): |
|
49 |
login, password = cnx.login, cnx.password |
|
50 |
cnx = self.authenticate(req, login, password) |
|
51 |
user = cnx.user(req) |
|
52 |
# backport session's data |
|
53 |
cnx.data = session.data |
|
54 |
else: |
|
55 |
raise InvalidSession('bad connection id') |
|
56 |
# associate the connection to the current request |
|
57 |
req.set_connection(cnx, user) |
|
58 |
return cnx |
|
1488
6da89a703c5a
add ability to login with a primary email address - no tests for now are unittest_application.py are now broken
Florent <florent@secondweb.fr>
parents:
0
diff
changeset
|
59 |
|
0 | 60 |
def authenticate(self, req, _login=None, _password=None): |
61 |
"""authenticate user and return corresponding user object |
|
1488
6da89a703c5a
add ability to login with a primary email address - no tests for now are unittest_application.py are now broken
Florent <florent@secondweb.fr>
parents:
0
diff
changeset
|
62 |
|
0 | 63 |
:raise ExplicitLogin: if authentication is required (no authentication |
64 |
info found or wrong user/password) |
|
65 |
||
66 |
Note: this method is violating AuthenticationManager interface by |
|
67 |
returning a session instance instead of the user. This is expected by |
|
68 |
the InMemoryRepositorySessionManager. |
|
69 |
""" |
|
70 |
if _login is not None: |
|
71 |
login, password = _login, _password |
|
72 |
else: |
|
73 |
login, password = req.get_authorization() |
|
74 |
if not login: |
|
75 |
# No session and no login -> try anonymous |
|
76 |
login, password = self.vreg.config.anonymous_user() |
|
77 |
if not login: # anonymous not authorized |
|
78 |
raise ExplicitLogin() |
|
79 |
# remove possibly cached cursor coming from closed connection |
|
80 |
clear_cache(req, 'cursor') |
|
81 |
cnxprops = ConnectionProperties(self.vreg.config.repo_method, |
|
82 |
close=False, log=self.log_queries) |
|
83 |
try: |
|
84 |
cnx = repo_connect(self.repo, login, password, cnxprops=cnxprops) |
|
85 |
except AuthenticationError: |
|
86 |
req.set_message(req._('authentication failure')) |
|
87 |
# restore an anonymous connection if possible |
|
88 |
anonlogin, anonpassword = self.vreg.config.anonymous_user() |
|
89 |
if anonlogin and anonlogin != login: |
|
90 |
cnx = repo_connect(self.repo, anonlogin, anonpassword, |
|
91 |
cnxprops=cnxprops) |
|
92 |
self._init_cnx(cnx, anonlogin, anonpassword) |
|
93 |
else: |
|
94 |
raise ExplicitLogin() |
|
95 |
else: |
|
96 |
self._init_cnx(cnx, login, password) |
|
97 |
# associate the connection to the current request |
|
98 |
req.set_connection(cnx) |
|
99 |
return cnx |
|
100 |
||
101 |
def _init_cnx(self, cnx, login, password): |
|
102 |
# decorate connection |
|
103 |
if login == self.vreg.config.anonymous_user()[0]: |
|
104 |
cnx.anonymous_connection = True |
|
105 |
cnx.vreg = self.vreg |
|
106 |
cnx.login = login |
|
107 |
cnx.password = password |
|
108 |