from typing import Generator, Optional from fastapi import Depends, HTTPException, status from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials from sqlalchemy.orm import Session from app.db.session import get_db from app.core.security import verify_token from app.models.user import User security = HTTPBearer() def get_current_user( db: Session = Depends(get_db), credentials: HTTPAuthorizationCredentials = Depends(security) ) -> User: token = credentials.credentials user_id = verify_token(token) if user_id is None: raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid authentication credentials", headers={"WWW-Authenticate": "Bearer"}, ) user = db.query(User).filter(User.id == int(user_id)).first() if user is None: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="User not found" ) if not user.is_active: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail="Inactive user" ) return user def get_current_developer( current_user: User = Depends(get_current_user) ) -> User: if current_user.user_type != "developer": raise HTTPException( status_code=status.HTTP_403_FORBIDDEN, detail="Access denied: Developer role required" ) return current_user def get_current_buyer( current_user: User = Depends(get_current_user) ) -> User: if current_user.user_type != "buyer": raise HTTPException( status_code=status.HTTP_403_FORBIDDEN, detail="Access denied: Buyer role required" ) return current_user