Implements BIT-154.7 — Auth administrativa, RBAC e quotas: - api/src/types/auth.ts: 3 roles (superadmin/admin/viewer), 18 permissions, ROLE_PERMISSIONS map as single source of truth - api/src/plugins/auth.ts: JWT via HTTP-only __Host- cookies (15min access / 7d refresh), token rotation, timing-safe login - api/src/plugins/rbac.ts: requirePermission() + requireRole() Fastify decorators for route-level enforcement - api/src/plugins/audit.ts: auditLog() helper + Fastify plugin; writes to audit_logs table; failure never aborts main operation - api/src/plugins/rate-limit.ts: per-route limits (login: 10/5min IP, ingest: 1000/min token, global: 500/min) + payload size enforcement - api/src/routes/admin/auth.ts: POST login, POST refresh, POST logout, GET /me — with audit events on all operations - db/migrations/001–004: admin_users, refresh_tokens, audit_logs tables + dev seed (bcrypt superadmin) - web/src/app/(admin)/login: Velzon Bootstrap 5 login page (Next.js client component, credentials:include, HTTP-only cookie flow) - docs/permission-matrix.md: full permission matrix + audit event catalogue + rate/payload limit table (BIT-183 acceptance criterion) Co-Authored-By: Paperclip <[email protected]>
40 lines
1.9 KiB
SQL
40 lines
1.9 KiB
SQL
-- Migration 003: audit_logs table
|
|
-- Append-only audit trail for critical admin operations.
|
|
-- No UPDATE or DELETE on this table — immutability is the point.
|
|
-- Partitioning by month recommended when volume grows (phase 2).
|
|
|
|
CREATE TABLE IF NOT EXISTS audit_logs (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
action VARCHAR(128) NOT NULL, -- e.g. 'auth.login', 'projects.delete'
|
|
actor_id UUID, -- NULL for unauthenticated events (login failures)
|
|
actor_ip INET,
|
|
target TEXT NOT NULL DEFAULT '', -- email, project ID, token ID, etc.
|
|
metadata JSONB NOT NULL DEFAULT '{}',
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
|
);
|
|
|
|
-- Query patterns:
|
|
-- 1. All actions by a user, recent first
|
|
CREATE INDEX IF NOT EXISTS audit_logs_actor_time_idx
|
|
ON audit_logs (actor_id, created_at DESC)
|
|
WHERE actor_id IS NOT NULL;
|
|
|
|
-- 2. All events of a specific type, recent first
|
|
CREATE INDEX IF NOT EXISTS audit_logs_action_time_idx
|
|
ON audit_logs (action, created_at DESC);
|
|
|
|
-- 3. Recent activity feed (dashboard)
|
|
CREATE INDEX IF NOT EXISTS audit_logs_time_idx
|
|
ON audit_logs (created_at DESC);
|
|
|
|
-- Revoke DELETE and UPDATE on this table to enforce immutability.
|
|
-- (Run as superuser after migration)
|
|
-- REVOKE DELETE, UPDATE, TRUNCATE ON audit_logs FROM PUBLIC;
|
|
-- REVOKE DELETE, UPDATE, TRUNCATE ON audit_logs FROM app_user;
|
|
|
|
COMMENT ON TABLE audit_logs IS 'Append-only audit trail. Critical: no DELETEs or UPDATEs allowed.';
|
|
COMMENT ON COLUMN audit_logs.action IS 'Dot-notation: domain.verb (e.g. auth.login, projects.delete)';
|
|
COMMENT ON COLUMN audit_logs.actor_id IS 'NULL for pre-auth events (failed logins)';
|
|
COMMENT ON COLUMN audit_logs.target IS 'Human-readable target: email, ID, or description';
|
|
COMMENT ON COLUMN audit_logs.metadata IS 'Structured context: role, reason, old/new values (no secrets)';
|