fix for crypt_context and load()

This commit is contained in:
Jörn-Michael Miehe 2022-03-28 02:23:00 +00:00
parent 3d83ddb6cc
commit ca955d1104
3 changed files with 11 additions and 23 deletions

View file

@ -173,14 +173,7 @@ class CryptoConfig(BaseModel):
schemes: list[str] = ["bcrypt"] schemes: list[str] = ["bcrypt"]
@property @property
def crypt_context_sync(self) -> CryptContext: def crypt_context(self) -> CryptContext:
return CryptContext(
schemes=self.schemes,
deprecated="auto",
)
@property
async def crypt_context(self) -> CryptContext:
return CryptContext( return CryptContext(
schemes=self.schemes, schemes=self.schemes,
deprecated="auto", deprecated="auto",
@ -199,8 +192,7 @@ class Config(BaseModel):
__singleton: Config | None = None __singleton: Config | None = None
@classmethod @classmethod
@property def load(cls) -> Config | None:
def _(cls) -> Config | None:
""" """
Load configuration from config file Load configuration from config file
""" """
@ -216,18 +208,14 @@ class Config(BaseModel):
except FileNotFoundError: except FileNotFoundError:
return None return None
@staticmethod @classmethod
async def load() -> Config | None: @property
def _(cls) -> Config | None:
""" """
Load configuration from config file Shorthand for load()
""" """
try: return cls.load()
with open(Settings.get().config_file, "r") as config_file:
return Config.parse_obj(json.load(config_file))
except FileNotFoundError:
return None
def save(self) -> None: def save(self) -> None:
""" """

View file

@ -77,7 +77,7 @@ class User(UserBase, table=True):
Authenticate with name/password against users in database. 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: if (user := cls.get(name)) is None:
# nonexistent user, fake doing password verification # nonexistent user, fake doing password verification
@ -144,7 +144,7 @@ class UserCreate(UserBase):
if (current_config := Config._) is None: if (current_config := Config._) is None:
raise ValueError("Not configured") raise ValueError("Not configured")
values["password"] = current_config.crypto.crypt_context_sync.hash( values["password"] = current_config.crypto.crypt_context.hash(
password_clear) password_clear)
return values return values

View file

@ -43,7 +43,7 @@ async def login(
if not user.authenticate( if not user.authenticate(
db=db, db=db,
password=form_data.password, password=form_data.password,
crypt_context=await current_config.crypto.crypt_context, crypt_context=current_config.crypto.crypt_context,
): ):
# authentication failed # authentication failed
raise HTTPException( raise HTTPException(
@ -93,7 +93,7 @@ async def add_user(
new_user = User.create( new_user = User.create(
db=db, db=db,
user=user, user=user,
crypt_context=await current_config.crypto.crypt_context, crypt_context=current_config.crypto.crypt_context,
) )
# fail if creation was unsuccessful # fail if creation was unsuccessful