a89c7894cf
Backend: - FastAPI avec PostgreSQL et Redis - Authentification JWT complète - API REST pour musique, playlists, recherche - Streaming audio via yt-dlp - SQLAlchemy 2.0 async Frontend: - Flutter avec thème néon cyberpunk - State management Riverpod - Layout adaptatif desktop/mobile - Lecteur audio avec mini-player Infrastructure: - Docker Compose (PostgreSQL + Redis) - Scripts d'installation automatisés - Scripts de build pour exécutables Fichiers ajoutés: - BUILD_CLIENT_*.bat/sh: Scripts de compilation - BUILD_CLIENT_README.md: Documentation compilation - CHECK_FLUTTER.sh: Vérificateur d'environnement - requirements.txt mis à jour pour Python 3.13 - Modèles SQLAlchemy corrigés (metadata -> extra_metadata) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
101 lines
2.5 KiB
Python
101 lines
2.5 KiB
Python
"""API dependencies."""
|
|
from typing import Annotated, Optional
|
|
|
|
from fastapi import Depends, Header, HTTPException, status
|
|
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
|
from jose import JWTError
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.config import settings
|
|
from app.core.database import get_db
|
|
from app.core.security import decode_token
|
|
from app.models.user import User
|
|
from app.services.auth_service import AuthService
|
|
|
|
|
|
# Database session dependency
|
|
DBSession = Annotated[AsyncSession, Depends(get_db)]
|
|
|
|
|
|
# Authentication
|
|
security = HTTPBearer()
|
|
|
|
|
|
async def get_current_user(
|
|
credentials: Annotated[HTTPAuthorizationCredentials, Depends(security)],
|
|
db: DBSession,
|
|
) -> User:
|
|
"""
|
|
Get current authenticated user from JWT token.
|
|
|
|
Args:
|
|
credentials: HTTP Authorization credentials
|
|
db: Database session
|
|
|
|
Returns:
|
|
Current user
|
|
|
|
Raises:
|
|
HTTPException: If token is invalid or user not found
|
|
"""
|
|
credentials_exception = HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Could not validate credentials",
|
|
headers={"WWW-Authenticate": "Bearer"},
|
|
)
|
|
|
|
try:
|
|
payload = decode_token(credentials.credentials)
|
|
user_id: str = payload.get("sub")
|
|
token_type: str = payload.get("type")
|
|
|
|
if user_id is None or token_type != "access":
|
|
raise credentials_exception
|
|
|
|
except JWTError:
|
|
raise credentials_exception
|
|
|
|
auth_service = AuthService(db)
|
|
user = await auth_service.get_user_by_id(user_id)
|
|
|
|
if user is None:
|
|
raise credentials_exception
|
|
|
|
return user
|
|
|
|
|
|
async def get_current_user_optional(
|
|
credentials: Annotated[Optional[HTTPAuthorizationCredentials], Depends(security)],
|
|
db: DBSession,
|
|
) -> Optional[User]:
|
|
"""
|
|
Get current user if authenticated, otherwise None.
|
|
|
|
Args:
|
|
credentials: Optional HTTP Authorization credentials
|
|
db: Database session
|
|
|
|
Returns:
|
|
Current user or None
|
|
"""
|
|
if credentials is None:
|
|
return None
|
|
|
|
try:
|
|
return await get_current_user(credentials, db) # type: ignore
|
|
except HTTPException:
|
|
return None
|
|
|
|
|
|
# Current user dependencies
|
|
CurrentUser = Annotated[User, Depends(get_current_user)]
|
|
CurrentUserOptional = Annotated[Optional[User], Depends(get_current_user_optional)]
|
|
|
|
|
|
def get_auth_service(db: DBSession) -> AuthService:
|
|
"""Get auth service instance."""
|
|
return AuthService(db)
|
|
|
|
|
|
AuthServiceDep = Annotated[AuthService, Depends(get_auth_service)]
|