feat: implement local KeyForge MVP with verified integration

This commit is contained in:
2026-11-16 06:45:41 +01:00
parent 95369a3725
commit 4337f1d801
47 changed files with 6429 additions and 41 deletions

View File

@ -0,0 +1,219 @@
//! Zweck: Verwaltungsgrenzen und vollständigen Tokenweg ohne TCP prüfen.
//! Wichtige Information: Sitzungen sind isolierte Testdaten; kein Produktionszugang.
//! Version: 1
use axum::{
Router,
body::Body,
http::{Request, StatusCode},
};
use http_body_util::BodyExt;
use keyforge_kern::Speicher;
use keyforge_verwaltung::{Anmeldung, web::router};
use std::sync::{Arc, Mutex};
use tower::ServiceExt;
fn vorbereitung() -> (tempfile::TempDir, Router, Arc<Mutex<Speicher>>) {
let ordner = tempfile::tempdir().unwrap();
let pfad = ordner.path().join("db");
let mut kern = Speicher::oeffnen(&pfad).unwrap();
kern.dienst_anlegen(
"beispiel",
"<script>Beispiel</script>",
&["lesen".into()],
"2026-11-15T00:00:00Z",
)
.unwrap();
kern.instanz_anlegen(
"instanz",
"beispiel",
"Lokal",
&"1".repeat(64),
"2026-11-15T00:00:00Z",
)
.unwrap();
Anmeldung::oeffnen(&pfad).unwrap();
let db = rusqlite::Connection::open(&pfad).unwrap();
db.execute(
"INSERT INTO sitzung(hash,csrf,ablauf) VALUES (?1,'csrf-test',?2)",
rusqlite::params![
keyforge_protokoll::sha256_hex("sitzung-test"),
chrono::Utc::now().timestamp() + 3600
],
)
.unwrap();
let kern = Arc::new(Mutex::new(kern));
let app = router(pfad, kern.clone());
(ordner, app, kern)
}
async fn anfrage(
app: Router,
pfad: &str,
formular: Option<String>,
sitzung: bool,
) -> axum::response::Response {
let mut anfrage = Request::builder().uri(pfad);
if sitzung {
anfrage = anfrage.header("cookie", "keyforge_sitzung=sitzung-test");
}
let body = if let Some(f) = formular {
anfrage = anfrage
.method("POST")
.header("content-type", "application/x-www-form-urlencoded");
Body::from(f)
} else {
Body::empty()
};
app.oneshot(anfrage.body(body).unwrap()).await.unwrap()
}
async fn text(antwort: axum::response::Response) -> String {
String::from_utf8(
antwort
.into_body()
.collect()
.await
.unwrap()
.to_bytes()
.to_vec(),
)
.unwrap()
}
#[tokio::test]
async fn sitzung_csrf_maskierung_und_tokenrotation() {
let (_ordner, app, kern) = vorbereitung();
assert_eq!(
anfrage(app.clone(), "/", None, false).await.status(),
StatusCode::SEE_OTHER
);
assert_eq!(
anfrage(
app.clone(),
"/aktion",
Some("art=token&csrf=falsch&id=beispiel".into()),
true
)
.await
.status(),
StatusCode::FORBIDDEN
);
let start = text(anfrage(app.clone(), "/", None, true).await).await;
assert!(start.contains("&lt;script&gt;Beispiel&lt;/script&gt;"));
assert!(!start.contains("<script>Beispiel"));
let antwort = anfrage(app.clone(),"/aktion",Some("art=token&csrf=csrf-test&id=beispiel&bezeichnung=Rechner&rechte=lesen&ablauf=2099-01-01T00%3A00%3A00Z".into()),true).await;
assert_eq!(antwort.status(), StatusCode::OK);
assert_eq!(antwort.headers()["cache-control"], "no-store");
let inhalt = text(antwort).await;
let token = inhalt
.split("<code>")
.nth(1)
.unwrap()
.split("</code>")
.next()
.unwrap();
let token_id = keyforge_protokoll::sha256_hex(token);
let start = text(anfrage(app.clone(), "/", None, true).await).await;
assert!(!start.contains(token));
assert!(start.contains("Zustellung ausstehend"));
assert!(start.contains("2099-01-01"));
assert!(start.contains("Ereignisse"));
let antwort = anfrage(
app.clone(),
"/aktion",
Some(format!("art=rotieren&csrf=csrf-test&id={token_id}")),
true,
)
.await;
assert_eq!(antwort.status(), StatusCode::OK);
let liste = kern
.lock()
.unwrap()
.pruefliste("instanz", "2026-11-15T00:00:00Z")
.unwrap();
let tokens = liste.tokens.unwrap();
assert_eq!(tokens.len(), 1);
assert_ne!(tokens[0].hash, token_id);
let antwort = anfrage(
app.clone(),
"/aktion",
Some("art=abmelden&csrf=csrf-test".into()),
true,
)
.await;
assert_eq!(antwort.status(), StatusCode::SEE_OTHER);
assert_eq!(
anfrage(app, "/", None, true).await.status(),
StatusCode::SEE_OTHER
);
}
#[tokio::test]
async fn defektes_schema_wird_sichtbar_und_datum_validiert() {
let (ordner, app, _) = vorbereitung();
let antwort = anfrage(
app.clone(),
"/aktion",
Some(
"art=token&csrf=csrf-test&id=beispiel&bezeichnung=Rechner&rechte=lesen&ablauf=gestern"
.into(),
),
true,
)
.await;
assert_eq!(antwort.status(), StatusCode::BAD_REQUEST);
let db = rusqlite::Connection::open(ordner.path().join("db")).unwrap();
db.execute("DROP TABLE ereignis", []).unwrap();
assert_eq!(
anfrage(app, "/", None, true).await.status(),
StatusCode::SERVICE_UNAVAILABLE
);
}
#[tokio::test]
async fn tls_anmeldung_setzt_sicheres_cookie() {
let (ordner, _, kern) = vorbereitung();
let pfad = ordner.path().join("db");
let geheim = "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ";
let mut anmeldung = Anmeldung::oeffnen(&pfad).unwrap();
anmeldung
.konto_anlegen("admin", "langes Testpasswort", geheim)
.unwrap();
let code =
keyforge_verwaltung::totp(geheim, (chrono::Utc::now().timestamp() / 30) as u64).unwrap();
let app = keyforge_verwaltung::web::router_mit_tls(pfad, kern, true);
let antwort = anfrage(
app,
"/anmelden",
Some(format!(
"name=admin&passwort=langes+Testpasswort&code={code}"
)),
false,
)
.await;
assert_eq!(antwort.status(), StatusCode::SEE_OTHER);
let cookie = antwort.headers()["set-cookie"].to_str().unwrap();
assert!(cookie.contains("; Secure"));
assert!(cookie.contains("; HttpOnly"));
assert!(cookie.contains("; SameSite=Strict"));
}
#[tokio::test]
async fn offene_uebernahme_sichtbar_und_ablehnbar() {
let (ordner, app, _) = vorbereitung();
let db = rusqlite::Connection::open(ordner.path().join("db")).unwrap();
db.execute("INSERT INTO uebernahme VALUES ('uebernahme-test','beispiel','instanz','[]','beantragt',0,9999999999)",[]).unwrap();
let start = text(anfrage(app.clone(), "/", None, true).await).await;
assert!(start.contains("uebernahme-test"));
assert!(start.contains("Übernahme ablehnen"));
let antwort = anfrage(
app,
"/aktion",
Some("art=uebernahme&csrf=csrf-test&id=uebernahme-test&entscheidung=ablehnen".into()),
true,
)
.await;
assert_eq!(antwort.status(), StatusCode::SEE_OTHER);
let status: String = db
.query_row(
"SELECT status FROM uebernahme WHERE id='uebernahme-test'",
[],
|r| r.get(0),
)
.unwrap();
assert_eq!(status, "abgelehnt");
}