Compare commits

..

2 commits

5 changed files with 24 additions and 29 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",
@ -196,33 +189,35 @@ class Config(BaseModel):
jwt: JWTConfig = Field(default_factory=JWTConfig)
crypto: CryptoConfig = Field(default_factory=CryptoConfig)
@staticmethod
def load_sync() -> Config | None:
__singleton: Config | None = None
@classmethod
def load(cls) -> Config | None:
"""
Load configuration from config file
"""
if cls.__singleton is not None:
return cls.__singleton
try:
with open(Settings.get().config_file, "r") as config_file:
return Config.parse_obj(json.load(config_file))
cls.__singleton = Config.parse_obj(json.load(config_file))
return cls.__singleton
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))
return cls.load()
except FileNotFoundError:
return None
async def save(self) -> None:
def save(self) -> None:
"""
Save configuration to config file
"""

View file

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

@ -41,7 +41,7 @@ app.include_router(main_router)
@app.on_event("startup")
async def on_startup() -> None:
# check if configured
if (current_config := await Config.load()) is not None:
if (current_config := Config._) is not None:
# connect to database
Connection.connect(current_config.db.uri)

View file

@ -33,7 +33,7 @@ async def initial_configure(
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
# create config file, connect to database
await config.save()
config.save()
Connection.connect(current_config.db.uri)
@ -90,5 +90,5 @@ async def set_config(
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
# update config file, reconnect to database
await new_config.save()
new_config.save()
Connection.connect(current_config.db.uri)

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