| 1 | // routes/federation.js — publieke Cirkels-endpoints (v1, publicatie-kant).
|
|---|
| 2 | //
|
|---|
| 3 | // GET /.klonkt/actor.json — ActivityStreams-actor + Ed25519-pubkey
|
|---|
| 4 | // GET /.klonkt/outbox.json — publieke posts als AS Create-objecten,
|
|---|
| 5 | // getekend via de Klonkt-Signature-header
|
|---|
| 6 | //
|
|---|
| 7 | // Site-agnostisch en zonder auth — alleen lezen. Zie docs/cirkels-v1-spec.md.
|
|---|
| 8 |
|
|---|
| 9 | import express from 'express';
|
|---|
| 10 | import { buildActor, buildOutbox, signBody, KLONKT_PROTO, MIN_PROTO } from '../services/CircleFederation.js';
|
|---|
| 11 | import { getTenancy } from '../services/SettingsService.js';
|
|---|
| 12 |
|
|---|
| 13 | const router = express.Router();
|
|---|
| 14 |
|
|---|
| 15 | function baseUrl(req) {
|
|---|
| 16 | const b = process.env.PUBLIC_BASE_URL || `${req.protocol}://${req.get('host')}`;
|
|---|
| 17 | return b.replace(/\/+$/, '');
|
|---|
| 18 | }
|
|---|
| 19 |
|
|---|
| 20 | // De proto die de consument zegt te draaien (uit z'n request-header), of 0.
|
|---|
| 21 | function consumerProto(req) {
|
|---|
| 22 | return parseInt(req.get('Klonkt-Proto') || '0', 10) || 0;
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | router.get('/.klonkt/actor.json', (req, res) => {
|
|---|
| 26 | // Cirkels = solo-naar-solo; hubs publiceren geen federatie-actor.
|
|---|
| 27 | if (getTenancy() === 'hub') return res.status(404).type('text/plain').send('Niet beschikbaar in hub-modus');
|
|---|
| 28 | // De actor serveren we ALTIJD (ook aan oudere consumenten) zodat zij onze proto
|
|---|
| 29 | // kunnen lezen en een nette "update vereist"-melding kunnen tonen.
|
|---|
| 30 | const body = JSON.stringify(buildActor(baseUrl(req)), null, 2);
|
|---|
| 31 | res.type('application/activity+json; charset=utf-8');
|
|---|
| 32 | res.set('Klonkt-Proto', String(KLONKT_PROTO));
|
|---|
| 33 | res.set('Cache-Control', 'public, max-age=300');
|
|---|
| 34 | res.send(body);
|
|---|
| 35 | });
|
|---|
| 36 |
|
|---|
| 37 | router.get('/.klonkt/outbox.json', (req, res) => {
|
|---|
| 38 | if (getTenancy() === 'hub') return res.status(404).type('text/plain').send('Niet beschikbaar in hub-modus');
|
|---|
| 39 | res.set('Klonkt-Proto', String(KLONKT_PROTO));
|
|---|
| 40 | // Te-oude consument? Weiger met 426 Upgrade Required (de crypto-binding sluit 'm
|
|---|
| 41 | // sowieso al uit; dit geeft een expliciet, leesbaar signaal). proto 0 = geen
|
|---|
| 42 | // header (bv. een browser/curl) → toestaan, die verifieert toch niet.
|
|---|
| 43 | const cp = consumerProto(req);
|
|---|
| 44 | if (cp && cp < MIN_PROTO) {
|
|---|
| 45 | return res.status(426).type('text/plain')
|
|---|
| 46 | .send(`Upgrade Required: deze cirkel draait proto ${KLONKT_PROTO}; jouw Klonkt (proto ${cp}) is te oud.`);
|
|---|
| 47 | }
|
|---|
| 48 | const body = JSON.stringify(buildOutbox(baseUrl(req)), null, 2);
|
|---|
| 49 | res.type('application/activity+json; charset=utf-8');
|
|---|
| 50 | res.set('Cache-Control', 'public, max-age=300');
|
|---|
| 51 | res.set('Klonkt-Signature', `ed25519=${signBody(body)}`);
|
|---|
| 52 | res.send(body);
|
|---|
| 53 | });
|
|---|
| 54 |
|
|---|
| 55 | export default router;
|
|---|