Sifa professional network API (Fastify, AT Protocol, Jetstream) sifa.id/
at main 25 lines 826 B view raw
1import type { FastifyRequest, FastifyReply } from 'fastify'; 2import type { Env } from '../config.js'; 3import './types.js'; 4 5/** 6 * Creates an admin middleware that checks `request.did` against the 7 * comma-separated allowlist in `config.ADMIN_DIDS`. 8 * 9 * Runs after auth middleware (which sets `request.did`). 10 * Fails closed: if ADMIN_DIDS is unset or empty, all requests are denied. 11 */ 12export function createAdminMiddleware(config: Env) { 13 const adminDids = new Set( 14 (config.ADMIN_DIDS ?? '') 15 .split(',') 16 .map((d) => d.trim()) 17 .filter(Boolean), 18 ); 19 20 return async function requireAdmin(request: FastifyRequest, reply: FastifyReply) { 21 if (!request.did || !adminDids.has(request.did)) { 22 return reply.status(403).send({ error: 'Forbidden', message: 'Admin access required' }); 23 } 24 }; 25}