clearml-server/server/service_repo/auth/fixed_user.py

57 lines
1.3 KiB
Python
Raw Normal View History

2019-07-08 21:04:43 +00:00
import hashlib
from functools import lru_cache
from typing import Sequence, TypeVar
import attr
from config import config
from config.info import get_default_company
2019-07-08 21:04:43 +00:00
T = TypeVar("T", bound="FixedUser")
class FixedUsersError(Exception):
pass
2019-07-08 21:04:43 +00:00
@attr.s(auto_attribs=True)
class FixedUser:
username: str
password: str
name: str
company: str = get_default_company()
2019-07-08 21:04:43 +00:00
def __attrs_post_init__(self):
self.user_id = hashlib.md5(f"{self.company}:{self.username}".encode()).hexdigest()
2019-07-08 21:04:43 +00:00
@classmethod
def enabled(cls):
2019-07-08 21:04:43 +00:00
return config.get("apiserver.auth.fixed_users.enabled", False)
@classmethod
def validate(cls):
if not cls.enabled():
return
users = cls.from_config()
if len({user.username for user in users}) < len(users):
raise FixedUsersError(
"Duplicate user names found in fixed users configuration"
)
2019-07-08 21:04:43 +00:00
@classmethod
@lru_cache()
def from_config(cls) -> Sequence[T]:
return [
cls(**user) for user in config.get("apiserver.auth.fixed_users.users", [])
]
2019-07-08 21:04:43 +00:00
@classmethod
@lru_cache()
def get_by_username(cls, username) -> T:
return next(
(user for user in cls.from_config() if user.username == username), None
)
def __hash__(self):
return hash(self.user_id)