Compare commits
No commits in common. "ca955d11044af05e3e9c22b95c47c643ede798df" and "b7179e7cfc4cc73e8527187f824b77e5791adaed" have entirely different histories.
ca955d1104
...
b7179e7cfc
5 changed files with 29 additions and 24 deletions
|
|
@ -173,7 +173,14 @@ class CryptoConfig(BaseModel):
|
||||||
schemes: list[str] = ["bcrypt"]
|
schemes: list[str] = ["bcrypt"]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def crypt_context(self) -> CryptContext:
|
def crypt_context_sync(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",
|
||||||
|
|
@ -189,35 +196,33 @@ class Config(BaseModel):
|
||||||
jwt: JWTConfig = Field(default_factory=JWTConfig)
|
jwt: JWTConfig = Field(default_factory=JWTConfig)
|
||||||
crypto: CryptoConfig = Field(default_factory=CryptoConfig)
|
crypto: CryptoConfig = Field(default_factory=CryptoConfig)
|
||||||
|
|
||||||
__singleton: Config | None = None
|
@staticmethod
|
||||||
|
def load_sync() -> Config | None:
|
||||||
@classmethod
|
|
||||||
def load(cls) -> Config | None:
|
|
||||||
"""
|
"""
|
||||||
Load configuration from config file
|
Load configuration from config file
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if cls.__singleton is not None:
|
|
||||||
return cls.__singleton
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(Settings.get().config_file, "r") as config_file:
|
with open(Settings.get().config_file, "r") as config_file:
|
||||||
cls.__singleton = Config.parse_obj(json.load(config_file))
|
return Config.parse_obj(json.load(config_file))
|
||||||
return cls.__singleton
|
|
||||||
|
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@classmethod
|
@staticmethod
|
||||||
@property
|
async def load() -> Config | None:
|
||||||
def _(cls) -> Config | None:
|
|
||||||
"""
|
"""
|
||||||
Shorthand for load()
|
Load configuration from config file
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return cls.load()
|
try:
|
||||||
|
with open(Settings.get().config_file, "r") as config_file:
|
||||||
|
return Config.parse_obj(json.load(config_file))
|
||||||
|
|
||||||
def save(self) -> None:
|
except FileNotFoundError:
|
||||||
|
return None
|
||||||
|
|
||||||
|
async def save(self) -> None:
|
||||||
"""
|
"""
|
||||||
Save configuration to config file
|
Save configuration to config 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
|
crypt_context = Config.load_sync().crypto.crypt_context_sync
|
||||||
|
|
||||||
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
|
||||||
|
|
@ -141,10 +141,10 @@ class UserCreate(UserBase):
|
||||||
if (password_clear := values.get("password_clear")) is None:
|
if (password_clear := values.get("password_clear")) is None:
|
||||||
raise ValueError("No password to hash")
|
raise ValueError("No password to hash")
|
||||||
|
|
||||||
if (current_config := Config._) is None:
|
if (current_config := Config.load_sync()) is None:
|
||||||
raise ValueError("Not configured")
|
raise ValueError("Not configured")
|
||||||
|
|
||||||
values["password"] = current_config.crypto.crypt_context.hash(
|
values["password"] = current_config.crypto.crypt_context_sync.hash(
|
||||||
password_clear)
|
password_clear)
|
||||||
|
|
||||||
return values
|
return values
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,7 @@ app.include_router(main_router)
|
||||||
@app.on_event("startup")
|
@app.on_event("startup")
|
||||||
async def on_startup() -> None:
|
async def on_startup() -> None:
|
||||||
# check if configured
|
# check if configured
|
||||||
if (current_config := Config._) is not None:
|
if (current_config := await Config.load()) is not None:
|
||||||
# connect to database
|
# connect to database
|
||||||
Connection.connect(current_config.db.uri)
|
Connection.connect(current_config.db.uri)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@ async def initial_configure(
|
||||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
# create config file, connect to database
|
# create config file, connect to database
|
||||||
config.save()
|
await config.save()
|
||||||
Connection.connect(current_config.db.uri)
|
Connection.connect(current_config.db.uri)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -90,5 +90,5 @@ async def set_config(
|
||||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
# update config file, reconnect to database
|
# update config file, reconnect to database
|
||||||
new_config.save()
|
await new_config.save()
|
||||||
Connection.connect(current_config.db.uri)
|
Connection.connect(current_config.db.uri)
|
||||||
|
|
|
||||||
|
|
@ -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=current_config.crypto.crypt_context,
|
crypt_context=await 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=current_config.crypto.crypt_context,
|
crypt_context=await current_config.crypto.crypt_context,
|
||||||
)
|
)
|
||||||
|
|
||||||
# fail if creation was unsuccessful
|
# fail if creation was unsuccessful
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue