kiwi-vpn/api/kiwi_vpn_api/routers/admin.py

91 lines
2.3 KiB
Python
Raw Normal View History

2022-03-20 03:45:40 +00:00
"""
/admin endpoints.
"""
2022-03-18 18:22:17 +00:00
from fastapi import APIRouter, Depends, HTTPException, status
2022-03-28 01:52:56 +00:00
from sqlmodel import select
2022-03-18 18:22:17 +00:00
2022-03-18 22:43:02 +00:00
from ..config import Config
2022-03-28 21:41:49 +00:00
from ..db import Connection, User, UserCapabilityType, UserCreate
2022-03-29 00:13:38 +00:00
from ._common import Responses, current_user_is_admin
2022-03-18 18:22:17 +00:00
2022-03-24 23:45:01 +00:00
router = APIRouter(prefix="/admin", tags=["admin"])
2022-03-18 18:22:17 +00:00
@router.put(
2022-03-28 01:52:56 +00:00
"/install/config",
2022-03-18 18:22:17 +00:00
responses={
2022-03-20 03:45:40 +00:00
status.HTTP_200_OK: Responses.OK,
status.HTTP_400_BAD_REQUEST: Responses.INSTALLED,
2022-03-18 18:22:17 +00:00
},
)
2022-03-28 01:52:56 +00:00
async def initial_configure(
2022-03-19 17:11:52 +00:00
config: Config,
2022-03-19 02:38:32 +00:00
current_config: Config | None = Depends(Config.load),
2022-03-18 18:22:17 +00:00
):
2022-03-20 03:45:40 +00:00
"""
2022-03-28 01:52:56 +00:00
PUT ./install/config: Configure `kiwi-vpn`.
2022-03-20 03:45:40 +00:00
"""
2022-03-28 01:52:56 +00:00
# fail if already configured
2022-03-18 18:22:17 +00:00
if current_config is not None:
2022-03-19 17:11:52 +00:00
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
2022-03-18 18:22:17 +00:00
2022-03-20 03:45:40 +00:00
# create config file, connect to database
2022-03-28 02:15:42 +00:00
config.save()
2022-03-28 02:00:58 +00:00
Connection.connect(current_config.db.uri)
2022-03-18 18:22:17 +00:00
2022-03-28 01:52:56 +00:00
@router.put(
"/install/admin",
responses={
status.HTTP_200_OK: Responses.OK,
status.HTTP_400_BAD_REQUEST: Responses.NOT_INSTALLED,
status.HTTP_409_CONFLICT: Responses.ENTRY_EXISTS,
},
)
async def create_initial_admin(
admin_user: UserCreate,
current_config: Config | None = Depends(Config.load),
):
"""
PUT ./install/admin: Create the first administrative user.
"""
# fail if not configured
if current_config is None:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
2022-03-28 22:25:37 +00:00
# fail if any user exists
2022-03-28 01:52:56 +00:00
with Connection.session as db:
2022-03-28 02:03:31 +00:00
if db.exec(select(User).limit(1)).first() is not None:
2022-03-28 01:52:56 +00:00
raise HTTPException(status_code=status.HTTP_409_CONFLICT)
# create an administrative user
2022-03-29 00:01:28 +00:00
new_user = User.create(admin_user)
2022-03-28 22:20:25 +00:00
new_user.set_capabilities([UserCapabilityType.admin])
2022-03-28 01:52:56 +00:00
new_user.update()
2022-03-20 13:14:12 +00:00
2022-03-19 17:11:52 +00:00
@router.put(
"/config",
2022-03-18 18:22:17 +00:00
responses={
2022-03-20 03:45:40 +00:00
status.HTTP_200_OK: Responses.OK,
status.HTTP_400_BAD_REQUEST: Responses.NOT_INSTALLED,
status.HTTP_401_UNAUTHORIZED: Responses.NEEDS_USER,
status.HTTP_403_FORBIDDEN: Responses.NEEDS_ADMIN,
2022-03-18 18:22:17 +00:00
},
)
2022-03-19 17:11:52 +00:00
async def set_config(
2022-03-28 02:38:52 +00:00
config: Config,
2022-03-29 00:13:38 +00:00
_: User = Depends(current_user_is_admin),
2022-03-18 18:22:17 +00:00
):
2022-03-20 03:45:40 +00:00
"""
PUT ./config: Edit `kiwi-vpn` main config.
"""
# update config file, reconnect to database
2022-03-28 02:38:52 +00:00
config.save()
Connection.connect(config.db.uri)