Buckets:

download
raw
3.54 kB
# apps/users/auth.py
from typing import Optional
from fastapi import Header, HTTPException, status
from typing import Annotated
from django.conf import settings
from django.contrib.auth import get_user_model
from asgiref.sync import sync_to_async
import logging
import jwt
logger = logging.getLogger(__name__)
User = get_user_model()
# ============= Authentication Dependencies =============
@sync_to_async
def fetch_user_from_db(user_id: str) -> User: # type: ignore
"""Fetch user from database by ID."""
"""Synchronous function to fetch user from database."""
try:
return User.objects.get(id=user_id, is_active=True)
except User.DoesNotExist:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="User not found or inactive",
headers={"WWW-Authenticate": "Bearer"},
)
async def get_current_user(
authorization: Optional[str] = Header(None, alias="Authorization")
) -> User: # type: ignore
"""
Get current authenticated user from JWT token.
Args:
authorization: Authorization header with Bearer token
Returns:
Django User instance
Raises:
HTTPException: If authentication fails
"""
if not authorization:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Missing authorization header",
headers={"WWW-Authenticate": "Bearer"},
)
try:
# Extract token
scheme, token = authorization.split()
if scheme.lower() != 'bearer':
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid authentication scheme. Use 'Bearer <token>'",
headers={"WWW-Authenticate": "Bearer"},
)
# Decode JWT token
try:
payload = jwt.decode(
token,
settings.SECRET_KEY,
algorithms=['HS256']
)
except jwt.ExpiredSignatureError:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Token has expired",
headers={"WWW-Authenticate": "Bearer"},
)
except jwt.InvalidTokenError:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid token",
headers={"WWW-Authenticate": "Bearer"},
)
# Get user from token payload
user_id = payload.get('user_id')
if not user_id:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid token payload",
headers={"WWW-Authenticate": "Bearer"},
)
# Fetch user from database
user = await fetch_user_from_db(user_id)
return user
except ValueError:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid authorization header format. Use 'Bearer <token>'",
headers={"WWW-Authenticate": "Bearer"},
)
except HTTPException:
raise
except Exception as e:
logger.error(f"Authentication error: {e}")
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Authentication failed",
headers={"WWW-Authenticate": "Bearer"},
)

Xet Storage Details

Size:
3.54 kB
·
Xet hash:
a21dd556cca49b8ec2974b5b7703781837feba3703efbe0923db24647177383a

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.