advent22/api/advent22_api/routers/user.py

32 lines
784 B
Python
Raw Normal View History

2022-11-15 22:17:32 +00:00
from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.security import HTTPBasic, HTTPBasicCredentials
from ..config import Config, get_config
router = APIRouter(prefix="/user", tags=["user"])
security = HTTPBasic()
async def is_admin(
credentials: HTTPBasicCredentials = Depends(security),
config: Config = Depends(get_config),
) -> bool:
if config.admin.name == credentials.username:
if config.admin.password == credentials.password:
return True
return False
async def require_admin(
is_admin: bool = Depends(is_admin),
) -> None:
if not is_admin:
raise HTTPException(status.HTTP_401_UNAUTHORIZED)
@router.get("/admin")
def check_admin(
_: None = Depends(require_admin),
) -> None:
return None