| [7bc636b] | 1 | /**
|
|---|
| 2 | * Auth middleware
|
|---|
| 3 | */
|
|---|
| 4 |
|
|---|
| [8cb1dc7] | 5 | import db from '../config/database.js';
|
|---|
| [98ecf51] | 6 | import PermissionsService from '../services/PermissionsService.js';
|
|---|
| [8cb1dc7] | 7 |
|
|---|
| [7bc636b] | 8 | /**
|
|---|
| 9 | * Validate a "next" URL for safe redirect after login.
|
|---|
| 10 | * Returns the URL if safe, otherwise null.
|
|---|
| 11 | *
|
|---|
| 12 | * Rules:
|
|---|
| 13 | * - Must be a string, max 256 chars (prevent abuse).
|
|---|
| 14 | * - Must start with "/" but NOT "//" or "/\" (no protocol-relative open redirects).
|
|---|
| 15 | * - Must not point back at /auth/* (prevents login → login loop).
|
|---|
| 16 | */
|
|---|
| 17 | export function safeNext(raw) {
|
|---|
| 18 | if (typeof raw !== 'string' || !raw.length || raw.length > 256) return null;
|
|---|
| 19 | if (raw[0] !== '/' || raw[1] === '/' || raw[1] === '\\') return null;
|
|---|
| 20 | if (/^\/auth(\/|$)/i.test(raw)) return null;
|
|---|
| 21 | return raw;
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | function loginRedirect(req, res) {
|
|---|
| 25 | // Preserve the originally-requested URL so login can return us there.
|
|---|
| 26 | const next = encodeURIComponent(req.originalUrl || req.url || '/');
|
|---|
| 27 | const target = `/auth/login?next=${next}`;
|
|---|
| 28 | if (req.headers['hx-request'] === 'true') {
|
|---|
| 29 | res.setHeader('HX-Redirect', target);
|
|---|
| 30 | return res.status(401).send('Login required');
|
|---|
| 31 | }
|
|---|
| 32 | return res.redirect(target);
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | export function requireAuth(req, res, next) {
|
|---|
| 36 | if (!req.session?.user) return loginRedirect(req, res);
|
|---|
| 37 | next();
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| [834bcc3] | 40 | // A 'kijker' (viewer) may VIEW everything (incl. Admin) but CHANGE nothing. The
|
|---|
| 41 | // write block lives in the global guard in server.js; this helper only determines
|
|---|
| 42 | // "is this a read-only account?". `readonly` is the legacy flag we still include
|
|---|
| 43 | // so unmigrated demo accounts remain blocked.
|
|---|
| [8afbdd6] | 44 | export function isViewer(user) {
|
|---|
| 45 | return !!user && (user.role === 'kijker' || !!user.readonly);
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| [7bc636b] | 48 | export function requireGod(req, res, next) {
|
|---|
| 49 | if (!req.session?.user) return loginRedirect(req, res);
|
|---|
| [8afbdd6] | 50 | const role = req.session.user.role;
|
|---|
| [834bcc3] | 51 | // god manages; a viewer MAY see the Admin panel (read-only) — the
|
|---|
| 52 | // global guard 403s every write, so this only grants view access.
|
|---|
| [8afbdd6] | 53 | if (role !== 'god' && role !== 'kijker') {
|
|---|
| [7bc636b] | 54 | return res.status(403).send('God role required');
|
|---|
| 55 | }
|
|---|
| 56 | next();
|
|---|
| 57 | }
|
|---|
| [8cb1dc7] | 58 |
|
|---|
| [834bcc3] | 59 | // Can the logged-in user manage the CURRENT site (res.locals.site)? god always;
|
|---|
| 60 | // otherwise only the owner of that site. Used for site-scoped admin routes
|
|---|
| 61 | // that an artist reaches via /user/<own-slug>/admin/... (res.locals.site is then
|
|---|
| 62 | // their own site; a foreign slug yields a different site -> 403).
|
|---|
| [8cb1dc7] | 63 | export function requireSiteManager(req, res, next) {
|
|---|
| 64 | if (!req.session?.user) return loginRedirect(req, res);
|
|---|
| 65 | const u = req.session.user;
|
|---|
| [834bcc3] | 66 | if (u.role === 'god' || u.role === 'kijker') return next(); // viewer = read-only view access
|
|---|
| [8cb1dc7] | 67 | const site = res.locals.site;
|
|---|
| [834bcc3] | 68 | // owner OR assigned co-admin (site_members) — canAdminSite covers both.
|
|---|
| [98ecf51] | 69 | if (site && PermissionsService.canAdminSite(u, site)) return next();
|
|---|
| [8cb1dc7] | 70 | return res.status(403).send('Geen toegang tot deze site.');
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| [834bcc3] | 73 | // Same, but the site is determined by the :slug parameter (e.g. site-edit).
|
|---|
| [8cb1dc7] | 74 | export function requireSiteManagerBySlug(req, res, next) {
|
|---|
| 75 | if (!req.session?.user) return loginRedirect(req, res);
|
|---|
| 76 | const u = req.session.user;
|
|---|
| [834bcc3] | 77 | if (u.role === 'god' || u.role === 'kijker') return next(); // viewer = read-only view access
|
|---|
| [98ecf51] | 78 | const site = db.prepare('SELECT id, owner_id FROM sites WHERE slug = ?').get(req.params.slug);
|
|---|
| 79 | if (site && PermissionsService.canAdminSite(u, site)) return next();
|
|---|
| [8cb1dc7] | 80 | return res.status(403).send('Geen toegang tot deze site.');
|
|---|
| 81 | }
|
|---|