database creation
This commit is contained in:
parent
a8815a193f
commit
2861b2fb0d
2 changed files with 37 additions and 27 deletions
|
@ -29,7 +29,8 @@ class DBConfig(BaseModel):
|
|||
db_type: DBType = DBType.sqlite
|
||||
|
||||
@property
|
||||
def database(self) -> Database:
|
||||
async def database(self) -> Database:
|
||||
if self.db_type == DBType.sqlite:
|
||||
return SqliteDatabase("tmp/vpn.db")
|
||||
|
||||
|
||||
|
@ -43,7 +44,7 @@ class CryptoConfig(BaseModel):
|
|||
schemes: list[str] = ["bcrypt"]
|
||||
|
||||
@property
|
||||
def cryptContext(self) -> CryptContext:
|
||||
async def cryptContext(self) -> CryptContext:
|
||||
return CryptContext(schemes=self.schemes, deprecated="auto")
|
||||
|
||||
|
||||
|
|
|
@ -2,10 +2,10 @@ import json
|
|||
from pathlib import Path
|
||||
from secrets import token_hex
|
||||
|
||||
from fastapi import APIRouter, Depends, status
|
||||
from fastapi.responses import JSONResponse
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from peewee import Database
|
||||
|
||||
from ..config import CRYPT_CONTEXT, DB, BaseConfig
|
||||
from ..config import CRYPT_CONTEXT, BaseConfig
|
||||
from ..db import Certificate, DistinguishedName, User, UserCapability
|
||||
|
||||
router = APIRouter(prefix="/install")
|
||||
|
@ -42,7 +42,7 @@ async def get_config(
|
|||
has_config: bool = Depends(has_config),
|
||||
):
|
||||
if has_config:
|
||||
return JSONResponse(status_code=status.HTTP_403_FORBIDDEN)
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
||||
|
||||
return config
|
||||
|
||||
|
@ -64,7 +64,7 @@ async def set_config(
|
|||
has_config: bool = Depends(has_config),
|
||||
):
|
||||
if has_config:
|
||||
return JSONResponse(status_code=status.HTTP_403_FORBIDDEN)
|
||||
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
||||
|
||||
if config.jwt.secret is None:
|
||||
config.jwt.secret = token_hex(32)
|
||||
|
@ -73,42 +73,51 @@ async def set_config(
|
|||
kv.write(config.json(indent=2))
|
||||
|
||||
|
||||
async def is_installed():
|
||||
return DB.table_exists(User)
|
||||
async def connect_db(config: BaseConfig = Depends(load_config)) -> Database:
|
||||
db = await config.db.database
|
||||
db.connect()
|
||||
return db
|
||||
|
||||
|
||||
@router.get("/check_installed", responses={
|
||||
async def has_tables(db: Database = Depends(connect_db)):
|
||||
return db.table_exists(User)
|
||||
|
||||
|
||||
@router.get("/db", responses={
|
||||
status.HTTP_200_OK: {
|
||||
"model": bool,
|
||||
},
|
||||
})
|
||||
async def check_installed(is_installed: bool = Depends(is_installed)):
|
||||
return is_installed
|
||||
async def check_db(
|
||||
has_tables: bool = Depends(has_tables),
|
||||
):
|
||||
return has_tables
|
||||
|
||||
|
||||
@router.get(
|
||||
"/create_db",
|
||||
@router.put(
|
||||
"/db",
|
||||
responses={
|
||||
status.HTTP_200_OK: {
|
||||
"content": None,
|
||||
},
|
||||
status.HTTP_400_BAD_REQUEST: {
|
||||
"description": "Could not create Database",
|
||||
"description": "Database exists",
|
||||
"content": None,
|
||||
},
|
||||
},
|
||||
)
|
||||
async def create_db(is_installed: bool = Depends(is_installed)):
|
||||
if is_installed:
|
||||
return JSONResponse(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
async def create_db(
|
||||
admin_name: str,
|
||||
admin_password: str,
|
||||
db: Database = Depends(connect_db),
|
||||
):
|
||||
if await has_tables(db):
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
db.create_tables([Certificate, DistinguishedName, User, UserCapability])
|
||||
|
||||
admin = User.create(
|
||||
name=admin_name,
|
||||
password=CRYPT_CONTEXT.hash(admin_password),
|
||||
)
|
||||
|
||||
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()
|
||||
|
|
Loading…
Reference in a new issue