some renames

This commit is contained in:
Jörn-Michael Miehe 2022-03-19 02:38:32 +00:00
parent ba68692b9d
commit 4959f1987c
5 changed files with 18 additions and 18 deletions

View file

@ -47,7 +47,7 @@ class JWTConfig(BaseModel):
hash_algorithm: str = ALGORITHMS.HS256 hash_algorithm: str = ALGORITHMS.HS256
expiry_minutes: int = 30 expiry_minutes: int = 30
async def encode( async def create_token(
self, self,
username: str, username: str,
expiry_minutes: int | None = None, expiry_minutes: int | None = None,
@ -64,7 +64,7 @@ class JWTConfig(BaseModel):
algorithm=self.hash_algorithm, algorithm=self.hash_algorithm,
) )
async def decode( async def decode_token(
self, self,
token: str, token: str,
) -> str | None: ) -> str | None:
@ -112,7 +112,7 @@ class Config(BaseModel):
crypto: CryptoConfig = Field(default_factory=CryptoConfig) crypto: CryptoConfig = Field(default_factory=CryptoConfig)
@staticmethod @staticmethod
async def get() -> Config | None: async def load() -> Config | None:
try: try:
with open(Settings.get().config_file, "r") as config_file: with open(Settings.get().config_file, "r") as config_file:
return Config.parse_obj(json.load(config_file)) return Config.parse_obj(json.load(config_file))
@ -120,6 +120,6 @@ class Config(BaseModel):
except FileNotFoundError: except FileNotFoundError:
return None return None
async def set(self) -> None: async def save(self) -> None:
with open(Settings.get().config_file, "w") as config_file: with open(Settings.get().config_file, "w") as config_file:
config_file.write(self.json(indent=2)) config_file.write(self.json(indent=2))

View file

@ -54,7 +54,7 @@ class User(UserBase):
orm_mode = True orm_mode = True
@classmethod @classmethod
def get( def from_db(
cls, cls,
db: Session, db: Session,
name: str, name: str,
@ -70,7 +70,7 @@ class User(UserBase):
return cls.from_orm(user) return cls.from_orm(user)
@classmethod @classmethod
def verify( def login(
cls, cls,
db: Session, db: Session,
name: str, name: str,

View file

@ -35,13 +35,13 @@ api.include_router(user.router)
@app.on_event("startup") @app.on_event("startup")
async def on_startup() -> None: async def on_startup() -> None:
if (current_config := await Config.get()) is not None: if (current_config := await Config.load()) is not None:
Connection.connect(await current_config.db.db_engine) Connection.connect(await current_config.db.db_engine)
# some testing # some testing
async for db in Connection.get(): async for db in Connection.get():
print(schemas.User.get(db, "admin")) print(schemas.User.from_db(db, "admin"))
print(schemas.User.get(db, "nonexistent")) print(schemas.User.from_db(db, "nonexistent"))
def main() -> None: def main() -> None:

View file

@ -23,7 +23,7 @@ router = APIRouter(prefix="/admin")
) )
async def set_config( async def set_config(
new_config: Config, new_config: Config,
current_config: Config | None = Depends(Config.get), current_config: Config | None = Depends(Config.load),
): ):
if current_config is not None: if current_config is not None:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN) raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
@ -31,7 +31,7 @@ async def set_config(
if new_config.jwt.secret is None: if new_config.jwt.secret is None:
new_config.jwt.secret = token_hex(32) new_config.jwt.secret = token_hex(32)
await new_config.set() await new_config.save()
Connection.connect(await new_config.db.db_engine) Connection.connect(await new_config.db.db_engine)
@ -50,7 +50,7 @@ async def set_config(
async def add_user( async def add_user(
user_name: str, user_name: str,
user_password: str, user_password: str,
current_config: Config | None = Depends(Config.get), current_config: Config | None = Depends(Config.load),
db: Session | None = Depends(Connection.get), db: Session | None = Depends(Connection.get),
): ):
if current_config is None: if current_config is None:

View file

@ -21,10 +21,10 @@ class Token(BaseModel):
@router.post("/auth", response_model=Token) @router.post("/auth", response_model=Token)
async def login( async def login(
form_data: OAuth2PasswordRequestForm = Depends(), form_data: OAuth2PasswordRequestForm = Depends(),
config: Config = Depends(Config.get), config: Config = Depends(Config.load),
db: Session = Depends(Connection.get), db: Session = Depends(Connection.get),
): ):
user = schemas.User.verify( user = schemas.User.login(
db=db, db=db,
name=form_data.username, name=form_data.username,
password=form_data.password, password=form_data.password,
@ -38,17 +38,17 @@ async def login(
headers={"WWW-Authenticate": "Bearer"}, headers={"WWW-Authenticate": "Bearer"},
) )
access_token = await config.jwt.encode(user.name) access_token = await config.jwt.create_token(user.name)
return {"access_token": access_token, "token_type": "bearer"} return {"access_token": access_token, "token_type": "bearer"}
async def dep_get_current_user( async def dep_get_current_user(
token: str = Depends(SCHEME), token: str = Depends(SCHEME),
db: Session = Depends(Connection.get), db: Session = Depends(Connection.get),
config: Config = Depends(Config.get), config: Config = Depends(Config.load),
): ):
username = await config.jwt.decode(token) username = await config.jwt.decode_token(token)
user = schemas.User.get(db, username) user = schemas.User.from_db(db, username)
if user is None: if user is None:
raise HTTPException( raise HTTPException(