2022-03-16 00:51:40 +00:00
|
|
|
from secrets import token_hex
|
2022-03-16 00:23:57 +00:00
|
|
|
|
2022-03-15 22:42:29 +00:00
|
|
|
from fastapi import APIRouter, Depends, status
|
|
|
|
from fastapi.responses import JSONResponse
|
2022-03-15 17:38:24 +00:00
|
|
|
|
2022-03-16 00:51:40 +00:00
|
|
|
from ..config import (CONFIG_FILE, CRYPT_CONTEXT, DB, BaseConfig, get_config,
|
|
|
|
get_default_config, is_configured)
|
2022-03-15 22:42:29 +00:00
|
|
|
from ..db import Certificate, DistinguishedName, User, UserCapability
|
2022-03-15 17:38:24 +00:00
|
|
|
|
|
|
|
router = APIRouter(prefix="/install")
|
|
|
|
|
|
|
|
|
2022-03-16 00:51:40 +00:00
|
|
|
@router.get("/config/default", response_model=BaseConfig)
|
|
|
|
async def get_default_config(config: BaseConfig = Depends(get_default_config)):
|
2022-03-16 00:23:57 +00:00
|
|
|
return config
|
|
|
|
|
|
|
|
|
|
|
|
@router.get(
|
2022-03-16 00:51:40 +00:00
|
|
|
"/config",
|
2022-03-16 00:23:57 +00:00
|
|
|
response_model=BaseConfig,
|
|
|
|
responses={
|
|
|
|
status.HTTP_404_NOT_FOUND: {
|
|
|
|
"description": "Not configured",
|
|
|
|
"content": None,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
2022-03-16 00:51:40 +00:00
|
|
|
async def get_config(
|
2022-03-16 00:23:57 +00:00
|
|
|
is_configured: bool = Depends(is_configured),
|
2022-03-16 00:51:40 +00:00
|
|
|
config: BaseConfig = Depends(get_config),
|
2022-03-16 00:23:57 +00:00
|
|
|
):
|
|
|
|
if not is_configured:
|
|
|
|
return JSONResponse(status_code=status.HTTP_404_NOT_FOUND)
|
|
|
|
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
2022-03-16 00:51:40 +00:00
|
|
|
@router.put(
|
|
|
|
"/config",
|
2022-03-16 00:23:57 +00:00
|
|
|
responses={
|
|
|
|
status.HTTP_200_OK: {
|
|
|
|
"content": None,
|
|
|
|
},
|
|
|
|
status.HTTP_403_FORBIDDEN: {
|
|
|
|
"description": "Must be admin",
|
|
|
|
"content": None,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
2022-03-16 00:51:40 +00:00
|
|
|
async def set_config(
|
2022-03-16 00:23:57 +00:00
|
|
|
config: BaseConfig,
|
|
|
|
is_configured: bool = Depends(is_configured),
|
|
|
|
):
|
|
|
|
if is_configured:
|
|
|
|
return JSONResponse(status_code=status.HTTP_403_FORBIDDEN)
|
|
|
|
|
2022-03-16 00:51:40 +00:00
|
|
|
if config.jwt.secret is None:
|
|
|
|
config.jwt.secret = token_hex(32)
|
|
|
|
|
2022-03-16 00:23:57 +00:00
|
|
|
config.save(CONFIG_FILE)
|
|
|
|
|
|
|
|
|
2022-03-15 17:38:24 +00:00
|
|
|
async def is_installed():
|
|
|
|
return DB.table_exists(User)
|
|
|
|
|
|
|
|
|
2022-03-15 22:42:29 +00:00
|
|
|
@router.get("/check_installed", responses={
|
|
|
|
status.HTTP_200_OK: {
|
|
|
|
"model": bool,
|
|
|
|
},
|
|
|
|
})
|
2022-03-15 17:38:24 +00:00
|
|
|
async def check_installed(is_installed: bool = Depends(is_installed)):
|
2022-03-15 22:42:29 +00:00
|
|
|
return is_installed
|
|
|
|
|
|
|
|
|
2022-03-16 00:23:57 +00:00
|
|
|
@router.get(
|
|
|
|
"/create_db",
|
|
|
|
responses={
|
|
|
|
status.HTTP_200_OK: {
|
|
|
|
"content": None,
|
|
|
|
},
|
|
|
|
status.HTTP_400_BAD_REQUEST: {
|
|
|
|
"description": "Could not create Database",
|
|
|
|
"content": None,
|
|
|
|
},
|
2022-03-15 22:42:29 +00:00
|
|
|
},
|
2022-03-16 00:23:57 +00:00
|
|
|
)
|
2022-03-15 17:38:24 +00:00
|
|
|
async def create_db(is_installed: bool = Depends(is_installed)):
|
|
|
|
if is_installed:
|
2022-03-15 22:42:29 +00:00
|
|
|
return JSONResponse(
|
|
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
|
|
)
|
2022-03-15 17:38:24 +00:00
|
|
|
|
|
|
|
DB.create_tables([Certificate, DistinguishedName, User, UserCapability])
|
|
|
|
|
|
|
|
admin = User.create(name="admin", password=CRYPT_CONTEXT.hash("secret"))
|
|
|
|
UserCapability.create(user=admin, capability="admin")
|
|
|
|
|
|
|
|
User.create(name="johndoe", password=CRYPT_CONTEXT.hash("secret"))
|
|
|
|
|
|
|
|
DB.close()
|