from __future__ import annotations import uuid import bcrypt from datetime import datetime, timedelta, timezone from typing import Annotated from fastapi import Depends, HTTPException, status from fastapi.security import OAuth2PasswordBearer from jose import JWTError, jwt from sqlalchemy import select from sqlalchemy.ext.asyncio import AsyncSession from app.config import get_settings from app.database import get_db settings = get_settings() oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/auth/login", auto_error=False) def hash_password(password: str) -> str: return bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt()).decode("utf-8") def verify_password(plain: str, hashed: str) -> bool: return bcrypt.checkpw(plain.encode("utf-8"), hashed.encode("utf-8")) def create_access_token(user_id: str, expires_delta: timedelta | None = None) -> str: expire = datetime.now(timezone.utc) + (expires_delta or timedelta(minutes=settings.access_token_expire_minutes)) payload = {"sub": user_id, "exp": expire} return jwt.encode(payload, settings.secret_key, algorithm=settings.algorithm) async def get_current_user( token: Annotated[str | None, Depends(oauth2_scheme)], db: Annotated[AsyncSession, Depends(get_db)], ): credentials_exception = HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid authentication credentials", headers={"WWW-Authenticate": "Bearer"}, ) if not token: raise credentials_exception from app.models.user import User # Check if this is an API token (MCP connector auth) if settings.api_token and token == settings.api_token: # Map to the first admin user result = await db.execute( select(User).where(User.is_admin == True).order_by(User.created_at.asc()).limit(1) ) user = result.scalar_one_or_none() if user is None: # Fall back to first user if no admin exists yet result = await db.execute( select(User).order_by(User.created_at.asc()).limit(1) ) user = result.scalar_one_or_none() if user is None: raise credentials_exception return user # Standard JWT validation try: payload = jwt.decode(token, settings.secret_key, algorithms=[settings.algorithm]) user_id: str = payload.get("sub") if user_id is None: raise credentials_exception except JWTError: raise credentials_exception result = await db.execute(select(User).where(User.id == uuid.UUID(user_id))) user = result.scalar_one_or_none() if user is None: raise credentials_exception return user