diff --git a/api/kiwi_vpn_api/config.py b/api/kiwi_vpn_api/config.py index 10f4c06..bc7b45c 100644 --- a/api/kiwi_vpn_api/config.py +++ b/api/kiwi_vpn_api/config.py @@ -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)) diff --git a/api/kiwi_vpn_api/db/schemas.py b/api/kiwi_vpn_api/db/schemas.py index dbfb393..bc86a0b 100644 --- a/api/kiwi_vpn_api/db/schemas.py +++ b/api/kiwi_vpn_api/db/schemas.py @@ -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, diff --git a/api/kiwi_vpn_api/main.py b/api/kiwi_vpn_api/main.py index 122e54b..dc699d1 100755 --- a/api/kiwi_vpn_api/main.py +++ b/api/kiwi_vpn_api/main.py @@ -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: diff --git a/api/kiwi_vpn_api/routers/admin.py b/api/kiwi_vpn_api/routers/admin.py index a73eee7..07c98a6 100644 --- a/api/kiwi_vpn_api/routers/admin.py +++ b/api/kiwi_vpn_api/routers/admin.py @@ -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: diff --git a/api/kiwi_vpn_api/routers/user.py b/api/kiwi_vpn_api/routers/user.py index 977bd14..a80e557 100644 --- a/api/kiwi_vpn_api/routers/user.py +++ b/api/kiwi_vpn_api/routers/user.py @@ -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(