firebreak/web/app/models/user.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

67 lines
2.4 KiB
Python

from __future__ import annotations
import uuid
from datetime import datetime
from typing import TYPE_CHECKING
from sqlalchemy import ForeignKey, String, Text, UniqueConstraint, Uuid, func
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.database import Base
if TYPE_CHECKING:
from .grant import Grant
from .session import Session
class User(Base):
__tablename__ = "users"
id: Mapped[uuid.UUID] = mapped_column(
Uuid, primary_key=True, server_default=func.gen_random_uuid()
)
display_name: Mapped[str | None] = mapped_column(String, nullable=True)
email: Mapped[str | None] = mapped_column(String, nullable=True, index=True)
pat_encrypted: Mapped[str | None] = mapped_column(Text, nullable=True)
pat_registered_at: Mapped[datetime | None] = mapped_column(nullable=True)
created_at: Mapped[datetime] = mapped_column(server_default=func.now())
@property
def has_pat(self) -> bool:
return self.pat_encrypted is not None
identities: Mapped[list[LinkedIdentity]] = relationship(
back_populates="user",
cascade="all, delete-orphan",
)
sessions: Mapped[list[Session]] = relationship(
"Session",
back_populates="user",
cascade="all, delete-orphan",
)
grants: Mapped[list[Grant]] = relationship(
"Grant",
back_populates="user",
)
class LinkedIdentity(Base):
__tablename__ = "linked_identities"
id: Mapped[uuid.UUID] = mapped_column(
Uuid, primary_key=True, server_default=func.gen_random_uuid()
)
user_id: Mapped[uuid.UUID] = mapped_column(
Uuid, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True
)
provider: Mapped[str] = mapped_column(String, nullable=False)
provider_user_id: Mapped[str] = mapped_column(String, nullable=False)
provider_username: Mapped[str] = mapped_column(String, nullable=False)
access_token_encrypted: Mapped[str | None] = mapped_column(Text, nullable=True)
access_token_expires_at: Mapped[datetime | None] = mapped_column(nullable=True)
refresh_token_encrypted: Mapped[str | None] = mapped_column(Text, nullable=True)
created_at: Mapped[datetime] = mapped_column(server_default=func.now())
user: Mapped[User] = relationship(back_populates="identities")
__table_args__ = (UniqueConstraint("provider", "provider_user_id"),)