File size: 1,111 Bytes
29340f0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import uuid
from pydantic import BaseModel, ValidationError
from fastapi import Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer
from jose import JWTError, jwt
from core.config import get_settings
settings = get_settings()
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
class TokenPayload(BaseModel):
sub: uuid.UUID
aud: str
async def get_current_user(token: str = Depends(oauth2_scheme)) -> uuid.UUID:
"""
Decodes and verifies the JWT token to get the current user.
Raises HTTPException if the token is invalid.
"""
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
try:
payload = jwt.decode(
token,
settings.SUPABASE_JWT_SECRET,
algorithms=["HS256"],
audience="authenticated"
)
token_data = TokenPayload(**payload)
except (JWTError, ValidationError):
raise credentials_exception
return token_data.sub |