47 lines
1.1 KiB
Python
47 lines
1.1 KiB
Python
"""
|
|
FastAPI dependency injection functions.
|
|
"""
|
|
|
|
from fastapi import (
|
|
Depends,
|
|
HTTPException,
|
|
status,
|
|
)
|
|
from fastapi.security import (
|
|
HTTPAuthorizationCredentials,
|
|
HTTPBearer,
|
|
)
|
|
from .security import decode_token
|
|
|
|
|
|
# HTTP Bearer token authentication
|
|
security = HTTPBearer()
|
|
|
|
|
|
async def get_current_user(
|
|
credentials: HTTPAuthorizationCredentials = Depends(security),
|
|
) -> str:
|
|
"""
|
|
FastAPI dependency to extract and
|
|
verify the current authenticated user.
|
|
"""
|
|
try:
|
|
payload = decode_token(credentials.credentials)
|
|
email: str | None = payload.get("sub")
|
|
|
|
if email is None:
|
|
raise HTTPException(
|
|
status_code = status.HTTP_401_UNAUTHORIZED,
|
|
detail = "Invalid authentication credentials",
|
|
headers = {"WWW-Authenticate": "Bearer"},
|
|
)
|
|
|
|
return email
|
|
|
|
except ValueError:
|
|
raise HTTPException(
|
|
status_code = status.HTTP_401_UNAUTHORIZED,
|
|
detail = "Invalid authentication credentials",
|
|
headers = {"WWW-Authenticate": "Bearer"},
|
|
) from None
|