mirror of
ssh://git.janware.com/srv/git/janware/proj/jw-python
synced 2026-01-15 01:52:56 +01:00
jwutils: Add Auth
Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
74902349b6
commit
9b1650b58f
4 changed files with 141 additions and 0 deletions
72
tools/python/jwutils/auth/Auth.py
Normal file
72
tools/python/jwutils/auth/Auth.py
Normal file
|
|
@ -0,0 +1,72 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
from typing import Optional, Union
|
||||||
|
|
||||||
|
import abc
|
||||||
|
|
||||||
|
from enum import Enum, auto
|
||||||
|
from jwutils import log, Config
|
||||||
|
|
||||||
|
class Access(Enum): # export
|
||||||
|
Read = auto()
|
||||||
|
Modify = auto()
|
||||||
|
Create = auto()
|
||||||
|
Delete = auto()
|
||||||
|
|
||||||
|
class Group: # export
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f'Group({self.name})'
|
||||||
|
|
||||||
|
@abc.abstractmethod
|
||||||
|
def _name(self) -> str:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self) -> str:
|
||||||
|
return self._name()
|
||||||
|
|
||||||
|
class User: # export
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f'User({self.name})'
|
||||||
|
|
||||||
|
@abc.abstractmethod
|
||||||
|
def _name(self) -> str:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abc.abstractmethod
|
||||||
|
def _groups(self) -> list[Group]:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self) -> str:
|
||||||
|
return self._name()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def groups(self) -> list[Group]:
|
||||||
|
return self._groups()
|
||||||
|
|
||||||
|
class Auth: # export
|
||||||
|
|
||||||
|
def __init__(self, conf: Config):
|
||||||
|
self.__conf = conf
|
||||||
|
|
||||||
|
@abc.abstractmethod
|
||||||
|
def _access(self, what: str, access_type: Optional[Access]=None, who: Optional[Union[User|Group]]=None) -> bool:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abc.abstractmethod
|
||||||
|
def _current_user(self) -> User:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@property
|
||||||
|
def conf(self):
|
||||||
|
return self.__conf
|
||||||
|
|
||||||
|
@property
|
||||||
|
def current_user(self) -> User:
|
||||||
|
return self._current_user()
|
||||||
|
|
||||||
|
def access(self, what: str, access_type: Optional[Access]=None, who: Optional[Union[User|Group]]=None) -> bool:
|
||||||
|
return self._access(what, access_type, who)
|
||||||
4
tools/python/jwutils/auth/Makefile
Normal file
4
tools/python/jwutils/auth/Makefile
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
TOPDIR = ../../../..
|
||||||
|
|
||||||
|
include $(TOPDIR)/make/proj.mk
|
||||||
|
include $(JWBDIR)/make/py-mod.mk
|
||||||
61
tools/python/jwutils/auth/dummy/Auth.py
Normal file
61
tools/python/jwutils/auth/dummy/Auth.py
Normal file
|
|
@ -0,0 +1,61 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
from typing import Optional, Union
|
||||||
|
|
||||||
|
from ... import log
|
||||||
|
from ... import Config
|
||||||
|
from .. import Access
|
||||||
|
from .. import Auth as AuthBase
|
||||||
|
from .. import Group as GroupBase
|
||||||
|
from .. import User as UserBase
|
||||||
|
|
||||||
|
class Group(GroupBase): # export
|
||||||
|
|
||||||
|
def __init__(self, auth: AuthBase, name: str):
|
||||||
|
self.__name = name
|
||||||
|
self.__auth = auth
|
||||||
|
|
||||||
|
def _name(self) -> str:
|
||||||
|
return self.__name
|
||||||
|
|
||||||
|
class User(UserBase):
|
||||||
|
|
||||||
|
def __init__(self, auth: AuthBase, name: str):
|
||||||
|
self.__name = name
|
||||||
|
self.__auth = auth
|
||||||
|
self.__groups = None
|
||||||
|
|
||||||
|
def _name(self) -> str:
|
||||||
|
return self.__name
|
||||||
|
|
||||||
|
def _groups(self) -> list[Group]:
|
||||||
|
if self.__groups is None:
|
||||||
|
for name in conf['user.' + name + '.groups']:
|
||||||
|
ret[name] = Group(self, name)
|
||||||
|
self.__groups = ret
|
||||||
|
self.__groups = ret
|
||||||
|
|
||||||
|
class Auth(AuthBase): # export
|
||||||
|
|
||||||
|
def __init__(self, conf: Config):
|
||||||
|
self.__conf = conf
|
||||||
|
self.__users = None
|
||||||
|
self.__groups = None
|
||||||
|
self.__current_user = None
|
||||||
|
|
||||||
|
def _user(self, name_) -> User:
|
||||||
|
if self.__users is None:
|
||||||
|
ret: dict[str, User] = {}
|
||||||
|
for name in self.conf.entries('user'):
|
||||||
|
ret[name] = User(self, name)
|
||||||
|
self.__users = ret
|
||||||
|
return self.__users[name_]
|
||||||
|
|
||||||
|
def _access(self, what: str, access_type: Optional[Access]=None, who: Optional[Union[User|Group]]=None) -> bool:
|
||||||
|
slog(log.WARNING, f'Returning False for {access_type} access to resource {what} by {who}')
|
||||||
|
return False
|
||||||
|
|
||||||
|
def _current_user(self) -> User:
|
||||||
|
if self.__current_user is None:
|
||||||
|
self.__current_user = self._user(self.conf['current_user'])
|
||||||
|
return self.__current_user
|
||||||
4
tools/python/jwutils/auth/dummy/Makefile
Normal file
4
tools/python/jwutils/auth/dummy/Makefile
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
TOPDIR = ../../../../..
|
||||||
|
|
||||||
|
include $(TOPDIR)/make/proj.mk
|
||||||
|
include $(JWBDIR)/make/py-mod.mk
|
||||||
Loading…
Add table
Add a link
Reference in a new issue