feat(BIT-183): admin auth, RBAC, audit logging, rate limiting

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]>
This commit is contained in:
1sonhadorandPaperclip committed 2026-03-22 04:22:57 +00:00
1 parent d455a26333
commit eeb5039a3c
21 files changed
+1455

No files matched your search

+2
View File
@@ -0,0 +1,2 @@
# Copy to .env.local
NEXT_PUBLIC_API_URL=http://localhost:3200
+24
View File
@@ -0,0 +1,24 @@
{
"name": "@errors-bitreport/web",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev -p 3201",
"build": "next build",
"start": "next start -p 3201",
"lint": "next lint",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"next": "^14.2.3",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"axios": "^1.7.2"
},
"devDependencies": {
"@types/node": "^20.14.0",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"typescript": "^5.4.5"
}
}
+29
View File
@@ -0,0 +1,29 @@
/**
* Auth pages layout — loads Velzon assets and sets body class.
* Velzon CSS is served from /public/assets/ (CDN or local copy).
*/
import type { Metadata } from 'next';
export const metadata: Metadata = {
title: 'Sign In — BitReport Error Reporting',
};
export default function AuthLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en" data-bs-theme="default" data-topbar="dark" data-sidebar="dark">
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="/assets/css/bootstrap.min.css" />
<link rel="stylesheet" href="/assets/css/icons.min.css" />
<link rel="stylesheet" href="/assets/css/app.min.css" />
<link rel="icon" href="/favicon.ico" />
</head>
<body className="auth-bg">
{children}
<script src="/assets/js/bootstrap.bundle.min.js" defer />
<script src="/assets/js/app.js" defer />
</body>
</html>
);
}
+191
View File
@@ -0,0 +1,191 @@
/**
* Admin login page — Velzon Bootstrap 5 template style.
* Rendered server-side (no auth required).
* On successful login the API sets HTTP-only cookies; JS never sees the tokens.
*/
'use client';
import { useState, type FormEvent } from 'react';
import { useRouter } from 'next/navigation';
const API_URL = process.env.NEXT_PUBLIC_API_URL ?? 'http://localhost:3200';
export default function LoginPage() {
const router = useRouter();
const [email, set_email] = useState('');
const [password, set_password] = useState('');
const [error, set_error] = useState<string | null>(null);
const [loading, set_loading] = useState(false);
async function handle_submit(e: FormEvent<HTMLFormElement>) {
e.preventDefault();
set_error(null);
set_loading(true);
try {
const res = await fetch(`${API_URL}/admin/auth/login`, {
method: 'POST',
credentials: 'include', // send/receive HTTP-only cookies
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email: email.trim(), password }),
});
if (res.ok) {
router.push('/admin/dashboard');
return;
}
const data = await res.json().catch(() => ({}));
set_error(data?.message ?? 'Invalid credentials. Please try again.');
} catch {
set_error('Unable to connect to the server. Please try again.');
} finally {
set_loading(false);
}
}
return (
<>
{/* Velzon auth-pages body class is applied via layout */}
<div className="auth-page-wrapper pt-5">
<div className="auth-one-bg-position auth-one-bg" id="auth-particles">
<div className="bg-overlay" />
<div className="shape">
<svg
xmlns="http://www.w3.org/2000/svg"
version="1.1"
xmlnsXlink="http://www.w3.org/1999/xlink"
viewBox="0 0 1440 120"
>
<path d="M 0,36 C 144,53.6 288,71.2 432,64 C 576,56.8 720,25.6 864,19.2 C 1008,12.8 1152,31.2 1296,46.9 C 1440,62.6 1440,62.6 1440,62.6 L 1440,120 L 0,120 Z" />
</svg>
</div>
</div>
<div className="auth-page-content">
<div className="container">
<div className="row">
<div className="col-lg-12">
<div className="text-center mt-sm-5 mb-4 text-white-50">
<div>
<a href="/" className="d-inline-block auth-logo">
<img src="/assets/images/logo-light.png" alt="" height="20" />
</a>
</div>
<p className="mt-3 fs-15 fw-medium">
BitReport · Error Reporting Platform
</p>
</div>
</div>
</div>
<div className="row justify-content-center">
<div className="col-md-8 col-lg-6 col-xl-5">
<div className="card mt-4">
<div className="card-body p-4">
<div className="text-center mt-2">
<h5 className="text-primary">Welcome Back!</h5>
<p className="text-muted">Sign in to continue to Error Reporting.</p>
</div>
{error && (
<div className="alert alert-danger alert-dismissible" role="alert">
<i className="ri-error-warning-line me-1 align-middle" />
{error}
<button
type="button"
className="btn-close"
onClick={() => set_error(null)}
aria-label="Close"
/>
</div>
)}
<div className="p-2 mt-4">
<form onSubmit={handle_submit} autoComplete="on" noValidate>
<div className="mb-3">
<label htmlFor="email" className="form-label">
Email
</label>
<input
id="email"
type="email"
className="form-control"
placeholder="[email protected]"
value={email}
onChange={(e) => set_email(e.target.value)}
autoComplete="username"
required
disabled={loading}
/>
</div>
<div className="mb-3">
<div className="float-end">
{/* Password reset — future feature */}
</div>
<label htmlFor="password" className="form-label">
Password
</label>
<div className="position-relative auth-pass-inputgroup mb-3">
<input
id="password"
type="password"
className="form-control pe-5"
placeholder="Enter password"
value={password}
onChange={(e) => set_password(e.target.value)}
autoComplete="current-password"
required
disabled={loading}
/>
</div>
</div>
<div className="mt-4">
<button
type="submit"
className="btn btn-success w-100"
disabled={loading || !email || !password}
>
{loading ? (
<>
<span
className="spinner-border spinner-border-sm me-1"
role="status"
aria-hidden="true"
/>
Signing In…
</>
) : (
'Sign In'
)}
</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<footer className="footer">
<div className="container">
<div className="row">
<div className="col-lg-12">
<div className="text-center">
<p className="mb-0 text-muted">
&copy; {new Date().getFullYear()} BitReport. Crafted with{' '}
<i className="mdi mdi-heart text-danger" /> by BitReport Team
</p>
</div>
</div>
</div>
</div>
</footer>
</div>
</>
);
}