|
19 | 19 |
|
20 | 20 | import logging |
21 | 21 | from typing import Optional |
22 | | - |
23 | 22 | from urllib import parse |
24 | 23 |
|
25 | 24 | from django.http import HttpRequest |
26 | 25 | from django.urls import reverse |
27 | 26 |
|
28 | 27 | from nav.auditlog.models import LogEntry |
29 | | -from nav.models.profiles import Account |
30 | | -from nav.web.auth import ldap, remote_user |
31 | | -from nav.web.auth.ldap_auth_backend import _handle_ldap_admin_status |
32 | | - |
| 28 | +from nav.web.auth import remote_user |
33 | 29 | from nav.web.auth.sudo import desudo |
34 | 30 | from nav.web.auth.utils import clear_session, get_account |
35 | 31 |
|
36 | | - |
37 | 32 | _logger = logging.getLogger(__name__) |
38 | 33 |
|
39 | 34 |
|
|
47 | 42 | LOGOUT_URL = '/index/logout/' |
48 | 43 |
|
49 | 44 |
|
50 | | -def authenticate(username: str, password: str) -> Optional[Account]: |
51 | | - """Authenticate username and password against database. |
52 | | - Returns account object if user was authenticated, else None. |
53 | | - """ |
54 | | - # FIXME Log stuff? |
55 | | - auth = False |
56 | | - account = None |
57 | | - |
58 | | - # Try to find the account in the database. If it's not found we can try |
59 | | - # LDAP. |
60 | | - try: |
61 | | - account = Account.objects.get(login__iexact=username) |
62 | | - except Account.DoesNotExist: |
63 | | - if ldap.available: |
64 | | - user = ldap.authenticate(username, password) |
65 | | - # If we authenticated, store the user in database. |
66 | | - if user: |
67 | | - account = Account( |
68 | | - login=user.username, name=user.get_real_name(), ext_sync='ldap' |
69 | | - ) |
70 | | - account.set_password(password) |
71 | | - account.save() |
72 | | - _handle_ldap_admin_status(user, account) |
73 | | - # We're authenticated now |
74 | | - auth = True |
75 | | - |
76 | | - if account and account.locked: |
77 | | - _logger.info("Locked user %s tried to log in", account.login) |
78 | | - |
79 | | - if ( |
80 | | - account |
81 | | - and account.ext_sync == 'ldap' |
82 | | - and ldap.available |
83 | | - and not auth |
84 | | - and not account.locked |
85 | | - ): |
86 | | - try: |
87 | | - auth = ldap.authenticate(username, password) |
88 | | - except ldap.NoAnswerError: |
89 | | - # Fallback to stored password if ldap is unavailable |
90 | | - auth = False |
91 | | - else: |
92 | | - if auth: |
93 | | - account.set_password(password) |
94 | | - account.save() |
95 | | - _handle_ldap_admin_status(auth, account) |
96 | | - else: |
97 | | - return |
98 | | - |
99 | | - if account and not auth: |
100 | | - auth = account.check_password(password) |
101 | | - |
102 | | - if auth and account: |
103 | | - return account |
104 | | - else: |
105 | | - return None |
106 | | - |
107 | | - |
108 | 45 | def get_login_url(request: HttpRequest) -> str: |
109 | 46 | """Calculate which login_url to use""" |
110 | 47 | path = parse.quote(request.get_full_path()) |
|
0 commit comments