firebreak/web/app/auth/session.py
ZTatman 8e56a5bb1c feat(web): setup wizard, settings page, and Myst → Firebreak rename
Replace manual .env configuration with a browser-based setup wizard.
New users deploy with `docker compose up` and configure Forgejo OAuth
credentials through /setup on first visit. Existing users can
reconfigure via the same page (auth-gated post-setup).

Key changes:

Rename (Myst → Firebreak):
- Config fields, env vars, session cookie, templates, docker-compose,
  README all renamed from Myst to Firebreak

Setup wizard (/setup):
- New AppSettings single-row DB table stores Forgejo config
- Auto-generates Fernet encryption key on first setup
- SetupRequiredMiddleware redirects all routes to /setup pre-config
- Reconfigure mode pre-fills existing values, requires auth
- Landing page links to /setup for recovery if OAuth breaks

Settings page (/settings):
- PAT registration moved out of dashboard into dedicated page
- Token card with registration date, delete, and update actions
- Dashboard shows CTA linking to settings when no PAT registered

Auth updates:
- Routes use AppSettings from DB instead of env-based Settings
- OAuth callback redirects new users to /settings for PAT setup
- Forgejo API errors no longer leak upstream response bodies
- Decrypt failures return actionable 503 instead of unhandled 500

Code quality:
- Extracted templating.py to eliminate circular import hacks
- AppSettings.get_fernet() caches Fernet cipher instance
- User.has_pat property replaces scattered None checks
- lookup_session promoted to public API
- Dead code removed, return type annotations added
- PAT input fields use type="password"
- CSRF gap documented in agents.md for future addressing

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-06-03 11:15:44 -04:00

123 lines
3.8 KiB
Python

from __future__ import annotations
import hashlib
import secrets
import uuid
from datetime import UTC, datetime, timedelta
from typing import Annotated
from fastapi import Cookie, Depends, HTTPException, Response, status
from sqlalchemy import select, update
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import selectinload
from app.deps import get_db
from app.models.session import Session
SESSION_COOKIE = "firebreak_session"
SESSION_DURATION_DAYS = 7
async def create_session(
db: AsyncSession, response: Response, user_id: uuid.UUID, secure: bool = True
) -> None:
"""Create a persisted browser session and set its cookie on the response."""
# The browser gets the raw token, but the database only stores its hash.
# This keeps a leaked sessions table from containing usable cookie values.
raw_token = secrets.token_hex(32)
token_hash = hashlib.sha256(raw_token.encode()).hexdigest()
expires_at = datetime.now(UTC) + timedelta(days=SESSION_DURATION_DAYS)
row = Session(
user_id=user_id,
token_hash=token_hash,
expires_at=expires_at,
)
db.add(row)
await db.commit()
# HttpOnly keeps client-side scripts from reading the session cookie. Lax is
# enough for normal navigation while reducing cross-site request exposure.
response.set_cookie(
key=SESSION_COOKIE,
value=raw_token, # raw token is stored in the cookie, not the hash
httponly=True,
samesite="lax",
secure=secure,
max_age=60 * 60 * 24 * SESSION_DURATION_DAYS,
path="/",
)
async def lookup_session(
db: AsyncSession,
firebreak_session: str | None,
) -> Session | None:
"""Return the active session for a raw cookie token, if one exists."""
if not firebreak_session:
return None
token_hash = hashlib.sha256(firebreak_session.encode()).hexdigest()
now = datetime.now(UTC)
stmt = (
select(Session)
.options(selectinload(Session.user))
.where(
Session.token_hash == token_hash,
Session.expires_at > now,
Session.revoked_at.is_(None),
)
)
result = await db.execute(stmt)
sess = result.scalar_one_or_none()
if sess is None:
return None
# Track activity whenever a valid cookie is used, then refresh the related
# user so route handlers can safely access sess.user.
sess.last_seen_at = now
await db.commit()
await db.refresh(sess, attribute_names=["user"])
return sess
async def get_current_session(
db: Annotated[AsyncSession, Depends(get_db)],
firebreak_session: Annotated[str | None, Cookie()] = None,
) -> Session:
"""FastAPI dependency that requires a valid signed-in session."""
sess = await lookup_session(db, firebreak_session)
if sess is None:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Not signed in",
)
return sess
async def get_optional_session(
db: Annotated[AsyncSession, Depends(get_db)],
firebreak_session: Annotated[str | None, Cookie()] = None,
) -> Session | None:
"""FastAPI dependency that returns the current session when available."""
return await lookup_session(db, firebreak_session)
async def revoke_session(db: AsyncSession, response: Response, firebreak_session: str | None) -> None:
"""Delete the browser cookie and mark the matching session as revoked."""
response.delete_cookie(
key=SESSION_COOKIE,
path="/",
samesite="lax",
httponly=True,
)
if not firebreak_session:
return
token_hash = hashlib.sha256(firebreak_session.encode()).hexdigest()
result = await db.execute(
update(Session)
.where(Session.token_hash == token_hash, Session.revoked_at.is_(None))
.values(revoked_at=datetime.now(UTC))
)
await db.commit()