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

95 lines
2.4 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-29 19:57:33 +00:00
from ..db import Connection, TagValue, User, UserCreate
2022-03-30 02:02:45 +00:00
from ._common import Responses, get_current_user
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-30 10:53:52 +00:00
Connection.connect(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 15:34:50 +00:00
new_user = User.create(user=admin_user)
2022-03-30 01:51:43 +00:00
new_user.add_tags([TagValue.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,
2022-03-30 02:07:22 +00:00
status.HTTP_403_FORBIDDEN: Responses.NEEDS_PERMISSION,
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-30 02:02:45 +00:00
current_user: User = Depends(get_current_user),
2022-03-18 18:22:17 +00:00
):
2022-03-20 03:45:40 +00:00
"""
PUT ./config: Edit `kiwi-vpn` main config.
"""
2022-03-30 02:02:45 +00:00
# check permissions
if not current_user.has_tag(TagValue.admin):
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
2022-03-20 03:45:40 +00:00
# update config file, reconnect to database
2022-03-28 02:38:52 +00:00
config.save()
Connection.connect(config.db.uri)