2026-06-10 13:28:35 +02:00
|
|
|
from __future__ import annotations
|
2025-02-14 14:29:30 +01:00
|
|
|
|
2026-07-13 19:35:49 +02:00
|
|
|
from typing import TYPE_CHECKING, override
|
2025-02-14 14:29:30 +01:00
|
|
|
|
2026-07-13 19:35:49 +02:00
|
|
|
from ...Config import Config
|
2026-06-10 13:28:35 +02:00
|
|
|
from ...log import WARNING, slog
|
|
|
|
|
from ..Auth import Access
|
|
|
|
|
from ..Auth import Auth as AuthBase
|
2026-07-13 19:35:49 +02:00
|
|
|
from ..Auth import Entity as EntityBase
|
|
|
|
|
from ..Auth import Group as Group # Needed by jwutils utility layer
|
|
|
|
|
from ..Auth import User as User # Needed by jwutils utility layer
|
|
|
|
|
from ..Auth import Kind, ProjectFlags
|
2025-02-14 14:29:30 +01:00
|
|
|
|
2026-06-10 13:28:35 +02:00
|
|
|
if TYPE_CHECKING:
|
2026-07-13 19:35:49 +02:00
|
|
|
from typing import Mapping, Sequence
|
2026-07-04 11:55:56 +02:00
|
|
|
|
2026-07-13 19:35:49 +02:00
|
|
|
class Entity(EntityBase): # export
|
2026-06-10 13:28:35 +02:00
|
|
|
|
2026-07-13 19:35:49 +02:00
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
*,
|
|
|
|
|
auth: AuthBase,
|
|
|
|
|
name: str,
|
|
|
|
|
conf: Config,
|
|
|
|
|
kind: Kind,
|
|
|
|
|
**kwargs,
|
|
|
|
|
):
|
2026-07-07 18:44:58 +02:00
|
|
|
super().__init__(**kwargs)
|
2025-02-14 14:29:30 +01:00
|
|
|
self.__name = name
|
2025-06-05 20:48:14 +02:00
|
|
|
self.__conf = conf
|
2026-07-13 19:35:49 +02:00
|
|
|
self.__kind = kind
|
2025-02-14 14:29:30 +01:00
|
|
|
self.__auth = auth
|
2026-07-13 19:35:49 +02:00
|
|
|
self.__groups: list[Entity] | None = None
|
2025-10-13 12:45:51 +02:00
|
|
|
self.__email: str = conf['email']
|
2025-06-05 20:48:14 +02:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def conf(self):
|
|
|
|
|
return self.__conf
|
2025-02-14 14:29:30 +01:00
|
|
|
|
2026-07-13 19:35:49 +02:00
|
|
|
@override
|
2025-02-14 14:29:30 +01:00
|
|
|
def _name(self) -> str:
|
|
|
|
|
return self.__name
|
|
|
|
|
|
2026-07-13 19:35:49 +02:00
|
|
|
@override
|
|
|
|
|
def _groups(self) -> Sequence[Entity]:
|
|
|
|
|
if self.__kind != Kind.User:
|
|
|
|
|
return []
|
2025-02-14 14:29:30 +01:00
|
|
|
if self.__groups is None:
|
2025-04-29 11:54:56 +02:00
|
|
|
name: str = ''
|
2026-07-13 19:35:49 +02:00
|
|
|
ret: list[Entity] = []
|
2025-06-05 20:48:14 +02:00
|
|
|
for name in self.conf['groups']:
|
2026-07-13 19:35:49 +02:00
|
|
|
ret.append(
|
|
|
|
|
Entity(
|
|
|
|
|
auth = self.__auth,
|
|
|
|
|
name = name,
|
|
|
|
|
kind = Kind.Group,
|
|
|
|
|
conf = Config(),
|
|
|
|
|
)
|
|
|
|
|
)
|
2025-02-14 14:29:30 +01:00
|
|
|
self.__groups = ret
|
2025-04-29 11:54:56 +02:00
|
|
|
return self.__groups
|
2025-02-14 14:29:30 +01:00
|
|
|
|
2026-07-13 19:35:49 +02:00
|
|
|
@override
|
2025-06-05 20:48:14 +02:00
|
|
|
def _email(self) -> str:
|
|
|
|
|
return self.__email
|
|
|
|
|
|
2026-07-13 19:35:49 +02:00
|
|
|
@override
|
|
|
|
|
def _kind(self) -> Kind:
|
|
|
|
|
return self.__kind
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
def _projects(self, flags: ProjectFlags) -> Sequence[str]:
|
|
|
|
|
return []
|
|
|
|
|
|
2026-06-10 13:28:35 +02:00
|
|
|
class Auth(AuthBase): # export
|
2025-02-14 14:29:30 +01:00
|
|
|
|
2026-07-13 19:35:49 +02:00
|
|
|
def __init__(self, conf: Config, **kwargs):
|
2026-07-07 18:44:58 +02:00
|
|
|
super().__init__(conf, **kwargs)
|
2026-07-13 19:35:49 +02:00
|
|
|
self.___users: dict[str, EntityBase] | None = None
|
2025-02-14 14:29:30 +01:00
|
|
|
self.__groups = None
|
2026-07-13 19:35:49 +02:00
|
|
|
self.__current_user: EntityBase | None = None
|
|
|
|
|
self.__user_by_email: dict[str, EntityBase] | None = None
|
2025-02-14 14:29:30 +01:00
|
|
|
|
2025-06-05 20:48:14 +02:00
|
|
|
@property
|
2026-07-13 19:35:49 +02:00
|
|
|
def __users(self) -> dict[str, EntityBase]:
|
2025-06-05 20:48:14 +02:00
|
|
|
if self.___users is None:
|
2026-07-13 19:35:49 +02:00
|
|
|
ret: dict[str, EntityBase] = {}
|
2025-02-14 14:29:30 +01:00
|
|
|
for name in self.conf.entries('user'):
|
2025-06-05 20:48:14 +02:00
|
|
|
conf = self.conf.branch('user.' + name)
|
2026-06-10 13:28:35 +02:00
|
|
|
assert conf is not None, 'Config is None'
|
2026-07-13 19:35:49 +02:00
|
|
|
ret[name] = Entity(
|
|
|
|
|
auth = self,
|
|
|
|
|
name = name,
|
|
|
|
|
conf = conf,
|
|
|
|
|
kind = Kind.User,
|
|
|
|
|
)
|
2025-06-05 20:48:14 +02:00
|
|
|
self.___users = ret
|
|
|
|
|
return self.___users
|
2025-02-14 14:29:30 +01:00
|
|
|
|
2026-07-13 19:35:49 +02:00
|
|
|
@override
|
2026-06-10 13:28:35 +02:00
|
|
|
def _access(
|
2026-07-13 19:35:49 +02:00
|
|
|
self, what: str, access_type: Access | None, who: EntityBase | None
|
mypy: Fix any_generics, redundant_casts, unused_ignores
Add mypy directives disallow_any_generics, warn_redundant_casts, and
warn_unused_ignores. Fix all resulting type annotation errors:
- load_yaml.py: Add type arguments to dict, tuple, set annotations; fix
resolving dict/set types for link keys; handle str|int path types.
- misc.py: Add Mapping[str, Any] and Iterable[Any] type arguments; fix
multiline export comment to avoid bad pattern flag.
- multi_key_dict.py: Add type argument to dict[tuple, Any].
- StringTree.py: Move Any import out of TYPE_CHECKING; fix MutableMapping
type args.
- serdes.py: Add type argument to re.Match.
- Config.py: Change Config from Mapping to Mapping[str, str]; remove
unused cast.
- ev/base/Object.py, SimpleObject.py, EventLogger.py, PollerOneShot.py:
Add Mapping[str, Any] type args.
- ev/app/EventRouter.py: Add dict[str, Any] type args in inner functions.
- auth/*: Remove now-unnecessary # type: ignore[return-value] comments.
Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.82.0
Signed-off-by: Jan Lindemann <jan@janware.com>
2026-07-24 19:25:57 +02:00
|
|
|
) -> bool:
|
2026-06-10 13:28:35 +02:00
|
|
|
slog(
|
|
|
|
|
WARNING,
|
|
|
|
|
f'Returning False for {access_type} access to resource {what} by {who}'
|
|
|
|
|
)
|
2025-02-14 14:29:30 +01:00
|
|
|
return False
|
|
|
|
|
|
2026-07-13 19:35:49 +02:00
|
|
|
@override
|
|
|
|
|
def _entity(self, name: str, kind: Kind) -> EntityBase:
|
|
|
|
|
if kind != Kind.User:
|
|
|
|
|
raise NotImplementedError(
|
|
|
|
|
'Support for retrieving groups is not implemented'
|
|
|
|
|
)
|
2025-06-05 20:48:14 +02:00
|
|
|
return self.__users[name]
|
|
|
|
|
|
2026-07-13 19:35:49 +02:00
|
|
|
@override
|
|
|
|
|
def _entities(self, kind: Kind) -> Mapping[str, EntityBase]:
|
|
|
|
|
if kind != Kind.User:
|
|
|
|
|
raise NotImplementedError(
|
|
|
|
|
'Support for retrieving all groups is not implemented'
|
|
|
|
|
)
|
2025-06-05 20:48:14 +02:00
|
|
|
return self.__users
|
|
|
|
|
|
2026-07-13 19:35:49 +02:00
|
|
|
@override
|
|
|
|
|
def _current_entity(self) -> EntityBase:
|
2025-02-14 14:29:30 +01:00
|
|
|
if self.__current_user is None:
|
2026-07-13 19:35:49 +02:00
|
|
|
self.__current_user = self._entity(
|
|
|
|
|
self.conf['current_user'],
|
|
|
|
|
kind = Kind.User,
|
|
|
|
|
)
|
2025-02-14 14:29:30 +01:00
|
|
|
return self.__current_user
|
2025-06-05 20:48:14 +02:00
|
|
|
|
2026-07-13 19:35:49 +02:00
|
|
|
@override
|
|
|
|
|
def _entity_by_email(self, email: str, kind: Kind) -> EntityBase:
|
|
|
|
|
if kind != Kind.User:
|
|
|
|
|
raise NotImplementedError(
|
|
|
|
|
'Support for retrieving groups by email is not implemented'
|
|
|
|
|
)
|
2025-06-05 20:48:14 +02:00
|
|
|
if self.__user_by_email is None:
|
2026-07-13 19:35:49 +02:00
|
|
|
ret: dict[str, EntityBase] = dict()
|
2025-06-05 20:48:14 +02:00
|
|
|
for user in self.__users.values():
|
|
|
|
|
ret[user.email] = user
|
|
|
|
|
self.__user_by_email = ret
|
|
|
|
|
return self.__user_by_email[email]
|