Spaces:
Running
Running
axeronis commited on
Commit ·
c5aa5c4
1
Parent(s): eb6444b
Debug: Add logging to auth dependencies
Browse files13 unmodified lines141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485865 unmodified lines929394959697989910010110210310410510610710810911011111213 unmodified linessecurity = HTTPBearer()async def get_current_user(credentials: HTTPAuthorizationCredentials = Depends(security),session: AsyncSession = Depends(get_async_session)) -> User:"""Dependency to get the current authenticated user from JWT token.Args:credentials: HTTP Bearer token credentialssession: Async database sessionReturns:Current authenticated User objectRaises:HTTPException: If token is invalid or user not found"""token = credentials.credentialspayload = decode_access_token(token)# Extract user ID from tokenuser_id: str = payload.get("sub")if user_id is None:raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,detail="Could not validate credentials",headers={"WWW-Authenticate": "Bearer"},)# Fetch user from databaseresult = await session.execute(select(User).where(User.id == UUID(user_id)))user = result.scalar_one_or_none()if user is None:raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,detail="User not found",headers={"WWW-Authenticate": "Bearer"},)# Check if user is activeif not user.is_active:raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,detail="Inactive user")return userasync def get_current_active_user(current_user: User = Depends(get_current_user)) -> User:"""Dependency to get the current active user.Args:current_user: Current user from get_current_user dependencyReturns:Current active User objectRaises:HTTPException: If user is inactive"""if not current_user.is_active:raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,5 unmodified linesasync def get_current_superuser(current_user: User = Depends(get_current_user)) -> User:"""Dependency to get the current superuser.Args:current_user: Current user from get_current_user dependencyReturns:Current superuser User objectRaises:HTTPException: If user is not a superuser"""if not current_user.is_superuser:raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,detail="Not enough permissions")return current_user13 unmodified lines14151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374755 unmodified lines818283848586878889909113 unmodified linessecurity = HTTPBearer()from app.core.logging import get_loggerlogger = get_logger(__name__)async def get_current_user(credentials: HTTPAuthorizationCredentials = Depends(security),session: AsyncSession = Depends(get_async_session)) -> User:"""Dependency to get the current authenticated user from JWT token."""token = credentials.credentialstry:payload = decode_access_token(token)except Exception as e:logger.error(f"Token decode failed: {e}")raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,detail="Invalid token",headers={"WWW-Authenticate": "Bearer"},)user_id: str = payload.get("sub")if user_id is None:logger.error("Token missing 'sub' claim")raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,detail="Could not validate credentials",headers={"WWW-Authenticate": "Bearer"},)result = await session.execute(select(User).where(User.id == UUID(user_id)))user = result.scalar_one_or_none()if user is None:logger.error(f"User {user_id} not found in DB")raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,detail="User not found",headers={"WWW-Authenticate": "Bearer"},)if not user.is_active:logger.warning(f"User {user.email} is inactive")raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,detail="Inactive user")# logger.info(f"Auth success: {user.email} ({user.role})") # Too noisy for prod, but good for debugreturn userasync def get_current_active_user(current_user: User = Depends(get_current_user)) -> User:if not current_user.is_active:raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,5 unmodified linesasync def get_current_superuser(current_user: User = Depends(get_current_user)) -> User:if not current_user.is_superuser:logger.warning(f"Access DENIED: User {current_user.email} is NOT a superuser")raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,detail="Not enough permissions")logger.info(f"Superuser access granted: {current_user.email}")return current_user
backend/app/api/deps.py+21-42