531dc968a8
- Add Logto OIDC integration (app/core/logto.py): CookieStorage adapter, create/decode session token helpers, sync_logto_user upsert - New auth endpoints (/api/v1/auth): sign-in, callback, sign-out, me - AuthRedirectMiddleware: protects HTML pages, redirects to /setup when Logto is unconfigured, to /login otherwise - Update require_admin_auth: accepts dmarq_session cookie JWT first, then API key, then Bearer JWT (fully backward compatible) - Update User model: add logto_id, username, picture, created_at, updated_at; make hashed_password nullable for Logto-only users; is_superuser default=True - New Alembic migration d4e5f6a7b8c9 for the above schema changes - Add LOGTO_ENDPOINT / LOGTO_APP_ID / LOGTO_APP_SECRET / LOGTO_REDIRECT_URI settings with logto_configured property - Create login.html (Sign in with Logto button) and setup.html (step-by-step configuration guide) - Update base.html: user menu with avatar/name and sign-out via Alpine.js fetch to /api/v1/auth/me - Update settings.html: remove localStorage adminApiKey; session cookie is sent automatically by browser; add 401 → /login redirect - Update requirements.txt: replace fastapi-users additions with logto + aiohttp - Add test_auth.py: 18 new tests covering session tokens, CookieStorage, sync_logto_user, /me, /sign-in (503), /sign-out cookie clearing - Fix test_security_extra.py: pass Request mock to require_admin_auth; add new test_valid_session_cookie_returns_auth_context Agent-Logs-Url: https://github.com/christianlouis/dmarq/sessions/b448f585-7646-40f8-ae2d-9986c361e3fd Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com>
78 lines
2.4 KiB
Python
78 lines
2.4 KiB
Python
import os
|
||
from typing import Generator
|
||
from urllib.parse import urlparse, urlunparse
|
||
|
||
from sqlalchemy import create_engine
|
||
from sqlalchemy.engine import make_url
|
||
from sqlalchemy.ext.declarative import declarative_base
|
||
from sqlalchemy.orm import sessionmaker
|
||
|
||
from app.core.config import get_settings
|
||
|
||
_ASYNC_TO_SYNC_SCHEMES = {
|
||
"postgresql+asyncpg": "postgresql+psycopg2",
|
||
}
|
||
|
||
|
||
def _make_sync_db_url(url: str) -> str:
|
||
"""Return the synchronous-driver equivalent of *url*.
|
||
|
||
Kubernetes and docker-compose deployments sometimes configure DATABASE_URL
|
||
with an async driver scheme (e.g. ``postgresql+asyncpg://``). Alembic and
|
||
the synchronous SQLAlchemy engine used here require a sync driver, so we
|
||
map known async schemes to their psycopg2 equivalents.
|
||
|
||
Only the scheme component of the URL is rewritten; all other parts
|
||
(credentials, host, path, query) are left untouched.
|
||
"""
|
||
parsed = urlparse(url)
|
||
sync_scheme = _ASYNC_TO_SYNC_SCHEMES.get(parsed.scheme)
|
||
if sync_scheme is None:
|
||
return url
|
||
return urlunparse(parsed._replace(scheme=sync_scheme))
|
||
|
||
|
||
def _ensure_sqlite_dir(url: str) -> None:
|
||
"""Create the parent directory for a SQLite database file if needed.
|
||
|
||
For SQLite URLs (``sqlite:///relative/path`` or ``sqlite:////absolute/path``),
|
||
the parent directory must exist before SQLAlchemy tries to open (or create)
|
||
the file. This is a no-op for in-memory databases (``sqlite://``) and for
|
||
non-SQLite URLs.
|
||
"""
|
||
sa_url = make_url(url)
|
||
if not sa_url.drivername.startswith("sqlite"):
|
||
return
|
||
db_path = sa_url.database
|
||
if not db_path or db_path == ":memory:":
|
||
return # in-memory – nothing to create
|
||
parent = os.path.dirname(db_path)
|
||
if parent:
|
||
os.makedirs(parent, exist_ok=True)
|
||
|
||
|
||
settings = get_settings()
|
||
|
||
_sync_url = _make_sync_db_url(settings.DATABASE_URL)
|
||
|
||
# Ensure the parent directory exists before SQLAlchemy tries to open the file
|
||
_ensure_sqlite_dir(_sync_url)
|
||
|
||
# Configure SQLAlchemy (normalise async driver schemes to their sync equivalents)
|
||
engine = create_engine(_sync_url, pool_pre_ping=True)
|
||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||
|
||
# Create base class for SQLAlchemy models
|
||
Base = declarative_base()
|
||
|
||
|
||
def get_db() -> Generator:
|
||
"""
|
||
Dependency for getting DB sessions
|
||
"""
|
||
db = SessionLocal()
|
||
try:
|
||
yield db
|
||
finally:
|
||
db.close()
|