This commit is contained in:
63
packages/server/src/middleware/assert-tier.ts
Normal file
63
packages/server/src/middleware/assert-tier.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* Tier enforcement middleware for Fastify routes.
|
||||
*
|
||||
* Usage:
|
||||
* server.post('/api/team/connect', { preHandler: [requireTier('TEAMS')] }, handler)
|
||||
*
|
||||
* Reads the current tier from config.json (same source as GET /api/tier).
|
||||
* Returns 403 with structured error when tier is insufficient.
|
||||
*/
|
||||
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import type { FastifyRequest, FastifyReply } from 'fastify';
|
||||
import { type Tier, assertTierCapability, TierError, parseTier, getEffectiveTier } from '@waggle/shared';
|
||||
|
||||
/**
|
||||
* Read the effective tier for a data directory.
|
||||
* Uses the same config.json source as the GET /api/tier route in settings.ts.
|
||||
* Also consumed by startService's D11 startup log line (no request in scope there).
|
||||
*/
|
||||
export function readTierFromDataDir(dataDir: string | undefined): Tier {
|
||||
try {
|
||||
if (!dataDir) return 'FREE';
|
||||
const configPath = path.join(dataDir, 'config.json');
|
||||
if (fs.existsSync(configPath)) {
|
||||
const raw = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
|
||||
const parsed = parseTier(String(raw.tier ?? ''));
|
||||
if (parsed) return getEffectiveTier(parsed, raw.trialStartedAt ?? null);
|
||||
}
|
||||
} catch { /* unreadable config → fail closed to FREE */ }
|
||||
return 'FREE';
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the current tier from the server's data directory.
|
||||
*/
|
||||
export function readTierFromRequest(request: FastifyRequest): Tier {
|
||||
return readTierFromDataDir(request.server.localConfig?.dataDir);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fastify preHandler that enforces a minimum tier.
|
||||
* Returns 403 TIER_INSUFFICIENT if the current tier is below the required level.
|
||||
*/
|
||||
export function requireTier(minimumTier: Tier) {
|
||||
return async (request: FastifyRequest, reply: FastifyReply): Promise<void> => {
|
||||
const userTier = readTierFromRequest(request);
|
||||
try {
|
||||
assertTierCapability(userTier, minimumTier);
|
||||
} catch (e) {
|
||||
if (e instanceof TierError) {
|
||||
return void reply.code(403).send({
|
||||
error: 'TIER_INSUFFICIENT',
|
||||
message: `This feature requires the ${e.required} tier. You are on ${e.actual}.`,
|
||||
required: e.required,
|
||||
actual: e.actual,
|
||||
upgradeUrl: 'https://waggle-os.ai/upgrade',
|
||||
});
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
}
|
||||
42
packages/server/src/middleware/audit.ts
Normal file
42
packages/server/src/middleware/audit.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import type { AuditService } from '../services/audit-service.js';
|
||||
import type { agentAuditLog } from '../db/schema.js';
|
||||
|
||||
export function createAuditWrapper(auditService: AuditService) {
|
||||
return async function auditAction(params: {
|
||||
userId: string;
|
||||
teamId?: string;
|
||||
agentName: string;
|
||||
actionType: string;
|
||||
description: string;
|
||||
beforeState?: Record<string, unknown>;
|
||||
requiresApproval?: boolean;
|
||||
action: () => Promise<Record<string, unknown>>;
|
||||
}): Promise<{ auditEntry: typeof agentAuditLog.$inferSelect; result?: Record<string, unknown> }> {
|
||||
if (params.requiresApproval) {
|
||||
// Don't execute -- just log for approval
|
||||
const auditEntry = await auditService.log({
|
||||
userId: params.userId,
|
||||
teamId: params.teamId,
|
||||
agentName: params.agentName,
|
||||
actionType: params.actionType,
|
||||
description: params.description,
|
||||
beforeState: params.beforeState,
|
||||
requiresApproval: true,
|
||||
});
|
||||
return { auditEntry };
|
||||
}
|
||||
|
||||
// Execute and log
|
||||
const result = await params.action();
|
||||
const auditEntry = await auditService.log({
|
||||
userId: params.userId,
|
||||
teamId: params.teamId,
|
||||
agentName: params.agentName,
|
||||
actionType: params.actionType,
|
||||
description: params.description,
|
||||
beforeState: params.beforeState,
|
||||
afterState: result,
|
||||
});
|
||||
return { auditEntry, result };
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user