diff --git a/README.md b/README.md index a138128..c595d1b 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ uv run python -c 'from cryptography.fernet import Fernet; print(Fernet.generate_ Add the output to your `.env` file (or your deployment platform's environment variables): -``` +```bash TOKEN_ENCRYPTION_KEY=your-generated-key-here ``` diff --git a/web/app/auth/session.py b/web/app/auth/session.py index fac193a..0f3753c 100644 --- a/web/app/auth/session.py +++ b/web/app/auth/session.py @@ -103,7 +103,9 @@ async def get_optional_session( return await lookup_session(db, firebreak_session) -async def revoke_session(db: AsyncSession, response: Response, firebreak_session: str | None) -> None: +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( diff --git a/web/app/main.py b/web/app/main.py index aa92377..ad920b8 100644 --- a/web/app/main.py +++ b/web/app/main.py @@ -4,9 +4,11 @@ from contextlib import asynccontextmanager from pathlib import Path from typing import Annotated -from fastapi import Depends, FastAPI, HTTPException, Request +from fastapi import Depends, FastAPI, Request from fastapi.responses import HTMLResponse, JSONResponse from fastapi.staticfiles import StaticFiles +from starlette.exceptions import HTTPException as StarletteHTTPException +from starlette.responses import Response from sqlalchemy import func as sql_func, select from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine @@ -86,8 +88,10 @@ ERROR_TITLES = { } -@app.exception_handler(HTTPException) -async def http_exception_handler(request: Request, exc: HTTPException) -> HTMLResponse | JSONResponse: +@app.exception_handler(StarletteHTTPException) +async def http_exception_handler( + request: Request, exc: StarletteHTTPException +) -> Response: """Render a styled error page for browser requests, JSON for API clients.""" accept = request.headers.get("accept", "") if "text/html" in accept: @@ -107,7 +111,7 @@ async def http_exception_handler(request: Request, exc: HTTPException) -> HTMLRe @app.get("/", response_class=HTMLResponse) async def root( request: Request, sess: Annotated[UserBrowserSession | None, Depends(get_optional_session)] -) -> HTMLResponse: +) -> Response: return templates.TemplateResponse( request, "index.html", @@ -118,7 +122,7 @@ async def root( @app.get("/dashboard", response_class=HTMLResponse) async def dashboard( request: Request, sess: Annotated[UserBrowserSession, Depends(get_current_session)] -) -> HTMLResponse: +) -> Response: return templates.TemplateResponse( request, "dashboard.html", diff --git a/web/app/middleware.py b/web/app/middleware.py index e441953..29ea3cf 100644 --- a/web/app/middleware.py +++ b/web/app/middleware.py @@ -8,11 +8,7 @@ class SetupRequiredMiddleware(BaseHTTPMiddleware): async def dispatch(self, request: Request, call_next) -> Response: path = request.url.path - if ( - path.startswith("/setup") - or path.startswith("/static") - or path == "/favicon.ico" - ): + if path.startswith("/setup") or path.startswith("/static") or path == "/favicon.ico": return await call_next(request) if request.app.state.app_settings is None: diff --git a/web/app/routers/settings.py b/web/app/routers/settings.py index 01fb9d4..d0ac943 100644 --- a/web/app/routers/settings.py +++ b/web/app/routers/settings.py @@ -5,6 +5,7 @@ from typing import Annotated from fastapi import APIRouter, Depends, Form, HTTPException, Request, status from fastapi.responses import HTMLResponse, RedirectResponse +from starlette.responses import Response from sqlalchemy.ext.asyncio import AsyncSession from app.auth.crypto import encrypt_optional @@ -21,7 +22,7 @@ router = APIRouter(prefix="/settings", tags=["settings"]) async def settings_page( request: Request, sess: Annotated[UserBrowserSession, Depends(get_current_session)], -) -> HTMLResponse: +) -> Response: return templates.TemplateResponse( request, "settings.html", diff --git a/web/app/routers/setup.py b/web/app/routers/setup.py index 3101092..95816ca 100644 --- a/web/app/routers/setup.py +++ b/web/app/routers/setup.py @@ -3,7 +3,8 @@ from __future__ import annotations from typing import Annotated from fastapi import APIRouter, Cookie, Depends, Form, HTTPException, Request, status -from fastapi.responses import HTMLResponse, RedirectResponse +from fastapi.responses import RedirectResponse +from starlette.responses import Response from sqlalchemy import func as sql_func, select from sqlalchemy.ext.asyncio import AsyncSession @@ -32,7 +33,7 @@ async def _require_auth_if_configured( return # If no users exist, allow access — the setup token still protects POST - user_count = await db.scalar(select(sql_func.count()).select_from(User)) + user_count = await db.scalar(select(sql_func.count()).select_from(User)) or 0 if user_count == 0: return @@ -44,16 +45,17 @@ async def _require_auth_if_configured( ) -@router.get("", dependencies=[Depends(_require_auth_if_configured)], response_class=HTMLResponse) -async def setup_page(request: Request, db: Annotated[AsyncSession, Depends(get_db)]) -> HTMLResponse: +@router.get("", dependencies=[Depends(_require_auth_if_configured)]) +async def setup_page( + request: Request, db: Annotated[AsyncSession, Depends(get_db)] +) -> Response: """Render the setup wizard. Pre-fills fields when reconfiguring.""" existing = request.app.state.app_settings - user_count = await db.scalar(select(sql_func.count()).select_from(User)) + user_count = await db.scalar(select(sql_func.count()).select_from(User)) or 0 has_users = user_count > 0 - public_base = ( - (existing.firebreak_public_base_url if existing else "") - or str(request.base_url).rstrip("/") - ) + public_base = (existing.firebreak_public_base_url if existing else "") or str( + request.base_url + ).rstrip("/") ctx = { "reconfiguring": existing is not None and has_users, "forgejo_base_url": existing.forgejo_base_url if existing else "", @@ -75,7 +77,7 @@ async def setup_submit( setup_token: Annotated[str, Form()] = "", ) -> RedirectResponse: """Process the setup wizard — creates or updates Forgejo settings.""" - user_count = await db.scalar(select(sql_func.count()).select_from(User)) + user_count = await db.scalar(select(sql_func.count()).select_from(User)) or 0 has_users = user_count > 0 is_reconfiguring = request.app.state.app_settings is not None and has_users diff --git a/web/app/templates/dashboard.html b/web/app/templates/dashboard.html index 9066a45..92ec2ed 100644 --- a/web/app/templates/dashboard.html +++ b/web/app/templates/dashboard.html @@ -1,65 +1,70 @@ {% extends "base.html" %} {% block title %}Dashboard — Firebreak{% endblock %} {% block content %} -
-
-
-

Welcome,

-

{{ user.display_name or user.email }}.

-
- Create share link -
-
-
-

Active links

-

0

-

No repository snapshots published yet.

-
-
-

Default expiry

-

7 days

-

Applied to newly created links.

-
-
-

Audit events

-

0

-

Viewer activity will appear here.

-
-
- {% if not pat_registered %} -
-

Access token required

-

- Register a Forgejo access token to create share links. -

- Go to Settings -
- {% endif %} -
-
-

Share links

-

Repository snapshots published from this account.

-
-
-
- - - - - - - - - - - - - - - -
RepositoryCommitStatusViewsExpires
You have not created any share links yet.
+
+
+
+

Welcome,

+

{{ user.display_name or user.email }}.

+
+ {% if pat_registered %} + Create share link + {% else %} + Create share link + {% endif %} +
+
+
+

Active links

+

0

+

No repository snapshots published yet.

+
+
+

Default expiry

+

7 days

+

Applied to newly created links.

+
+
+

Audit events

+

0

+

Viewer activity will appear here.

+ {% if not pat_registered %} +
+

Access token required

+

+ Register a Forgejo access token to create share links. +

+ Go to Settings +
+ {% endif %} +
+
+

Share links

+

Repository snapshots published from this account.

+
+
+
+ + + + + + + + + + + + + + + +
RepositoryCommitStatusViewsExpires
You have not created any share links yet.
+
+
+
-
-{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/web/app/templates/error.html b/web/app/templates/error.html index f7f8e91..1d28f90 100644 --- a/web/app/templates/error.html +++ b/web/app/templates/error.html @@ -111,7 +111,7 @@ "He doesn't bite. He just harvests.", "Move along. Nothing to scrape here.", "Unauthorized. The Zuck abides.", - "ACCESS DENIED. Deploying Cuckerberg.", + "ACCESS DENIED. Deploying Cuckerberg." ] %}

{{ quips | random }}

{% endif %} diff --git a/web/app/templates/setup.html b/web/app/templates/setup.html index 47e4786..e1fbdd6 100644 --- a/web/app/templates/setup.html +++ b/web/app/templates/setup.html @@ -64,9 +64,7 @@ class="block w-full rounded-md border border-[#f7f2e8]/14 bg-[#1f2226] px-3 py-2.5 font-mono text-sm text-[#f7f2e8] placeholder:text-[#f7f2e8]/30 focus:border-[#f7f2e8]/30 focus:outline-none focus:ring-2 focus:ring-[#f7f2e8]/10" placeholder="Paste the token from your server logs" required /> -

- Printed to the server console on first boot. -

+

Printed to the server console on first boot.

{% endif %}