source: Klonkt/src/routes/admin-paid.js@ e685f55

main
Last change on this file since e685f55 was 61e3daf, checked in by Robin <roboburr@…>, 7 weeks ago

Feature: paid posts slice 1, owner Patreon config (encrypted)

The site owner can connect their OWN Patreon campaign for paid posts
(klonkt-demo-aki), premium-gated in Beheer. Client id/secret, campaign
id and the creator access/refresh token are stored ENCRYPTED at rest
(new CryptoBox AES-256-GCM helper, key from PAID_SECRET), so a database
dump leaks nothing usable; the token auto-refreshes. Separate from
Klonkt Premium's license flow, which is untouched. Degrades gracefully:
without PAID_SECRET the admin page refuses to save rather than storing
plaintext. Nothing patron-facing yet (posts.paid + unlock come in
slices 2 to 4), so no changelog entry.

CryptoBox also carries the cookie-less signed-blob helper (signBlob/
verifyBlob) that slices 3 and 4 reuse for the OAuth state and the
WebAuthn challenge.

Changed files:
src/config/database.js

  • paid_patreon table (site_id PK, secrets encrypted)

src/server.js

  • mount /admin/paid

src/views/pages/admin.ejs

  • "Betaalde posts" button in Beheer

New file:
src/services/CryptoBox.js

  • aes-256-gcm encrypt/decrypt + HMAC signBlob/verifyBlob

src/services/PaidPatreonService.js

  • owner config CRUD (encrypted), token refresh, creatorAccessToken

src/routes/admin-paid.js

  • premium-gated config form (GET/POST/disconnect)

src/views/pages/admin-paid.ejs

  • the form + status

test/paid-patreon.test.js

  • crypto roundtrip, no-plaintext-in-DB, refresh, blob signing

docs/paid-posts-design.md, docs/privacy-betaalde-posts.md

  • concurrency property documented

-robo
Co-Authored-By: Claude Opus 4.8 <noreply@…>

  • Property mode set to 100644
File size: 2.7 KB
Line 
1/**
2 * Admin: Paid posts (premium module, god-only). Slice 1 of klonkt-demo-aki.
3 *
4 * GET /admin/paid -> the owner's Patreon config form + status
5 * POST /admin/paid -> save config (secret/token stored encrypted)
6 * POST /admin/paid/disconnect -> forget the config
7 *
8 * Premium-gated via premiumUnlocked(), like stats/downloads. This is the site
9 * owner's OWN Patreon campaign, separate from Klonkt Premium's license flow.
10 */
11import express from 'express';
12import { renderPage } from '../middleware/render.js';
13import { requireGod } from '../middleware/auth.js';
14import { premiumUnlocked } from '../services/PatreonService.js';
15import { cryptoBoxReady } from '../services/CryptoBox.js';
16import PaidPatreon from '../services/PaidPatreonService.js';
17
18const router = express.Router();
19
20function gate(req, res) {
21 if (!premiumUnlocked()) {
22 res.status(403).send('Betaalde posts is een premium-functie: koppel Patreon in Beheer, Instellingen.');
23 return false;
24 }
25 if (!res.locals.site) { res.status(400).send('Geen site.'); return false; }
26 return true;
27}
28
29router.get('/', requireGod, (req, res) => {
30 if (!gate(req, res)) return;
31 renderPage(req, res, 'pages/admin-paid', {
32 pageTitle: 'Betaalde posts',
33 bodyClass: 'on-admin',
34 status: PaidPatreon.ownerStatus(res.locals.site.id),
35 secretReady: cryptoBoxReady(),
36 saved: req.query.saved === '1',
37 error: req.query.error || null,
38 });
39});
40
41router.post('/', requireGod, (req, res) => {
42 if (!gate(req, res)) return;
43 if (!cryptoBoxReady()) return res.redirect('/admin/paid?error=' + encodeURIComponent('PAID_SECRET ontbreekt in de serverconfig; secrets kunnen niet versleuteld worden opgeslagen.'));
44 const b = req.body || {};
45 const eur = String(b.default_min_eur || '').replace(',', '.').trim();
46 const cents = eur ? Math.round(parseFloat(eur) * 100) : undefined;
47 try {
48 PaidPatreon.saveOwnerConfig(res.locals.site.id, {
49 clientId: (b.client_id || '').trim() || undefined,
50 // Empty secret/token fields keep the stored value (no re-paste needed).
51 clientSecret: (b.client_secret || '').trim() || undefined,
52 campaignId: (b.campaign_id || '').trim() || undefined,
53 accessToken: (b.access_token || '').trim() || undefined,
54 refreshToken: (b.refresh_token || '').trim() || undefined,
55 defaultMinCents: Number.isFinite(cents) ? cents : undefined,
56 });
57 return res.redirect('/admin/paid?saved=1');
58 } catch (e) {
59 return res.redirect('/admin/paid?error=' + encodeURIComponent(e.message || 'Opslaan mislukt'));
60 }
61});
62
63router.post('/disconnect', requireGod, (req, res) => {
64 if (!gate(req, res)) return;
65 PaidPatreon.disconnect(res.locals.site.id);
66 res.redirect('/admin/paid?saved=1');
67});
68
69export default router;
Note: See TracBrowser for help on using the repository browser.