139 lines
3.1 KiB
Python
139 lines
3.1 KiB
Python
"""
|
|
Tool_OCR - FastAPI Dependencies
|
|
Authentication and database session dependencies
|
|
"""
|
|
|
|
from typing import Generator, Optional
|
|
import logging
|
|
|
|
from fastapi import Depends, HTTPException, status
|
|
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.core.database import SessionLocal
|
|
from app.core.security import decode_access_token
|
|
from app.models.user import User
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
# HTTP Bearer token security scheme
|
|
security = HTTPBearer()
|
|
|
|
|
|
def get_db() -> Generator:
|
|
"""
|
|
Database session dependency
|
|
|
|
Yields:
|
|
Session: SQLAlchemy database session
|
|
"""
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
def get_current_user(
|
|
credentials: HTTPAuthorizationCredentials = Depends(security),
|
|
db: Session = Depends(get_db)
|
|
) -> User:
|
|
"""
|
|
Get current authenticated user from JWT token
|
|
|
|
Args:
|
|
credentials: HTTP Bearer credentials
|
|
db: Database session
|
|
|
|
Returns:
|
|
User: Current user object
|
|
|
|
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"},
|
|
)
|
|
|
|
# Extract token
|
|
token = credentials.credentials
|
|
|
|
# Decode token
|
|
payload = decode_access_token(token)
|
|
if payload is None:
|
|
raise credentials_exception
|
|
|
|
# Extract user ID from token (convert from string to int)
|
|
user_id_str: Optional[str] = payload.get("sub")
|
|
if user_id_str is None:
|
|
raise credentials_exception
|
|
|
|
try:
|
|
user_id: int = int(user_id_str)
|
|
except (ValueError, TypeError):
|
|
raise credentials_exception
|
|
|
|
# Query user from database
|
|
user = db.query(User).filter(User.id == user_id).first()
|
|
if user is None:
|
|
raise credentials_exception
|
|
|
|
# Check if user is active
|
|
if not user.is_active:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Inactive user"
|
|
)
|
|
|
|
return user
|
|
|
|
|
|
def get_current_active_user(
|
|
current_user: User = Depends(get_current_user)
|
|
) -> User:
|
|
"""
|
|
Get current active user
|
|
|
|
Args:
|
|
current_user: Current user from get_current_user
|
|
|
|
Returns:
|
|
User: Current active user
|
|
|
|
Raises:
|
|
HTTPException: If user is inactive
|
|
"""
|
|
if not current_user.is_active:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Inactive user"
|
|
)
|
|
return current_user
|
|
|
|
|
|
def get_current_admin_user(
|
|
current_user: User = Depends(get_current_user)
|
|
) -> User:
|
|
"""
|
|
Get current admin user
|
|
|
|
Args:
|
|
current_user: Current user from get_current_user
|
|
|
|
Returns:
|
|
User: Current admin user
|
|
|
|
Raises:
|
|
HTTPException: If user is not admin
|
|
"""
|
|
if not current_user.is_admin:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="Not enough privileges"
|
|
)
|
|
return current_user
|