From bf8cb86cee30dc0edf2722a6362a6b81a5d2333f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Sun, 20 Mar 2022 02:32:40 +0000 Subject: [PATCH] brevity --- api/kiwi_vpn_api/main.py | 7 ++++--- api/kiwi_vpn_api/routers/_deps.py | 5 +++-- api/kiwi_vpn_api/routers/admin.py | 13 +++++++------ api/kiwi_vpn_api/routers/user.py | 19 ++++++++++--------- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/api/kiwi_vpn_api/main.py b/api/kiwi_vpn_api/main.py index 4d35e9d..4680d47 100755 --- a/api/kiwi_vpn_api/main.py +++ b/api/kiwi_vpn_api/main.py @@ -4,7 +4,8 @@ import uvicorn from fastapi import FastAPI from .config import Config, Settings -from .db import Connection, schemas +from .db import Connection +from .db.schemas import User from .routers import admin, user settings = Settings.get() @@ -40,8 +41,8 @@ async def on_startup() -> None: # some testing with Connection.use() as db: - print(schemas.User.from_db(db, "admin")) - print(schemas.User.from_db(db, "nonexistent")) + print(User.from_db(db, "admin")) + print(User.from_db(db, "nonexistent")) def main() -> None: diff --git a/api/kiwi_vpn_api/routers/_deps.py b/api/kiwi_vpn_api/routers/_deps.py index 4566a75..9a01759 100644 --- a/api/kiwi_vpn_api/routers/_deps.py +++ b/api/kiwi_vpn_api/routers/_deps.py @@ -3,7 +3,8 @@ from fastapi.security import OAuth2PasswordBearer from sqlalchemy.orm import Session from ..config import Config -from ..db import Connection, schemas +from ..db import Connection +from ..db.schemas import User oauth2_scheme = OAuth2PasswordBearer(tokenUrl="user/auth") @@ -44,6 +45,6 @@ async def get_current_user( return None username = await current_config.jwt.decode_token(token) - user = schemas.User.from_db(db, username) + user = User.from_db(db, username) return user diff --git a/api/kiwi_vpn_api/routers/admin.py b/api/kiwi_vpn_api/routers/admin.py index 062e7b5..a17806f 100644 --- a/api/kiwi_vpn_api/routers/admin.py +++ b/api/kiwi_vpn_api/routers/admin.py @@ -1,7 +1,8 @@ from fastapi import APIRouter, Depends, HTTPException, status from ..config import Config -from ..db import Connection, schemas +from ..db import Connection +from ..db.schemas import User, UserCapability, UserCreate from . import _deps router = APIRouter(prefix="/admin") @@ -16,7 +17,7 @@ router = APIRouter(prefix="/admin") ) async def install( config: Config, - admin_user: schemas.UserCreate, + admin_user: UserCreate, current_config: Config | None = Depends(Config.load), ): if current_config is not None: @@ -26,7 +27,7 @@ async def install( Connection.connect(await config.db.db_engine) with Connection.use() as db: - admin_user = schemas.User.create( + admin_user = User.create( db=db, user=admin_user, crypt_context=await config.crypto.crypt_context, @@ -34,7 +35,7 @@ async def install( admin_user.add_capabilities( db=db, - capabilities=[schemas.UserCapability.admin], + capabilities=[UserCapability.admin], ) @@ -50,13 +51,13 @@ async def install( async def set_config( new_config: Config, current_config: Config | None = Depends(Config.load), - current_user: schemas.User | None = Depends(_deps.get_current_user), + current_user: User | None = Depends(_deps.get_current_user), ): if current_config is None: raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST) if (current_user is None - or schemas.UserCapability.admin not in current_user.capabilities): + or UserCapability.admin not in current_user.capabilities): raise HTTPException(status_code=status.HTTP_403_FORBIDDEN) await new_config.save() diff --git a/api/kiwi_vpn_api/routers/user.py b/api/kiwi_vpn_api/routers/user.py index ed38653..5c24b62 100644 --- a/api/kiwi_vpn_api/routers/user.py +++ b/api/kiwi_vpn_api/routers/user.py @@ -4,7 +4,8 @@ from pydantic import BaseModel from sqlalchemy.orm import Session from ..config import Config -from ..db import Connection, schemas +from ..db import Connection +from ..db.schemas import User, UserCapability, UserCreate from . import _deps router = APIRouter(prefix="/user") @@ -24,7 +25,7 @@ async def login( if current_config is None: raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST) - user = schemas.User.login( + user = User.login( db=db, name=form_data.username, password=form_data.password, @@ -42,9 +43,9 @@ async def login( return {"access_token": access_token, "token_type": "bearer"} -@router.get("/current", response_model=schemas.User) +@router.get("/current", response_model=User) async def get_current_user( - current_user: schemas.User | None = Depends(_deps.get_current_user), + current_user: User | None = Depends(_deps.get_current_user), ): return current_user @@ -58,22 +59,22 @@ async def get_current_user( status.HTTP_403_FORBIDDEN: _deps.Responses.needs_admin, status.HTTP_409_CONFLICT: _deps.Responses.entry_exists, }, - response_model=schemas.User, + response_model=User, ) async def add_user( - user: schemas.UserCreate, + user: UserCreate, current_config: Config | None = Depends(Config.load), - current_user: schemas.User | None = Depends(_deps.get_current_user), + current_user: User | None = Depends(_deps.get_current_user), db: Session | None = Depends(Connection.get), ): if current_config is None: raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST) if (current_user is None - or schemas.UserCapability.admin not in current_user.capabilities): + or UserCapability.admin not in current_user.capabilities): raise HTTPException(status_code=status.HTTP_403_FORBIDDEN) - new_user = schemas.User.create( + new_user = User.create( db=db, user=user, crypt_context=await current_config.crypto.crypt_context,