49 lines
1.2 KiB
Python
49 lines
1.2 KiB
Python
"""Dependency injection for FastAPI."""
|
|
|
|
from fastapi import Depends, HTTPException, status
|
|
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
|
|
|
from app.config import settings
|
|
from app.interfaces.db_api_client import DBApiClient
|
|
from app.utils.security import decode_access_token
|
|
|
|
security = HTTPBearer()
|
|
|
|
|
|
def get_db_client() -> DBApiClient:
|
|
"""
|
|
Get DB API client instance.
|
|
|
|
Returns:
|
|
DBApiClient configured with api_prefix from settings
|
|
"""
|
|
return DBApiClient(api_prefix=settings.DB_API_URL)
|
|
|
|
|
|
async def get_current_user(
|
|
credentials: HTTPAuthorizationCredentials = Depends(security)
|
|
) -> dict:
|
|
"""
|
|
Get current user from JWT token.
|
|
|
|
Args:
|
|
credentials: HTTP Bearer credentials from request
|
|
|
|
Returns:
|
|
Token payload with user_id and login
|
|
|
|
Raises:
|
|
HTTPException: If token is invalid or expired
|
|
"""
|
|
token = credentials.credentials
|
|
payload = decode_access_token(token)
|
|
|
|
if payload is None:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Invalid or expired token",
|
|
headers={"WWW-Authenticate": "Bearer"},
|
|
)
|
|
|
|
return payload
|