Spaces:
Running
Running
File size: 1,548 Bytes
7ffe51d |
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
from sqlmodel import Session
from typing import Generator
from fastapi import Depends, HTTPException, status
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from src.core.database import get_session
from src.core.security import verify_jwt_token
from src.core.config import settings
security = HTTPBearer()
def get_db() -> Generator[Session, None, None]:
"""Get database session dependency."""
yield from get_session()
def get_current_user(
credentials: HTTPAuthorizationCredentials = Depends(security)
) -> int:
"""
Get current user ID from JWT token.
Extracts and verifies JWT from Authorization header.
Args:
credentials: HTTP Bearer credentials from Authorization header
Returns:
User ID extracted from validated token
Raises:
HTTPException: 401 if token is missing, invalid, or expired
"""
if not credentials:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Not authenticated",
headers={"WWW-Authenticate": "Bearer"}
)
token = credentials.credentials
# Verify token and extract payload
payload = verify_jwt_token(token, settings.BETTER_AUTH_SECRET)
# Extract user ID from 'sub' claim
user_id = payload.get("sub")
if not user_id:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid token payload",
headers={"WWW-Authenticate": "Bearer"}
)
return int(user_id)
|