author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 13 Oct 2009 08:50:19 +0200 | |
changeset 3647 | 2941f4a0aab9 |
parent 3460 | e4843535db25 |
child 3658 | d8f2ec7e91fa |
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 |
|
3460
e4843535db25
[api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
76 |
login, password = self.vreg.config.anonymous_user() |
0 | 77 |
if not login: # anonymous not authorized |
78 |
raise ExplicitLogin() |
|
79 |
# remove possibly cached cursor coming from closed connection |
|
80 |
clear_cache(req, 'cursor') |
|
3460
e4843535db25
[api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
81 |
cnxprops = ConnectionProperties(self.vreg.config.repo_method, |
0 | 82 |
close=False, log=self.log_queries) |
83 |
try: |
|
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
84 |
cnx = repo_connect(self.repo, login, password=password, |
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
85 |
cnxprops=cnxprops) |
0 | 86 |
except AuthenticationError: |
87 |
req.set_message(req._('authentication failure')) |
|
88 |
# restore an anonymous connection if possible |
|
3460
e4843535db25
[api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
89 |
anonlogin, anonpassword = self.vreg.config.anonymous_user() |
0 | 90 |
if anonlogin and anonlogin != login: |
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
91 |
cnx = repo_connect(self.repo, anonlogin, password=anonpassword, |
0 | 92 |
cnxprops=cnxprops) |
93 |
self._init_cnx(cnx, anonlogin, anonpassword) |
|
94 |
else: |
|
95 |
raise ExplicitLogin() |
|
96 |
else: |
|
97 |
self._init_cnx(cnx, login, password) |
|
98 |
# associate the connection to the current request |
|
99 |
req.set_connection(cnx) |
|
100 |
return cnx |
|
101 |
||
102 |
def _init_cnx(self, cnx, login, password): |
|
103 |
# decorate connection |
|
3460
e4843535db25
[api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
104 |
if login == self.vreg.config.anonymous_user()[0]: |
0 | 105 |
cnx.anonymous_connection = True |
3460
e4843535db25
[api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
106 |
cnx.vreg = self.vreg |
0 | 107 |
cnx.login = login |
108 |
cnx.password = password |
|
109 |