🔒️ Add optionally_authenticated permission

This commit is contained in:
Brian Wiborg 2025-10-11 01:07:55 +02:00
parent 1c42b44d41
commit 643a6b2eb7
No known key found for this signature in database

View file

@ -1,6 +1,6 @@
import time import time
from enum import Enum from enum import Enum
from typing import Any, Dict, List from typing import Any, Dict, List, Optional
import jwt import jwt
import settings import settings
@ -124,6 +124,12 @@ async def get_current_user(token: str = Depends(oauth2_scheme)) -> User:
return user return user
async def optionally_authenticated(token: Optional[str] = Depends(oauth2_scheme)) -> Optional[User]:
if token is None:
return None
return await get_current_user(token)
async def require_authenticated(current_user: User = Depends(get_current_user)) -> User: async def require_authenticated(current_user: User = Depends(get_current_user)) -> User:
"""Ensure the current user is an admin.""" """Ensure the current user is an admin."""
if not current_user: if not current_user: