mirror of
https://code.lenaisten.de/Lenaisten/advent22.git
synced 2024-11-23 16:23:00 +00:00
32 lines
784 B
Python
32 lines
784 B
Python
|
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
|