From ca955d11044af05e3e9c22b95c47c643ede798df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Mon, 28 Mar 2022 02:23:00 +0000 Subject: [PATCH] fix for crypt_context and load() --- api/kiwi_vpn_api/config.py | 26 +++++++------------------- api/kiwi_vpn_api/db/user.py | 4 ++-- api/kiwi_vpn_api/routers/user.py | 4 ++-- 3 files changed, 11 insertions(+), 23 deletions(-) diff --git a/api/kiwi_vpn_api/config.py b/api/kiwi_vpn_api/config.py index b90c103..531b69a 100644 --- a/api/kiwi_vpn_api/config.py +++ b/api/kiwi_vpn_api/config.py @@ -173,14 +173,7 @@ class CryptoConfig(BaseModel): schemes: list[str] = ["bcrypt"] @property - def crypt_context_sync(self) -> CryptContext: - return CryptContext( - schemes=self.schemes, - deprecated="auto", - ) - - @property - async def crypt_context(self) -> CryptContext: + def crypt_context(self) -> CryptContext: return CryptContext( schemes=self.schemes, deprecated="auto", @@ -199,8 +192,7 @@ class Config(BaseModel): __singleton: Config | None = None @classmethod - @property - def _(cls) -> Config | None: + def load(cls) -> Config | None: """ Load configuration from config file """ @@ -216,18 +208,14 @@ class Config(BaseModel): except FileNotFoundError: return None - @staticmethod - async def load() -> Config | None: + @classmethod + @property + def _(cls) -> Config | None: """ - Load configuration from config file + Shorthand for load() """ - try: - with open(Settings.get().config_file, "r") as config_file: - return Config.parse_obj(json.load(config_file)) - - except FileNotFoundError: - return None + return cls.load() def save(self) -> None: """ diff --git a/api/kiwi_vpn_api/db/user.py b/api/kiwi_vpn_api/db/user.py index f13ccc1..3b462c9 100644 --- a/api/kiwi_vpn_api/db/user.py +++ b/api/kiwi_vpn_api/db/user.py @@ -77,7 +77,7 @@ class User(UserBase, table=True): Authenticate with name/password against users in database. """ - crypt_context = Config._.crypto.crypt_context_sync + crypt_context = Config._.crypto.crypt_context if (user := cls.get(name)) is None: # nonexistent user, fake doing password verification @@ -144,7 +144,7 @@ class UserCreate(UserBase): if (current_config := Config._) is None: raise ValueError("Not configured") - values["password"] = current_config.crypto.crypt_context_sync.hash( + values["password"] = current_config.crypto.crypt_context.hash( password_clear) return values diff --git a/api/kiwi_vpn_api/routers/user.py b/api/kiwi_vpn_api/routers/user.py index 627c5fc..2deace2 100644 --- a/api/kiwi_vpn_api/routers/user.py +++ b/api/kiwi_vpn_api/routers/user.py @@ -43,7 +43,7 @@ async def login( if not user.authenticate( db=db, password=form_data.password, - crypt_context=await current_config.crypto.crypt_context, + crypt_context=current_config.crypto.crypt_context, ): # authentication failed raise HTTPException( @@ -93,7 +93,7 @@ async def add_user( new_user = User.create( db=db, user=user, - crypt_context=await current_config.crypto.crypt_context, + crypt_context=current_config.crypto.crypt_context, ) # fail if creation was unsuccessful