myst/web/app/main.py
ZTatman 5fec547983 feat(web): Forgejo OAuth, Myst sessions, and auth schema
Implement Myst-owned sessions after Forgejo OAuth per auth design: User and
LinkedIdentity with encrypted OAuth tokens, Session in its own module,
Grant token_hash plus user_id and expires_at. Add auth/identity and
auth/session helpers, Forgejo login/callback/logout routes, and a minimal
/dashboard that requires get_current_session.

Move get_db to app/deps.py so routers and auth avoid importing app.main.
Add httpx and cryptography; extend Settings with OAuth client fields,
optional MYST_PUBLIC_BASE_URL for redirect_uri, and OAUTH_TOKEN_ENCRYPTION_KEY
(Fernet) required when persisting OAuth tokens.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-12 15:12:17 -04:00

51 lines
1.4 KiB
Python

from contextlib import asynccontextmanager
from fastapi import Depends, FastAPI
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from app.auth.session import get_current_session
from app.config import get_settings
from app.database import Base
from app.models.session import Session as UserBrowserSession
from app.routers import auth, links
import app.models # noqa: F401 - register ORM tables on metadata
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Manage database engine lifecycle."""
settings = get_settings()
engine = create_async_engine(
url=settings.database_url,
pool_pre_ping=True,
echo=settings.app_name != "Myst",
)
app.state.db_session = async_sessionmaker(
engine,
class_=AsyncSession,
expire_on_commit=False, # Prevents MissingGreenlet in async
)
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all) # Create all tables on startup
yield
await engine.dispose() # Close all connections on shutdown
app = FastAPI(lifespan=lifespan)
app.include_router(auth.router)
app.include_router(links.router)
@app.get("/")
async def root():
return {"message": "Welcome to Myst"}
@app.get("/dashboard")
async def dashboard(sess: UserBrowserSession = Depends(get_current_session)):
return {
"display_name": sess.user.display_name,
"email": sess.user.email,
}