refactor(admin): separates Admin-Login statt is_admin-Flag
Auf Wunsch: Betreiber-Bereich getrennt von Kundenkonten. - auth.js: signAdminToken (role:operator), requireAdmin prüft Token-Rolle; requireAuth weist Operator-Token ab (saubere Trennung beide Richtungen) - routes/admin.js: POST /admin/login (ADMIN_PASSWORD → Operator-Token) - env.js: adminPassword statt adminEmail - 0003_admin.sql: droppt die nicht mehr genutzte accounts.is_admin-Spalte - register/login/account/me: is_admin restlos entfernt E2E: Kunde→403, falsches PW→401, richtiges PW→Token, stats→200, Admin-Token→Kundenroute→401. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
+23
-31
@@ -1,10 +1,10 @@
|
||||
// HOST-Kundenkonten: Passwort-Hashing + JWT-Ausstellung/-Prüfung.
|
||||
// (Das sind die Konten auf der RAPPORT-HOST-Plattform — NICHT die Endnutzer
|
||||
// in den einzelnen Rapport-Instanzen.)
|
||||
// HOST-Auth. Zwei getrennte Welten:
|
||||
// • Kundenkonten (accounts) — Register/Login, JWT mit {sub,email}
|
||||
// • Betreiber/Admin — SEPARATES Login mit ADMIN_PASSWORD, JWT mit {role:operator}
|
||||
// Ein Kunde kann NIE Admin werden; Admin ist kein Kundenkonto.
|
||||
import bcrypt from "bcryptjs";
|
||||
import jwt from "jsonwebtoken";
|
||||
import { env } from "./env.js";
|
||||
import { one, query } from "./db.js";
|
||||
|
||||
export async function hashPassword(plain) {
|
||||
return bcrypt.hash(plain, 10);
|
||||
@@ -14,47 +14,39 @@ export async function verifyPassword(plain, hash) {
|
||||
return bcrypt.compare(plain, hash);
|
||||
}
|
||||
|
||||
// — Kunden-Token —
|
||||
export function signToken(account) {
|
||||
return jwt.sign(
|
||||
{ sub: account.id, email: account.email, is_admin: !!account.is_admin },
|
||||
env.jwtSecret,
|
||||
{ expiresIn: "7d" }
|
||||
);
|
||||
return jwt.sign({ sub: account.id, email: account.email }, env.jwtSecret, { expiresIn: "7d" });
|
||||
}
|
||||
|
||||
// Promotet das Konto zum Admin, wenn die E-Mail ADMIN_EMAIL entspricht.
|
||||
// Wird bei Register/Login aufgerufen, damit dein Konto ohne manuellen DB-Eingriff
|
||||
// Admin wird. Gibt das (ggf. aktualisierte) is_admin zurück.
|
||||
export async function ensureAdminFlag(account) {
|
||||
const shouldBeAdmin = env.adminEmail && account.email.toLowerCase() === env.adminEmail;
|
||||
if (shouldBeAdmin && !account.is_admin) {
|
||||
await query("update accounts set is_admin = true where id = $1", [account.id]);
|
||||
return true;
|
||||
}
|
||||
return !!account.is_admin;
|
||||
// — Admin/Betreiber-Token (eigene Rolle, kürzere Laufzeit) —
|
||||
export function signAdminToken() {
|
||||
return jwt.sign({ role: "operator" }, env.jwtSecret, { expiresIn: "12h" });
|
||||
}
|
||||
|
||||
// Express-Middleware: setzt req.account aus dem Bearer-Token oder 401.
|
||||
// Middleware: eingeloggter Kunde (oder 401).
|
||||
export function requireAuth(req, res, next) {
|
||||
const header = req.headers.authorization || "";
|
||||
const token = header.startsWith("Bearer ") ? header.slice(7) : null;
|
||||
const token = (req.headers.authorization || "").replace(/^Bearer /, "");
|
||||
if (!token) return res.status(401).json({ error: "Nicht angemeldet." });
|
||||
try {
|
||||
const payload = jwt.verify(token, env.jwtSecret);
|
||||
req.account = { id: payload.sub, email: payload.email, is_admin: !!payload.is_admin };
|
||||
const p = jwt.verify(token, env.jwtSecret);
|
||||
if (p.role === "operator") return res.status(401).json({ error: "Admin-Token, kein Kundenkonto." });
|
||||
req.account = { id: p.sub, email: p.email };
|
||||
next();
|
||||
} catch {
|
||||
res.status(401).json({ error: "Session ungültig oder abgelaufen." });
|
||||
}
|
||||
}
|
||||
|
||||
// Wie requireAuth, aber verlangt Admin. Prüft die DB (autoritativ — nicht nur
|
||||
// das Token), damit ein entzogenes Admin-Recht sofort greift.
|
||||
// Middleware: Betreiber/Admin (Operator-Rolle im Token, oder 403).
|
||||
export function requireAdmin(req, res, next) {
|
||||
requireAuth(req, res, async () => {
|
||||
const row = await one("select is_admin from accounts where id = $1", [req.account.id]);
|
||||
if (!row || !row.is_admin) return res.status(403).json({ error: "Kein Admin-Zugriff." });
|
||||
req.account.is_admin = true;
|
||||
const token = (req.headers.authorization || "").replace(/^Bearer /, "");
|
||||
if (!token) return res.status(401).json({ error: "Nicht angemeldet." });
|
||||
try {
|
||||
const p = jwt.verify(token, env.jwtSecret);
|
||||
if (p.role !== "operator") return res.status(403).json({ error: "Kein Admin-Zugriff." });
|
||||
next();
|
||||
});
|
||||
} catch {
|
||||
res.status(401).json({ error: "Session ungültig oder abgelaufen." });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user