Show More
@@ -1,76 +1,77 | |||||
1 | from datetime import datetime |
|
1 | from datetime import datetime | |
2 | from decorator import decorator |
|
2 | from decorator import decorator | |
3 | from functools import wraps |
|
3 | from functools import wraps | |
4 | from pylons import session, url |
|
4 | from pylons import session, url | |
5 | from pylons.controllers.util import abort, redirect |
|
5 | from pylons.controllers.util import abort, redirect | |
6 | from pylons_app.model import meta |
|
6 | from pylons_app.model import meta | |
7 | from pylons_app.model.db import Users |
|
7 | from pylons_app.model.db import Users | |
8 | from sqlalchemy.exc import OperationalError |
|
8 | from sqlalchemy.exc import OperationalError | |
9 | from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound |
|
9 | from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound | |
10 | import crypt |
|
10 | import crypt | |
11 | import logging |
|
11 | import logging | |
12 | log = logging.getLogger(__name__) |
|
12 | log = logging.getLogger(__name__) | |
13 |
|
13 | |||
14 | def get_crypt_password(password): |
|
14 | def get_crypt_password(password): | |
15 | """ |
|
15 | """ | |
16 | Cryptographic function used for password hashing |
|
16 | Cryptographic function used for password hashing | |
17 | @param password: password to hash |
|
17 | @param password: password to hash | |
18 | """ |
|
18 | """ | |
19 | return crypt.crypt(password, '6a') |
|
19 | return crypt.crypt(password, '6a') | |
20 |
|
20 | |||
21 | def authfunc(environ, username, password): |
|
21 | def authfunc(environ, username, password): | |
22 | sa = meta.Session |
|
22 | sa = meta.Session | |
23 | password_crypt = get_crypt_password(password) |
|
23 | password_crypt = get_crypt_password(password) | |
24 | try: |
|
24 | try: | |
25 | user = sa.query(Users).filter(Users.username == username).one() |
|
25 | user = sa.query(Users).filter(Users.username == username).one() | |
26 | except (NoResultFound, MultipleResultsFound, OperationalError) as e: |
|
26 | except (NoResultFound, MultipleResultsFound, OperationalError) as e: | |
27 | log.error(e) |
|
27 | log.error(e) | |
28 | user = None |
|
28 | user = None | |
29 |
|
29 | |||
30 | if user: |
|
30 | if user: | |
31 | if user.active: |
|
31 | if user.active: | |
32 | if user.username == username and user.password == password_crypt: |
|
32 | if user.username == username and user.password == password_crypt: | |
33 | log.info('user %s authenticated correctly', username) |
|
33 | log.info('user %s authenticated correctly', username) | |
34 | return True |
|
34 | return True | |
35 | else: |
|
35 | else: | |
36 | log.error('user %s is disabled', username) |
|
36 | log.error('user %s is disabled', username) | |
37 |
|
37 | |||
38 | return False |
|
38 | return False | |
39 |
|
39 | |||
40 | class AuthUser(object): |
|
40 | class AuthUser(object): | |
41 | """ |
|
41 | """ | |
42 | A simple object that handles a mercurial username for authentication |
|
42 | A simple object that handles a mercurial username for authentication | |
43 | """ |
|
43 | """ | |
44 |
username = ' |
|
44 | username = 'None' | |
45 | is_authenticated = False |
|
45 | is_authenticated = False | |
46 | is_admin = False |
|
46 | is_admin = False | |
47 | permissions = set() |
|
47 | permissions = set() | |
48 | group = set() |
|
48 | group = set() | |
49 |
|
49 | |||
50 | def __init__(self): |
|
50 | def __init__(self): | |
51 | pass |
|
51 | pass | |
52 |
|
52 | |||
53 | #=============================================================================== |
|
53 | #=============================================================================== | |
54 | # DECORATORS |
|
54 | # DECORATORS | |
55 | #=============================================================================== |
|
55 | #=============================================================================== | |
56 | class LoginRequired(object): |
|
56 | class LoginRequired(object): | |
57 | """ |
|
57 | """ | |
58 | Must be logged in to execute this function else redirect to login page |
|
58 | Must be logged in to execute this function else redirect to login page | |
59 | """ |
|
59 | """ | |
60 | def __init__(self): |
|
60 | def __init__(self): | |
61 | pass |
|
61 | pass | |
62 |
|
62 | |||
63 | def __call__(self, func): |
|
63 | def __call__(self, func): | |
64 | user = session.get('hg_app_user', AuthUser()) |
|
|||
65 | log.info('Checking login required for %s', user.username) |
|
|||
66 |
|
64 | |||
67 | @wraps(func) |
|
65 | @wraps(func) | |
68 | def _wrapper(*fargs, **fkwargs): |
|
66 | def _wrapper(*fargs, **fkwargs): | |
|
67 | user = session.get('hg_app_user', AuthUser()) | |||
|
68 | log.info('Checking login required for user:%s', user.username) | |||
69 | if user.is_authenticated: |
|
69 | if user.is_authenticated: | |
70 | log.info('user %s is authenticated', user.username) |
|
70 | log.info('user %s is authenticated', user.username) | |
71 | func(*fargs) |
|
71 | func(*fargs) | |
72 | else: |
|
72 | else: | |
73 | logging.info('user %s not authenticated', user.username) |
|
73 | logging.info('user %s not authenticated', user.username) | |
|
74 | logging.info('redirecting to login page') | |||
74 | return redirect(url('login_home')) |
|
75 | return redirect(url('login_home')) | |
75 |
|
76 | |||
76 | return _wrapper |
|
77 | return _wrapper |
General Comments 0
You need to be logged in to leave comments.
Login now