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>
72 lines
2.2 KiB
Python
72 lines
2.2 KiB
Python
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy.orm import selectinload
|
|
|
|
from app.auth.crypto import encrypt_optional
|
|
from app.models.app_settings import AppSettings
|
|
from app.models.user import LinkedIdentity, User
|
|
|
|
|
|
async def find_or_create_user(
|
|
db: AsyncSession,
|
|
app_settings: AppSettings,
|
|
*,
|
|
provider: str,
|
|
provider_user_id: str,
|
|
provider_username: str,
|
|
email: str | None,
|
|
access_token_raw: str | None,
|
|
access_token_expires_at: datetime | None,
|
|
refresh_token_raw: str | None,
|
|
) -> User:
|
|
"""Find or create a user based on the given provider and identity details."""
|
|
|
|
fernet = app_settings.get_fernet()
|
|
|
|
result = await db.execute(
|
|
select(LinkedIdentity)
|
|
.options(selectinload(LinkedIdentity.user))
|
|
.where(
|
|
LinkedIdentity.provider == provider,
|
|
LinkedIdentity.provider_user_id == provider_user_id,
|
|
)
|
|
)
|
|
identity = result.scalar_one_or_none()
|
|
|
|
if identity:
|
|
identity.provider_username = provider_username
|
|
if access_token_raw is not None:
|
|
identity.access_token_encrypted = encrypt_optional(fernet, access_token_raw)
|
|
identity.access_token_expires_at = access_token_expires_at
|
|
if refresh_token_raw is not None:
|
|
identity.refresh_token_encrypted = encrypt_optional(fernet, refresh_token_raw)
|
|
user = identity.user
|
|
if email:
|
|
user.email = email
|
|
if not user.display_name:
|
|
user.display_name = provider_username
|
|
await db.commit()
|
|
await db.refresh(user)
|
|
return user
|
|
|
|
user = User(email=email, display_name=provider_username)
|
|
db.add(user)
|
|
await db.flush()
|
|
|
|
linked = LinkedIdentity(
|
|
user_id=user.id,
|
|
provider=provider,
|
|
provider_user_id=provider_user_id,
|
|
provider_username=provider_username,
|
|
access_token_encrypted=encrypt_optional(fernet, access_token_raw),
|
|
access_token_expires_at=access_token_expires_at,
|
|
refresh_token_encrypted=encrypt_optional(fernet, refresh_token_raw),
|
|
)
|
|
db.add(linked)
|
|
await db.commit()
|
|
await db.refresh(user)
|
|
return user
|