diff --git a/api/kiwi_vpn_api/routers/_common.py b/api/kiwi_vpn_api/routers/_common.py index 3b76e30..5c8c4b9 100644 --- a/api/kiwi_vpn_api/routers/_common.py +++ b/api/kiwi_vpn_api/routers/_common.py @@ -58,6 +58,10 @@ async def get_current_config( ) -> Config: """ Get the current configuration if it exists. + + Status: + + - 400: `kiwi-vpn` not installed """ # fail if not configured @@ -73,6 +77,10 @@ async def get_current_user( ) -> User: """ Get the currently logged-in user if it exists. + + Status: + + - 403: invalid auth token, or user not found """ # don't use error 404 here - possible user enumeration @@ -89,23 +97,32 @@ async def get_current_user( async def get_user_by_name( user_name: str, - _: Config = Depends(get_current_config), -) -> User | None: +) -> User: """ Get a user by name. + + Status: + + - 404: user not found """ - return User.get(user_name) + # fail if device doesn't exist + if (user := User.get(user_name)) is None: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) + + return user async def get_device_by_id( device_id: int, - current_config: Config = Depends(get_current_config), -) -> Device | None: +) -> Device: + """ + Get a device by ID. - # can't connect to an unconfigured database - if current_config is None: - raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST) + Status: + + - 404: device not found + """ # fail if device doesn't exist if (device := Device.get(device_id)) is None: diff --git a/api/kiwi_vpn_api/routers/device.py b/api/kiwi_vpn_api/routers/device.py index 44cc144..710de22 100644 --- a/api/kiwi_vpn_api/routers/device.py +++ b/api/kiwi_vpn_api/routers/device.py @@ -37,10 +37,6 @@ async def add_device( if not current_user.can_create(Device, owner): raise HTTPException(status_code=status.HTTP_403_FORBIDDEN) - # fail if owner doesn't exist - if owner is None: - raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) - # create the new device new_device = Device.create( owner=owner,