Compare commits
2
Commits
b8b8ec7b7a
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
654f4455cc | ||
|
|
c9e4b0cdbb |
+3
-3
@@ -4,11 +4,11 @@
|
|||||||
einem Deployment vereint hat. Die Zugangscode-Schranke (middleware/gate.js) läuft vor JEDER
|
einem Deployment vereint hat. Die Zugangscode-Schranke (middleware/gate.js) läuft vor JEDER
|
||||||
Anfrage, exakt wie functions/_middleware.js das vorher bei Cloudflare tat.
|
Anfrage, exakt wie functions/_middleware.js das vorher bei Cloudflare tat.
|
||||||
|
|
||||||
Umgebungsvariablen kommen aus einer .env-Datei — gestartet wird dieser Server mit
|
Umgebungsvariablen kommen aus einer .env-Datei im server/-Ordner (siehe .env.example), geladen
|
||||||
"node --env-file=.env index.js" (Node 22 kann .env-Dateien nativ laden, kein zusätzliches
|
über das "dotenv"-Paket direkt beim Start.
|
||||||
Paket nötig).
|
|
||||||
===================================================================== */
|
===================================================================== */
|
||||||
|
|
||||||
|
import "dotenv/config";
|
||||||
import express from "express";
|
import express from "express";
|
||||||
import cookieParser from "cookie-parser";
|
import cookieParser from "cookie-parser";
|
||||||
import { dirname, join } from "node:path";
|
import { dirname, join } from "node:path";
|
||||||
|
|||||||
+10
-2
@@ -9,7 +9,15 @@
|
|||||||
|
|
||||||
export const COOKIE_NAME = "vandiy_gate_session";
|
export const COOKIE_NAME = "vandiy_gate_session";
|
||||||
export const ROLE_COOKIE_NAME = "vandiy_gate_role";
|
export const ROLE_COOKIE_NAME = "vandiy_gate_role";
|
||||||
export const SESSION_TAGE = 90;
|
|
||||||
|
/* Zugang gilt nur für die laufende Browser-Sitzung (Wunsch 05.08.2026: "jedes Mal wenn wir den
|
||||||
|
Browser zu machen, soll der Code wieder gefragt werden"). Umgesetzt über ein reines
|
||||||
|
Sitzungs-Cookie OHNE maxAge/expires in middleware/gate.js — das löscht der Browser beim
|
||||||
|
Schließen selbst. SESSION_STUNDEN ist nur die zusätzliche Notbremse im signierten Token für
|
||||||
|
den Fall, dass ein Browser tage-/wochenlang offen bleibt (dann läuft der Zugang trotzdem ab).
|
||||||
|
Betrifft NUR die Zugangsschranke — Kundenkonten (customer-auth.js, 30 Tage) bleiben unberührt,
|
||||||
|
echte Kund:innen sollen sich nicht bei jedem Besuch neu anmelden müssen. */
|
||||||
|
export const SESSION_STUNDEN = 12;
|
||||||
|
|
||||||
function b64urlEncode(bytes) {
|
function b64urlEncode(bytes) {
|
||||||
let bin = "";
|
let bin = "";
|
||||||
@@ -34,7 +42,7 @@ async function hmacKey(secret) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function signSession(role, secret) {
|
export async function signSession(role, secret) {
|
||||||
const payload = JSON.stringify({ role, exp: Date.now() + SESSION_TAGE * 24 * 60 * 60 * 1000 });
|
const payload = JSON.stringify({ role, exp: Date.now() + SESSION_STUNDEN * 60 * 60 * 1000 });
|
||||||
const payloadB64 = b64urlEncode(new TextEncoder().encode(payload));
|
const payloadB64 = b64urlEncode(new TextEncoder().encode(payload));
|
||||||
const key = await hmacKey(secret);
|
const key = await hmacKey(secret);
|
||||||
const sig = await crypto.subtle.sign("HMAC", key, new TextEncoder().encode(payloadB64));
|
const sig = await crypto.subtle.sign("HMAC", key, new TextEncoder().encode(payloadB64));
|
||||||
|
|||||||
@@ -12,10 +12,9 @@
|
|||||||
- API-Routen bekommen bei fehlender Sitzung 401-JSON statt HTML-Redirect.
|
- API-Routen bekommen bei fehlender Sitzung 401-JSON statt HTML-Redirect.
|
||||||
===================================================================== */
|
===================================================================== */
|
||||||
|
|
||||||
import { COOKIE_NAME, ROLE_COOKIE_NAME, SESSION_TAGE, signSession, verifySession } from "../lib/auth.js";
|
import { COOKIE_NAME, ROLE_COOKIE_NAME, signSession, verifySession } from "../lib/auth.js";
|
||||||
import {
|
import {
|
||||||
VERWALTUNG_COOKIE_NAME,
|
VERWALTUNG_COOKIE_NAME,
|
||||||
VERWALTUNG_SESSION_STUNDEN,
|
|
||||||
signVerwaltungSession,
|
signVerwaltungSession,
|
||||||
verifyVerwaltungSession,
|
verifyVerwaltungSession,
|
||||||
} from "../lib/verwaltung-auth.js";
|
} from "../lib/verwaltung-auth.js";
|
||||||
@@ -44,9 +43,9 @@ export async function gateAuthHandler(req, res) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const session = await signSession(role, process.env.SITE_ACCESS_SECRET);
|
const session = await signSession(role, process.env.SITE_ACCESS_SECRET);
|
||||||
const maxAge = SESSION_TAGE * 24 * 60 * 60 * 1000;
|
// Bewusst OHNE maxAge/expires → reines Sitzungs-Cookie, verfällt beim Schließen des Browsers.
|
||||||
res.cookie(COOKIE_NAME, session, { path: "/", maxAge, httpOnly: true, secure: true, sameSite: "lax" });
|
res.cookie(COOKIE_NAME, session, { path: "/", httpOnly: true, secure: true, sameSite: "lax" });
|
||||||
res.cookie(ROLE_COOKIE_NAME, role, { path: "/", maxAge, secure: true, sameSite: "lax" });
|
res.cookie(ROLE_COOKIE_NAME, role, { path: "/", secure: true, sameSite: "lax" });
|
||||||
return res.json({ ok: true, next });
|
return res.json({ ok: true, next });
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,8 +61,8 @@ export async function verwaltungAuthHandler(req, res) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const session = await signVerwaltungSession(process.env.VERWALTUNG_SESSION_SECRET);
|
const session = await signVerwaltungSession(process.env.VERWALTUNG_SESSION_SECRET);
|
||||||
const maxAge = VERWALTUNG_SESSION_STUNDEN * 60 * 60 * 1000;
|
// Ebenfalls reines Sitzungs-Cookie (siehe gateAuthHandler oben).
|
||||||
res.cookie(VERWALTUNG_COOKIE_NAME, session, { path: "/", maxAge, httpOnly: true, secure: true, sameSite: "lax" });
|
res.cookie(VERWALTUNG_COOKIE_NAME, session, { path: "/", httpOnly: true, secure: true, sameSite: "lax" });
|
||||||
return res.json({ ok: true, next });
|
return res.json({ ok: true, next });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-1
@@ -4,7 +4,7 @@
|
|||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "Eigener Node.js/Express-Server für Van's DIY & Bastelbedarf – Ersatz für Cloudflare Pages Functions + D1, läuft auf dem eigenen Netcup-Server. Portiert 1:1 aus functions/ (Cloudflare Pages Functions), gleiche Sicherheitslogik, gleiche Datenbankstruktur (SQLite statt D1).",
|
"description": "Eigener Node.js/Express-Server für Van's DIY & Bastelbedarf – Ersatz für Cloudflare Pages Functions + D1, läuft auf dem eigenen Netcup-Server. Portiert 1:1 aus functions/ (Cloudflare Pages Functions), gleiche Sicherheitslogik, gleiche Datenbankstruktur (SQLite statt D1).",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=22.12.0"
|
"node": ">=20.0.0"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node index.js"
|
"start": "node index.js"
|
||||||
@@ -12,6 +12,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"better-sqlite3": "^11.8.1",
|
"better-sqlite3": "^11.8.1",
|
||||||
"cookie-parser": "^1.4.7",
|
"cookie-parser": "^1.4.7",
|
||||||
|
"dotenv": "^16.4.7",
|
||||||
"express": "^4.21.2"
|
"express": "^4.21.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user