jwutils: Move to jwutils -> jw.util
Move all implementation source code from the jwutils module to jw.util. For compatibility with existing Python modules, keep a thin, autogenerated compatibility shim under jwutils. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
bc7652fdf9
commit
a2684dd601
6 changed files with 678 additions and 52 deletions
141
src/python/jw/util/auth/Auth.py
Normal file
141
src/python/jw/util/auth/Auth.py
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
from typing import Optional, Union, Self
|
||||
|
||||
import abc
|
||||
|
||||
from enum import Flag, Enum, auto
|
||||
|
||||
from ..log import *
|
||||
from ..Config import Config
|
||||
from ..misc import load_object
|
||||
|
||||
class Access(Enum): # export
|
||||
Read = auto()
|
||||
Modify = auto()
|
||||
Create = auto()
|
||||
Delete = auto()
|
||||
|
||||
class ProjectFlags(Flag): # export
|
||||
NoFlags = auto()
|
||||
Contributing = auto()
|
||||
Active = 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:
|
||||
raise NotImplementedError
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return self._name()
|
||||
|
||||
def _display_name(self) -> str:
|
||||
return self._name()
|
||||
|
||||
@property
|
||||
def display_name(self) -> str:
|
||||
return self._display_name()
|
||||
|
||||
@abc.abstractmethod
|
||||
def _groups(self) -> list[Group]:
|
||||
raise NotImplementedError
|
||||
|
||||
@property
|
||||
def groups(self) -> list[Group]:
|
||||
return self._groups()
|
||||
|
||||
@abc.abstractmethod
|
||||
def _email(self) -> str:
|
||||
raise NotImplementedError
|
||||
|
||||
@property
|
||||
def email(self) -> str:
|
||||
return self._email()
|
||||
|
||||
class Auth(abc.ABC): # export
|
||||
|
||||
@classmethod
|
||||
def load(cls, conf: Config, tp: str='') -> Self:
|
||||
if tp == '':
|
||||
val = conf.get('type')
|
||||
if val is None:
|
||||
msg = f'No type specified in auth configuration'
|
||||
conf.dump(ERR, msg)
|
||||
raise Exception(msg)
|
||||
tp = val
|
||||
return load_object(f'jwutils.auth.{tp}.Auth', Auth, 'Auth', conf)
|
||||
|
||||
def __init__(self, conf: Config):
|
||||
self.__conf = conf
|
||||
self.__base_user_by_email: Optional[dict[str, User]] = None
|
||||
|
||||
@property
|
||||
def conf(self):
|
||||
return self.__conf
|
||||
|
||||
@abc.abstractmethod
|
||||
def _access(self, what: str, access_type: Optional[Access], who: User|Group|None) -> bool:
|
||||
raise NotImplementedError
|
||||
|
||||
def access(self, what: str, access_type: Optional[Access]=None, who: Optional[Union[User|Group]]=None) -> bool:
|
||||
return self._access(what, access_type, who)
|
||||
|
||||
@abc.abstractmethod
|
||||
def _current_user(self) -> User:
|
||||
raise NotImplementedError
|
||||
|
||||
@property
|
||||
def current_user(self) -> User:
|
||||
return self._current_user()
|
||||
|
||||
@abc.abstractmethod
|
||||
def _user(self, name) -> User:
|
||||
raise NotImplementedError
|
||||
|
||||
def user(self, name) -> User:
|
||||
return self._user(name)
|
||||
|
||||
@abc.abstractmethod
|
||||
def _users(self) -> dict[str, User]:
|
||||
raise NotImplementedError
|
||||
|
||||
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
|
||||
def users(self) -> dict[str, User]:
|
||||
return self._users()
|
||||
|
||||
@abc.abstractmethod
|
||||
def _projects(self, name, flags: ProjectFlags) -> list[str]:
|
||||
raise NotImplementedError
|
||||
|
||||
def projects(self, name, flags: ProjectFlags) -> list[str]:
|
||||
return self._projects(name, flags)
|
||||
4
src/python/jw/util/auth/Makefile
Normal file
4
src/python/jw/util/auth/Makefile
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
TOPDIR = ../../../../..
|
||||
|
||||
include $(TOPDIR)/make/proj.mk
|
||||
include $(JWBDIR)/make/py-mod.mk
|
||||
93
src/python/jw/util/auth/dummy/Auth.py
Normal file
93
src/python/jw/util/auth/dummy/Auth.py
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
from typing import Optional, Union
|
||||
|
||||
from ...log import *
|
||||
from ... import Config
|
||||
from .. import Access
|
||||
from .. import Auth as AuthBase
|
||||
from .. import Group as GroupBase
|
||||
from .. import User as UserBase
|
||||
from .. import ProjectFlags
|
||||
|
||||
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): # export
|
||||
|
||||
def __init__(self, auth: AuthBase, name: str, conf: Config):
|
||||
self.__name = name
|
||||
self.__conf = conf
|
||||
self.__auth = auth
|
||||
self.__groups: Optional[list[GroupBase]] = None
|
||||
self.__email: str = conf['email']
|
||||
|
||||
@property
|
||||
def conf(self):
|
||||
return self.__conf
|
||||
|
||||
def _name(self) -> str:
|
||||
return self.__name
|
||||
|
||||
def _groups(self) -> list[GroupBase]:
|
||||
if self.__groups is None:
|
||||
name: str = ''
|
||||
ret: list[GroupBase] = []
|
||||
for name in self.conf['groups']:
|
||||
ret.append(Group(self.__auth, name))
|
||||
self.__groups = ret
|
||||
return self.__groups
|
||||
|
||||
def _email(self) -> str:
|
||||
return self.__email
|
||||
|
||||
class Auth(AuthBase): # export
|
||||
|
||||
def __init__(self, conf: Config):
|
||||
super().__init__(conf)
|
||||
self.___users: Optional[dict[str, UserBase]] = None
|
||||
self.__groups = None
|
||||
self.__current_user: UserBase|None = None
|
||||
self.__user_by_email: Optional[dict[str, UserBase]] = None
|
||||
|
||||
@property
|
||||
def __users(self) -> dict[str, UserBase]:
|
||||
if self.___users is None:
|
||||
ret: dict[str, UserBase] = {}
|
||||
for name in self.conf.entries('user'):
|
||||
conf = self.conf.branch('user.' + name)
|
||||
ret[name] = User(self, name, conf)
|
||||
self.___users = ret
|
||||
return self.___users
|
||||
|
||||
def _access(self, what: str, access_type: Optional[Access], who: User|GroupBase|None) -> bool: # type: ignore
|
||||
slog(WARNING, f'Returning False for {access_type} access to resource {what} by {who}')
|
||||
return False
|
||||
|
||||
def _user(self, name) -> UserBase:
|
||||
return self.__users[name]
|
||||
|
||||
def _users(self) -> dict[str, UserBase]:
|
||||
return self.__users
|
||||
|
||||
def _current_user(self) -> UserBase:
|
||||
if self.__current_user is None:
|
||||
self.__current_user = self._user(self.conf['current_user'])
|
||||
return self.__current_user
|
||||
|
||||
def _user_by_email(self, email: str) -> UserBase:
|
||||
if self.__user_by_email is None:
|
||||
ret: dict[str, UserBase] = dict()
|
||||
for user in self.__users.values():
|
||||
ret[user.email] = user
|
||||
self.__user_by_email = ret
|
||||
return self.__user_by_email[email]
|
||||
|
||||
def _projects(self, name, flags: ProjectFlags) -> list[str]:
|
||||
return []
|
||||
4
src/python/jw/util/auth/dummy/Makefile
Normal file
4
src/python/jw/util/auth/dummy/Makefile
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
TOPDIR = ../../../../../..
|
||||
|
||||
include $(TOPDIR)/make/proj.mk
|
||||
include $(JWBDIR)/make/py-mod.mk
|
||||
140
src/python/jw/util/auth/ldap/Auth.py
Normal file
140
src/python/jw/util/auth/ldap/Auth.py
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
from typing import Optional, Union
|
||||
|
||||
import ldap
|
||||
|
||||
from ...log import *
|
||||
from ...ldap import bind
|
||||
from ...Config import Config
|
||||
from .. import Access
|
||||
from .. import Auth as AuthBase
|
||||
from .. import Group as GroupBase
|
||||
from .. import User as UserBase
|
||||
from .. import ProjectFlags
|
||||
|
||||
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,
|
||||
cn: str,
|
||||
email: str
|
||||
):
|
||||
|
||||
self.__auth = auth
|
||||
self.__name = name
|
||||
self.__cn = cn
|
||||
self.__email = email
|
||||
self.__groups: Optional[list[GroupBase]] = None
|
||||
|
||||
def _name(self) -> str:
|
||||
return self.__name
|
||||
|
||||
def _groups(self) -> list[GroupBase]:
|
||||
raise NotImplementedError
|
||||
|
||||
def _email(self) -> str:
|
||||
return self.__email
|
||||
|
||||
def _display_name(self) -> str:
|
||||
return self.__cn
|
||||
|
||||
class Auth(AuthBase): # export
|
||||
|
||||
def __init__(self, conf: Config):
|
||||
super().__init__(conf)
|
||||
self.___users: Optional[dict[str, UserBase]] = None
|
||||
self.___user_by_email: Optional[dict[str, User]] = None
|
||||
self.__groups = None
|
||||
self.__current_user: User|None = None
|
||||
self.__user_base_dn = conf['user_base_dn']
|
||||
self.__conn = self.__bind()
|
||||
self.__dummy = self.load(conf, 'dummy')
|
||||
|
||||
def __bind(self):
|
||||
return bind(self.conf)
|
||||
|
||||
@property
|
||||
def __users(self) -> dict[str, UserBase]:
|
||||
if self.___users is None:
|
||||
ret: dict[str, UserBase] = {}
|
||||
ret_by_email: dict[str, User] = {}
|
||||
for res in self.__conn.find(
|
||||
self.__user_base_dn,
|
||||
ldap.SCOPE_SUBTREE,
|
||||
"objectClass=inetOrgPerson",
|
||||
('uid', 'cn', 'uidNumber', 'mail', 'maildrop')
|
||||
):
|
||||
try:
|
||||
display_name = None
|
||||
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')
|
||||
uidNumber = res[1]['uidNumber'][0].decode('utf-8')
|
||||
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')
|
||||
continue
|
||||
user = User(self, name=uid, cn=cn, email=emails[0])
|
||||
ret[uid] = user
|
||||
for email in emails:
|
||||
ret_by_email[email] = user
|
||||
except Exception as e:
|
||||
slog(WARNING, f'Exception {e}')
|
||||
raise
|
||||
continue
|
||||
for dummy_user in self.__dummy.users.values():
|
||||
ret[dummy_user.name] = dummy_user
|
||||
self.___users = ret
|
||||
self.___user_by_email = ret_by_email
|
||||
return self.___users
|
||||
|
||||
@property
|
||||
def __user_by_email(self) -> dict[str, UserBase]:
|
||||
if self.___user_by_email is None:
|
||||
self.__users
|
||||
return self.___user_by_email # type: ignore # We are sure that ___user_by_email is not None at this point
|
||||
|
||||
def _access(self, what: str, access_type: Optional[Access], who: User|GroupBase|None) -> bool: # type: ignore
|
||||
slog(WARNING, f'Returning False for {access_type} access to resource {what} by {who}')
|
||||
return False
|
||||
|
||||
def _user(self, name) -> UserBase:
|
||||
try:
|
||||
return self.__users[name]
|
||||
except:
|
||||
slog(ERR, f'No such user: "{name}"')
|
||||
raise
|
||||
|
||||
def _user_by_email(self, email: str) -> UserBase:
|
||||
return self.__user_by_email[email]
|
||||
|
||||
def _current_user(self) -> User:
|
||||
raise NotImplementedError
|
||||
|
||||
def _users(self) -> dict[str, UserBase]:
|
||||
return self.__users
|
||||
|
||||
def _projects(self, name, flags: ProjectFlags) -> list[str]:
|
||||
if flags & ProjectFlags.Contributing:
|
||||
# TODO: Ask LDAP
|
||||
slog(WARNING, f'Querying LDAP for projects a user contributes to is not implemented, ignoring')
|
||||
return []
|
||||
4
src/python/jw/util/auth/ldap/Makefile
Normal file
4
src/python/jw/util/auth/ldap/Makefile
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
TOPDIR = ../../../../../..
|
||||
|
||||
include $(TOPDIR)/make/proj.mk
|
||||
include $(JWBDIR)/make/py-mod.mk
|
||||
Loading…
Reference in a new issue