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"]
@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:
"""

View file

@ -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

View file

@ -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