Architecture: - Moved backend/app/ to diligence/ Python package - Dialect-agnostic models (Uuid + JSON, no PostgreSQL imports) - Auto-detect database: empty DATABASE_URL → SQLite at ~/.diligence/data.db - Cross-dialect migrations (inspector-based, no raw PostgreSQL DDL) - FastAPI serves pre-built React frontend for pip path - CLI entry point: diligence [--port] [--no-browser] [--data-dir] - pyproject.toml with optional deps: [postgres], [mcp], [dev] - Docker path unchanged: docker compose up -d pip install path: pip install . && diligence Docker path: ./setup.sh && docker compose up -d
79 lines
2.7 KiB
Python
79 lines
2.7 KiB
Python
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 diligence.config import get_settings
|
|
from diligence.database import get_db
|
|
|
|
settings = get_settings()
|
|
ALGORITHM = "HS256" # Hardcoded — not configurable for security
|
|
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=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 diligence.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=[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
|