Files
politics-tracking/web/src/lib/viewer.ts
T
simonandClaude Opus 5.5 9f0e2fc115 reports: user articles that embed claims under a database seal and link each other
- /berichte lists published reports; /berichte/<slug> renders a report: Markdown subset built as React
  elements (no raw HTML), [[angabe:ID]] embeds the claim card under a seal (green 'Amtliche Daten' when a
  source is official), [text](angabe:ID|bericht:slug|person:slug|org:slug) links inline, [[bericht:slug]]
  embeds another report; a box says how many claims the text rests on; backlinks between reports.
- /berichte/verfassen (login at the proxy): own reports, editor with a picker that finds claims, reports and
  persons and inserts their codes; draft / published / withdrawn; every save kept in report_revision.
- Claim pages show the embed code, 'use in a new report' and the reports citing the claim.
- API: GET /api/v1/reports, /reports/{slug} (references resolved like permalinks, merged claims follow the
  survivor); POST/PUT for the web app with POLITRACK_AUTHOR_TOKEN on behalf of the proxy user. The web app
  trusts Remote-User only with X-Politrack-Proxy = POLITRACK_PROXY_SECRET.
- Migration 0011 (report, report_revision, report_reference, app_user.display_name); docs: data model,
  deployment (sign-in setup), design system, roadmap.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 10:19:50 +01:00

43 lines
1.9 KiB
TypeScript

// Who is signed in: the reverse proxy (SWAG + Authelia) passes the login as Remote-User/-Name/-Email/-Groups.
// Those headers are trusted only together with X-Politrack-Proxy = POLITRACK_PROXY_SECRET, which only the proxy
// sets: a request that reaches the web container directly (LAN port) can't claim to be someone. Without the
// secret configured, nobody is signed in and reports are read-only.
import {headers} from 'next/headers';
export type Viewer = {subject: string; name: string; email: string | null; groups: string[]};
function same(a: string, b: string): boolean {
if (a.length !== b.length) return false;
let diff = 0;
for (let i = 0; i < a.length; i++) diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
return diff === 0;
}
export async function getViewer(): Promise<Viewer | null> {
const secret = process.env.POLITRACK_PROXY_SECRET;
if (!secret || !process.env.POLITRACK_AUTHOR_TOKEN) return null;
const h = await headers();
if (!same(h.get('x-politrack-proxy') ?? '', secret)) return null;
const subject = h.get('remote-user')?.trim();
if (!subject) return null;
return {
subject,
name: h.get('remote-name')?.trim() || subject,
email: h.get('remote-email')?.trim() || null,
groups: (h.get('remote-groups') ?? '').split(',').map((g) => g.trim()).filter(Boolean)
};
}
/** Headers for API calls on behalf of the viewer (values percent-encoded: names may contain umlauts). */
export function viewerHeaders(viewer: Viewer | null): Record<string, string> {
const token = process.env.POLITRACK_AUTHOR_TOKEN;
if (!viewer || !token) return {};
return {
Authorization: `Bearer ${token}`,
'X-Politrack-User': encodeURIComponent(viewer.subject),
'X-Politrack-User-Name': encodeURIComponent(viewer.name),
'X-Politrack-User-Email': encodeURIComponent(viewer.email ?? ''),
'X-Politrack-User-Groups': encodeURIComponent(viewer.groups.join(','))
};
}