2026-06-10 13:28:35 +02:00
|
|
|
from __future__ import annotations
|
2025-06-05 20:48:14 +02:00
|
|
|
|
2026-07-04 11:55:56 +02:00
|
|
|
from typing import TYPE_CHECKING
|
2025-06-05 20:48:14 +02:00
|
|
|
|
2026-06-10 13:28:35 +02:00
|
|
|
import ldap # type: ignore[import-untyped]
|
2025-06-05 20:48:14 +02:00
|
|
|
|
2025-07-26 13:51:42 +02:00
|
|
|
from ...ldap import bind
|
2026-06-10 13:28:35 +02:00
|
|
|
from ...log import DEBUG, ERR, WARNING, slog
|
|
|
|
|
from ..Auth import Access
|
|
|
|
|
from ..Auth import Auth as AuthBase
|
|
|
|
|
from ..Auth import Group as GroupBase
|
|
|
|
|
from ..Auth import ProjectFlags
|
|
|
|
|
from ..Auth import User as UserBase
|
2025-06-05 20:48:14 +02:00
|
|
|
|
2026-06-10 13:28:35 +02:00
|
|
|
if TYPE_CHECKING:
|
2026-07-04 11:55:56 +02:00
|
|
|
from typing import Sequence
|
|
|
|
|
|
2026-06-10 13:28:35 +02:00
|
|
|
from ...Config import Config
|
|
|
|
|
|
|
|
|
|
class Group(GroupBase): # export
|
2025-06-05 20:48:14 +02:00
|
|
|
|
2026-07-07 18:44:58 +02:00
|
|
|
def __init__(self, *, auth: AuthBase, name: str, **kwargs):
|
|
|
|
|
super().__init__(**kwargs)
|
2025-06-05 20:48:14 +02:00
|
|
|
self.__name = name
|
|
|
|
|
self.__auth = auth
|
|
|
|
|
|
|
|
|
|
def _name(self) -> str:
|
|
|
|
|
return self.__name
|
|
|
|
|
|
|
|
|
|
class User(UserBase):
|
|
|
|
|
|
2026-07-07 18:44:58 +02:00
|
|
|
def __init__(self, *, auth: AuthBase, name: str, cn: str, email: str, **kwargs):
|
|
|
|
|
super().__init__(**kwargs)
|
2025-06-05 20:48:14 +02:00
|
|
|
self.__auth = auth
|
|
|
|
|
self.__name = name
|
|
|
|
|
self.__cn = cn
|
|
|
|
|
self.__email = email
|
2026-07-04 11:55:56 +02:00
|
|
|
self.__groups: list[GroupBase] | None = None
|
2025-06-05 20:48:14 +02:00
|
|
|
|
|
|
|
|
def _name(self) -> str:
|
|
|
|
|
return self.__name
|
|
|
|
|
|
2026-07-04 11:55:56 +02:00
|
|
|
def _groups(self) -> Sequence[GroupBase]:
|
2025-06-05 20:48:14 +02:00
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
def _email(self) -> str:
|
|
|
|
|
return self.__email
|
|
|
|
|
|
|
|
|
|
def _display_name(self) -> str:
|
|
|
|
|
return self.__cn
|
|
|
|
|
|
2026-06-10 13:28:35 +02:00
|
|
|
class Auth(AuthBase): # export
|
2025-06-05 20:48:14 +02:00
|
|
|
|
2026-07-07 18:44:58 +02:00
|
|
|
def __init__(self, *, conf: Config, **kwargs):
|
|
|
|
|
super().__init__(conf, **kwargs)
|
2026-07-04 11:55:56 +02:00
|
|
|
self.___users: dict[str, UserBase] | None = None
|
|
|
|
|
self.___user_by_email: dict[str, User] | None = None
|
2025-06-05 20:48:14 +02:00
|
|
|
self.__groups = None
|
2026-06-10 13:28:35 +02:00
|
|
|
self.__current_user: User | None = None
|
2025-06-05 20:48:14 +02:00
|
|
|
self.__user_base_dn = conf['user_base_dn']
|
|
|
|
|
self.__conn = self.__bind()
|
2025-09-18 23:11:22 +02:00
|
|
|
self.__dummy = self.load(conf, 'dummy')
|
2025-06-05 20:48:14 +02:00
|
|
|
|
|
|
|
|
def __bind(self):
|
2025-07-26 13:51:42 +02:00
|
|
|
return bind(self.conf)
|
2025-06-05 20:48:14 +02:00
|
|
|
|
|
|
|
|
@property
|
2025-10-13 12:45:51 +02:00
|
|
|
def __users(self) -> dict[str, UserBase]:
|
2025-06-05 20:48:14 +02:00
|
|
|
if self.___users is None:
|
2025-10-13 12:45:51 +02:00
|
|
|
ret: dict[str, UserBase] = {}
|
2025-06-05 20:48:14 +02:00
|
|
|
ret_by_email: dict[str, User] = {}
|
2025-09-11 21:35:01 +02:00
|
|
|
for res in self.__conn.find(
|
2025-06-05 20:48:14 +02:00
|
|
|
self.__user_base_dn,
|
2026-06-10 13:28:35 +02:00
|
|
|
ldap.SCOPE_SUBTREE, # pyright: ignore[reportAttributeAccessIssue]
|
2025-06-05 20:48:14 +02:00
|
|
|
"objectClass=inetOrgPerson",
|
2026-06-10 13:28:35 +02:00
|
|
|
('uid', 'cn', 'uidNumber', 'mail', 'maildrop')):
|
2025-09-11 21:35:01 +02:00
|
|
|
try:
|
|
|
|
|
if 'displayName' in res[1]:
|
|
|
|
|
cn = res[1]['displayName'][0].decode('utf-8')
|
|
|
|
|
else:
|
|
|
|
|
cn = res[1]['cn'][0].decode('utf-8')
|
|
|
|
|
uid = res[1]['uid'][0].decode('utf-8')
|
2026-06-10 13:28:35 +02:00
|
|
|
res[1]['uidNumber'][0].decode('utf-8')
|
2025-09-11 21:35:01 +02:00
|
|
|
emails = []
|
|
|
|
|
#for attr in ['mail', 'maildrop']:
|
|
|
|
|
for attr in ['mail']:
|
|
|
|
|
if attr in res[1]:
|
|
|
|
|
for entry in res[1][attr]:
|
|
|
|
|
emails.append(entry.decode('utf-8'))
|
|
|
|
|
if not emails:
|
|
|
|
|
slog(DEBUG, f'No email for user "{uid}", skipping')
|
2025-06-05 20:48:14 +02:00
|
|
|
continue
|
2026-07-07 18:44:58 +02:00
|
|
|
user = User(auth = self, name = uid, cn = cn, email = emails[0])
|
2025-09-11 21:35:01 +02:00
|
|
|
ret[uid] = user
|
|
|
|
|
for email in emails:
|
|
|
|
|
ret_by_email[email] = user
|
|
|
|
|
except Exception as e:
|
|
|
|
|
slog(WARNING, f'Exception {e}')
|
|
|
|
|
raise
|
|
|
|
|
continue
|
2025-10-13 12:45:51 +02:00
|
|
|
for dummy_user in self.__dummy.users.values():
|
|
|
|
|
ret[dummy_user.name] = dummy_user
|
2025-06-05 20:48:14 +02:00
|
|
|
self.___users = ret
|
|
|
|
|
self.___user_by_email = ret_by_email
|
|
|
|
|
return self.___users
|
|
|
|
|
|
|
|
|
|
@property
|
2025-10-13 12:45:51 +02:00
|
|
|
def __user_by_email(self) -> dict[str, UserBase]:
|
2025-06-05 20:48:14 +02:00
|
|
|
if self.___user_by_email is None:
|
|
|
|
|
self.__users
|
2026-06-10 13:28:35 +02:00
|
|
|
return self.___user_by_email # type: ignore # We are sure that ___user_by_email is not None at this point
|
2025-06-05 20:48:14 +02:00
|
|
|
|
2026-06-10 13:28:35 +02:00
|
|
|
def _access(
|
2026-07-04 11:55:56 +02:00
|
|
|
self, what: str, access_type: Access | None, who: UserBase | GroupBase | None
|
2026-06-10 13:28:35 +02:00
|
|
|
) -> bool: # type: ignore
|
|
|
|
|
slog(
|
|
|
|
|
WARNING,
|
|
|
|
|
f'Returning False for {access_type} access to resource {what} by {who}'
|
|
|
|
|
)
|
2025-06-05 20:48:14 +02:00
|
|
|
return False
|
|
|
|
|
|
2025-10-13 12:45:51 +02:00
|
|
|
def _user(self, name) -> UserBase:
|
2025-06-05 20:48:14 +02:00
|
|
|
try:
|
|
|
|
|
return self.__users[name]
|
|
|
|
|
except:
|
|
|
|
|
slog(ERR, f'No such user: "{name}"')
|
|
|
|
|
raise
|
|
|
|
|
|
2025-10-13 12:45:51 +02:00
|
|
|
def _user_by_email(self, email: str) -> UserBase:
|
2025-06-05 20:48:14 +02:00
|
|
|
return self.__user_by_email[email]
|
|
|
|
|
|
|
|
|
|
def _current_user(self) -> User:
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
2025-10-13 12:45:51 +02:00
|
|
|
def _users(self) -> dict[str, UserBase]:
|
2025-06-05 20:48:14 +02:00
|
|
|
return self.__users
|
|
|
|
|
|
2026-07-04 11:55:56 +02:00
|
|
|
def _projects(self, name, flags: ProjectFlags) -> Sequence[str]:
|
2025-06-05 20:48:14 +02:00
|
|
|
if flags & ProjectFlags.Contributing:
|
|
|
|
|
# TODO: Ask LDAP
|
2026-06-10 13:28:35 +02:00
|
|
|
slog(
|
|
|
|
|
WARNING,
|
|
|
|
|
'Querying LDAP for projects a user contributes to is not '
|
|
|
|
|
'implemented, ignoring'
|
|
|
|
|
)
|
2025-10-13 12:45:51 +02:00
|
|
|
return []
|