2026-06-10 13:28:35 +02:00
|
|
|
from __future__ import annotations
|
2025-02-14 14:29:30 +01:00
|
|
|
|
|
|
|
|
import abc
|
|
|
|
|
|
2026-06-10 13:28:35 +02:00
|
|
|
from enum import Enum, Flag, auto
|
2026-07-04 11:55:56 +02:00
|
|
|
from typing import TYPE_CHECKING, Self, Sequence
|
2025-06-05 20:48:14 +02:00
|
|
|
|
2026-06-10 13:28:35 +02:00
|
|
|
from ..log import ERR
|
2026-06-10 07:22:46 +02:00
|
|
|
from ..misc import load_object
|
2025-02-14 14:29:30 +01:00
|
|
|
|
2026-06-10 13:28:35 +02:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from ..Config import Config
|
|
|
|
|
|
|
|
|
|
class Access(Enum): # export
|
|
|
|
|
Read = auto()
|
2025-02-14 14:29:30 +01:00
|
|
|
Modify = auto()
|
|
|
|
|
Create = auto()
|
|
|
|
|
Delete = auto()
|
|
|
|
|
|
2026-06-10 13:28:35 +02:00
|
|
|
class ProjectFlags(Flag): # export
|
|
|
|
|
NoFlags = auto()
|
2025-06-05 20:48:14 +02:00
|
|
|
Contributing = auto()
|
2026-06-10 13:28:35 +02:00
|
|
|
Active = auto()
|
2025-06-05 20:48:14 +02:00
|
|
|
|
2026-07-04 11:55:56 +02:00
|
|
|
class AuthEntity(abc.ABC): # export
|
2025-02-14 14:29:30 +01:00
|
|
|
|
2026-07-07 18:44:58 +02:00
|
|
|
def __init__(self, **kwargs) -> None:
|
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
|
2025-02-14 14:29:30 +01:00
|
|
|
@abc.abstractmethod
|
|
|
|
|
def _name(self) -> str:
|
2026-07-04 11:55:56 +02:00
|
|
|
...
|
2025-02-14 14:29:30 +01:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def name(self) -> str:
|
|
|
|
|
return self._name()
|
|
|
|
|
|
2025-06-05 20:48:14 +02:00
|
|
|
def _display_name(self) -> str:
|
|
|
|
|
return self._name()
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def display_name(self) -> str:
|
|
|
|
|
return self._display_name()
|
|
|
|
|
|
|
|
|
|
def _email(self) -> str:
|
2026-07-04 11:55:56 +02:00
|
|
|
return 'no-email@example.com'
|
2025-06-05 20:48:14 +02:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def email(self) -> str:
|
|
|
|
|
return self._email()
|
|
|
|
|
|
2026-07-04 11:55:56 +02:00
|
|
|
class Group(AuthEntity): # export
|
|
|
|
|
|
2026-07-07 18:44:58 +02:00
|
|
|
def __init__(self, **kwargs) -> None:
|
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
|
2026-07-04 11:55:56 +02:00
|
|
|
def __repr__(self):
|
|
|
|
|
return f'Group({self.name})'
|
|
|
|
|
|
|
|
|
|
class User(AuthEntity): # export
|
|
|
|
|
|
2026-07-07 18:44:58 +02:00
|
|
|
def __init__(self, **kwargs) -> None:
|
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
|
2026-07-04 11:55:56 +02:00
|
|
|
def __repr__(self):
|
|
|
|
|
return f'User({self.name})'
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
def _groups(self) -> Sequence[Group]:
|
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def groups(self) -> Sequence[Group]:
|
|
|
|
|
return self._groups()
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
def _email(self) -> str:
|
|
|
|
|
...
|
|
|
|
|
|
2026-06-10 13:28:35 +02:00
|
|
|
class Auth(abc.ABC): # export
|
2025-06-05 20:48:14 +02:00
|
|
|
|
|
|
|
|
@classmethod
|
2026-06-10 13:28:35 +02:00
|
|
|
def load(cls, conf: Config, tp: str = '') -> Self:
|
2025-09-12 08:25:16 +02:00
|
|
|
if tp == '':
|
2025-10-13 12:45:51 +02:00
|
|
|
val = conf.get('type')
|
|
|
|
|
if val is None:
|
2026-06-10 13:28:35 +02:00
|
|
|
msg = 'No type specified in auth configuration'
|
2025-09-12 08:25:16 +02:00
|
|
|
conf.dump(ERR, msg)
|
|
|
|
|
raise Exception(msg)
|
2025-10-13 12:45:51 +02:00
|
|
|
tp = val
|
2025-06-05 20:48:14 +02:00
|
|
|
return load_object(f'jwutils.auth.{tp}.Auth', Auth, 'Auth', conf)
|
2025-02-14 14:29:30 +01:00
|
|
|
|
|
|
|
|
def __init__(self, conf: Config):
|
|
|
|
|
self.__conf = conf
|
2026-07-04 11:55:56 +02:00
|
|
|
self.__base_user_by_email: dict[str, User] | None = None
|
2025-02-14 14:29:30 +01:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def conf(self):
|
|
|
|
|
return self.__conf
|
|
|
|
|
|
2025-06-05 20:48:14 +02:00
|
|
|
@abc.abstractmethod
|
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: User | Group | None
|
2026-06-10 13:28:35 +02:00
|
|
|
) -> bool:
|
2026-07-04 11:55:56 +02:00
|
|
|
...
|
2025-06-05 20:48:14 +02:00
|
|
|
|
2026-06-10 13:28:35 +02:00
|
|
|
def access(
|
|
|
|
|
self,
|
|
|
|
|
what: str,
|
2026-07-04 11:55:56 +02:00
|
|
|
access_type: Access | None = None,
|
|
|
|
|
who: User | Group | None = None
|
2026-06-10 13:28:35 +02:00
|
|
|
) -> bool:
|
2025-06-05 20:48:14 +02:00
|
|
|
return self._access(what, access_type, who)
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
|
def _current_user(self) -> User:
|
2026-07-04 11:55:56 +02:00
|
|
|
...
|
2025-06-05 20:48:14 +02:00
|
|
|
|
2025-02-14 14:29:30 +01:00
|
|
|
@property
|
|
|
|
|
def current_user(self) -> User:
|
|
|
|
|
return self._current_user()
|
|
|
|
|
|
2025-06-05 20:48:14 +02:00
|
|
|
@abc.abstractmethod
|
|
|
|
|
def _user(self, name) -> User:
|
2026-07-04 11:55:56 +02:00
|
|
|
...
|
2025-06-05 20:48:14 +02:00
|
|
|
|
|
|
|
|
def user(self, name) -> User:
|
|
|
|
|
return self._user(name)
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
2025-10-13 12:45:51 +02:00
|
|
|
def _users(self) -> dict[str, User]:
|
2026-07-04 11:55:56 +02:00
|
|
|
...
|
2025-06-05 20:48:14 +02:00
|
|
|
|
|
|
|
|
def _user_by_email(self, email: str) -> User:
|
|
|
|
|
if self.__base_user_by_email is None:
|
|
|
|
|
ret: dict[str, User] = dict()
|
|
|
|
|
users = self._users()
|
|
|
|
|
for user in users.values():
|
|
|
|
|
ret[user.email] = user
|
|
|
|
|
self.__base_user_by_email = ret
|
|
|
|
|
return self.__base_user_by_email[email]
|
|
|
|
|
|
|
|
|
|
def user_by_email(self, email) -> User:
|
|
|
|
|
return self._user_by_email(email)
|
|
|
|
|
|
|
|
|
|
@property
|
2025-10-13 12:45:51 +02:00
|
|
|
def users(self) -> dict[str, User]:
|
2025-06-05 20:48:14 +02:00
|
|
|
return self._users()
|
|
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
2026-07-04 11:55:56 +02:00
|
|
|
def _projects(self, name, flags: ProjectFlags) -> Sequence[str]:
|
|
|
|
|
...
|
2025-06-05 20:48:14 +02:00
|
|
|
|
2026-07-04 11:55:56 +02:00
|
|
|
def projects(self, name, flags: ProjectFlags) -> Sequence[str]:
|
2025-06-05 20:48:14 +02:00
|
|
|
return self._projects(name, flags)
|