documentation

This commit is contained in:
Jörn-Michael Miehe 2023-09-08 19:44:41 +00:00
parent 11daf2bf8b
commit 21279f747c
2 changed files with 24 additions and 11 deletions

View file

@ -11,13 +11,14 @@ security = HTTPBasic()
async def user_is_admin( async def user_is_admin(
credentials: HTTPBasicCredentials = Depends(security), credentials: HTTPBasicCredentials = Depends(security),
config: Config = Depends(Config.get_config), cfg: Config = Depends(Config.get_config),
) -> bool: ) -> bool:
username_correct = secrets.compare_digest(credentials.username, config.admin.name) """
True iff der user "admin" ist
"""
password_correct = secrets.compare_digest( username_correct = secrets.compare_digest(credentials.username, cfg.admin.name)
credentials.password, config.admin.password password_correct = secrets.compare_digest(credentials.password, cfg.admin.password)
)
return username_correct and password_correct return username_correct and password_correct
@ -25,11 +26,19 @@ async def user_is_admin(
async def require_admin( async def require_admin(
is_admin: bool = Depends(user_is_admin), is_admin: bool = Depends(user_is_admin),
) -> None: ) -> None:
"""
HTTP 401 iff der user nicht "admin" ist
"""
if not is_admin: if not is_admin:
raise HTTPException(status.HTTP_401_UNAUTHORIZED) raise HTTPException(status.HTTP_401_UNAUTHORIZED)
async def user_visible_days() -> int: async def user_visible_doors() -> int:
"""
Anzahl der user-sichtbaren Türchen
"""
today = date.today() today = date.today()
if today.month == 12: if today.month == 12:
@ -41,7 +50,11 @@ async def user_visible_days() -> int:
return 0 return 0
async def user_can_view_day( async def user_can_view_door(
day: int, day: int,
) -> bool: ) -> bool:
return day < await user_visible_days() """
True iff das Türchen von Tag `day` user-sichtbar ist
"""
return day < await user_visible_doors()

View file

@ -7,7 +7,7 @@ from PIL import Image
from ..core.config import Config from ..core.config import Config
from ..core.depends import get_image, get_part, shuffle_solution from ..core.depends import get_image, get_part, shuffle_solution
from ..core.image_helpers import api_return_image from ..core.image_helpers import api_return_image
from ._security import user_can_view_day, user_is_admin, user_visible_days from ._security import user_can_view_door, user_is_admin, user_visible_doors
router = APIRouter(prefix="/days", tags=["days"]) router = APIRouter(prefix="/days", tags=["days"])
@ -36,7 +36,7 @@ async def get_visible_days() -> int:
Sichtbare Türchen Sichtbare Türchen
""" """
return await user_visible_days() return await user_visible_doors()
@router.get("/part/{day}") @router.get("/part/{day}")
@ -56,7 +56,7 @@ async def get_part_for_day(
) )
async def get_image_for_day( async def get_image_for_day(
image: Image.Image = Depends(get_image), image: Image.Image = Depends(get_image),
can_view: bool = Depends(user_can_view_day), can_view: bool = Depends(user_can_view_door),
is_admin: bool = Depends(user_is_admin), is_admin: bool = Depends(user_is_admin),
) -> StreamingResponse: ) -> StreamingResponse:
""" """