Files
oikos/public/api.js
T
ulsklyc dd8ad80eb4 feat: Phase 5 — Härtung (CSRF, Rate-Limit, Validation, Error Boundary, README)
Schritt 28 — CSRF-Schutz (Double Submit Cookie Pattern):
- server/middleware/csrf.js: generiert 32-Byte-Token, speichert in Session + Cookie;
  validiert X-CSRF-Token-Header auf POST/PUT/PATCH/DELETE via timingSafeEqual
- server/auth.js: CSRF-Token beim Login erzeugen und als Cookie setzen
- public/api.js: getCsrfToken() liest Cookie; apiFetch() sendet Header auf
  state-ändernden Requests automatisch

Schritt 29 — Globaler Rate-Limiter:
- server/index.js: apiLimiter (300 req/min/IP) auf allen /api/-Routen;
  ergänzt den bestehenden loginLimiter (5 req/min)

Schritt 27 — Zentralisierte Eingabe-Validierung:
- server/middleware/validate.js: str(), oneOf(), date(), time(), num(), color(),
  collectErrors() mit einheitlichen Längengrenzen (MAX_TITLE=200, MAX_TEXT=5000)
- server/routes/tasks.js: validateTaskInput() nutzt nun validate.js

Schritt 31 — Frontend Error Boundary:
- public/router.js: window.onerror + unhandledrejection-Handler zeigen Toast

Schritt 33 — README.md:
- Setup-Anleitung (Docker + Node.js), Nginx-Config, User-Verwaltung,
  Umgebungsvariablen-Referenz, Backup, Sicherheitsübersicht

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-24 22:00:47 +01:00

108 lines
2.8 KiB
JavaScript

/**
* Modul: API-Client
* Zweck: Fetch-Wrapper mit Session-Auth, einheitlicher Fehlerbehandlung und JSON-Parsing
* Abhängigkeiten: keine
*/
const API_BASE = '/api/v1';
/** Liest den CSRF-Token aus dem Cookie (gesetzt vom Server nach Login). */
function getCsrfToken() {
return document.cookie.split(';')
.map((c) => c.trim())
.find((c) => c.startsWith('csrf-token='))
?.slice('csrf-token='.length) ?? '';
}
/**
* Zentraler Fetch-Wrapper.
* Setzt Content-Type, handhabt 401-Redirects und parsed JSON-Fehler.
*
* @param {string} path - API-Pfad ohne /api/v1 (z.B. '/tasks')
* @param {RequestInit} options - Fetch-Optionen
* @returns {Promise<any>} Geparstes JSON oder wirft einen Fehler
*/
async function apiFetch(path, options = {}) {
const url = `${API_BASE}${path}`;
const method = options.method ?? 'GET';
const stateChanging = ['POST', 'PUT', 'PATCH', 'DELETE'].includes(method);
const response = await fetch(url, {
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
...(stateChanging ? { 'X-CSRF-Token': getCsrfToken() } : {}),
...options.headers,
},
...options,
});
if (response.status === 401) {
// Session abgelaufen → zur Login-Seite
window.dispatchEvent(new CustomEvent('auth:expired'));
throw new Error('Sitzung abgelaufen.');
}
const data = await response.json().catch(() => null);
if (!response.ok) {
const message = data?.error || `HTTP ${response.status}`;
throw new ApiError(message, response.status, data);
}
return data;
}
/**
* Strukturierter API-Fehler mit HTTP-Status-Code.
*/
class ApiError extends Error {
constructor(message, status, data = null) {
super(message);
this.name = 'ApiError';
this.status = status;
this.data = data;
}
}
// --------------------------------------------------------
// Convenience-Methoden
// --------------------------------------------------------
const api = {
get: (path) => apiFetch(path, { method: 'GET' }),
post: (path, body) => apiFetch(path, {
method: 'POST',
body: JSON.stringify(body),
}),
put: (path, body) => apiFetch(path, {
method: 'PUT',
body: JSON.stringify(body),
}),
patch: (path, body) => apiFetch(path, {
method: 'PATCH',
body: JSON.stringify(body),
}),
delete: (path) => apiFetch(path, { method: 'DELETE' }),
};
// --------------------------------------------------------
// Auth-spezifische Methoden
// --------------------------------------------------------
const auth = {
login: (username, password) => api.post('/auth/login', { username, password }),
logout: () => api.post('/auth/logout'),
me: () => api.get('/auth/me'),
getUsers: () => api.get('/auth/users'),
createUser: (data) => api.post('/auth/users', data),
deleteUser: (id) => api.delete(`/auth/users/${id}`),
};
export { api, auth, ApiError };