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

View file

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

View file

@ -35,13 +35,13 @@ api.include_router(user.router)
@app.on_event("startup")
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)
# some testing
async for db in Connection.get():
print(schemas.User.get(db, "admin"))
print(schemas.User.get(db, "nonexistent"))
print(schemas.User.from_db(db, "admin"))
print(schemas.User.from_db(db, "nonexistent"))
def main() -> None:

View file

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

View file

@ -21,10 +21,10 @@ class Token(BaseModel):
@router.post("/auth", response_model=Token)
async def login(
form_data: OAuth2PasswordRequestForm = Depends(),
config: Config = Depends(Config.get),
config: Config = Depends(Config.load),
db: Session = Depends(Connection.get),
):
user = schemas.User.verify(
user = schemas.User.login(
db=db,
name=form_data.username,
password=form_data.password,
@ -38,17 +38,17 @@ async def login(
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"}
async def dep_get_current_user(
token: str = Depends(SCHEME),
db: Session = Depends(Connection.get),
config: Config = Depends(Config.get),
config: Config = Depends(Config.load),
):
username = await config.jwt.decode(token)
user = schemas.User.get(db, username)
username = await config.jwt.decode_token(token)
user = schemas.User.from_db(db, username)
if user is None:
raise HTTPException(