Vollständiges Zugangs-/Rollen-/Rechtesystem gebaut (Anforderungsdokument): D1-Datenbank, AES-verschlüsselte Codes nur für Owner einsehbar, granulare Permissions, Session-Auth mit Lockout, Zugänge-&-Teammitglieder-Verwaltungsseite, Bewerbungen mit Status/Notizen/Entwürfen
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
/* =====================================================================
|
||||
admin-auth.js — gemeinsame Session-Logik für postfach.html und
|
||||
zugaenge.html. Session (Token + Rolle/Rechte) liegt in sessionStorage
|
||||
(pro Tab, verschwindet beim Schließen) — der Server erzwingt zusätzlich
|
||||
Inaktivitäts-Timeout (30 Min) und eine absolute Höchstdauer (12 Std).
|
||||
===================================================================== */
|
||||
|
||||
const SESSION_KEY = "dogfather_admin_session";
|
||||
|
||||
function getSession() {
|
||||
try {
|
||||
return JSON.parse(sessionStorage.getItem(SESSION_KEY) || "null");
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function setSession(data) {
|
||||
sessionStorage.setItem(SESSION_KEY, JSON.stringify(data));
|
||||
}
|
||||
|
||||
function clearSession() {
|
||||
sessionStorage.removeItem(SESSION_KEY);
|
||||
}
|
||||
|
||||
/** Prüft, ob die aktuelle Session (Owner oder Rolle+Overrides) ein Recht hat. */
|
||||
function sessionHasPermission(session, key) {
|
||||
if (!session) return false;
|
||||
if (session.isOwner) return true;
|
||||
const overrides = session.overrides || {};
|
||||
if (Object.prototype.hasOwnProperty.call(overrides, key)) return !!overrides[key];
|
||||
return Array.isArray(session.permissions) && session.permissions.includes(key);
|
||||
}
|
||||
|
||||
async function apiCall(apiBaseUrl, path, body, { auth = true } = {}) {
|
||||
const session = getSession();
|
||||
const headers = { "Content-Type": "application/json" };
|
||||
if (auth && session?.token) headers["Authorization"] = `Bearer ${session.token}`;
|
||||
|
||||
const res = await fetch(`${apiBaseUrl}${path}`, {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify(body || {}),
|
||||
});
|
||||
let data;
|
||||
try {
|
||||
data = await res.json();
|
||||
} catch {
|
||||
data = { ok: false, error: "Ungültige Antwort vom Server." };
|
||||
}
|
||||
if (res.status === 401 && auth) {
|
||||
clearSession(); // Session ist abgelaufen/ungültig -> zurück zum Login zwingen
|
||||
}
|
||||
return { httpOk: res.ok, ...data };
|
||||
}
|
||||
|
||||
async function doLogin(apiBaseUrl, code) {
|
||||
const res = await apiCall(apiBaseUrl, "/auth/login", { code }, { auth: false });
|
||||
if (res.ok) {
|
||||
setSession({
|
||||
token: res.token,
|
||||
isOwner: res.isOwner,
|
||||
name: res.name,
|
||||
username: res.username,
|
||||
roleName: res.roleName,
|
||||
permissions: res.permissions || [],
|
||||
overrides: res.overrides || {},
|
||||
});
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
async function doLogout(apiBaseUrl) {
|
||||
await apiCall(apiBaseUrl, "/auth/logout", {});
|
||||
clearSession();
|
||||
}
|
||||
|
||||
// Für Einbindung ohne Modul-Bundler global verfügbar machen.
|
||||
if (typeof window !== "undefined") {
|
||||
window.AdminAuth = { getSession, setSession, clearSession, sessionHasPermission, apiCall, doLogin, doLogout };
|
||||
}
|
||||
Reference in New Issue
Block a user