Phase 1: identity, sessions, audit, frontend shell

AD sign-in over LDAPS (search with the service account, bind as the user,
nested groups through LDAP_MATCHING_RULE_IN_CHAIN, disabled accounts refused),
roles as the union of PS-* groups mapped by a table derived from the seeded
departments. Server-side sessions with idle, lock and absolute timeouts, Origin
and X-Requested-With check, step-up unlock. Optional Argon2id PIN set with the
domain password; PIN sign-in by employee ID on registered devices only; five
wrong attempts disable the PIN, three wrong at unlock or signature end the
session. Rate limits per account and per address before any bind. Devices,
signatures table and sign helper, audit rows for every identity event, SSE over
LISTEN/NOTIFY with presence. Account settings and Admin APIs and screens
(AD view, devices, PIN clearing, group map with missing-group flags, refresh
from AD now). CLI ad-groups --list and ad-refresh. Migration 0002.

Frontend: real sign-in (password tab, PIN keypad on registered devices), lock
screen, role switcher, Account settings, Admin shell, screen placeholders.

CI: Samba AD DC container (PSTEST.LOCAL) with a generated CA, 49 of the 50
groups plus one extra, 17 test users; wired into the CI compose overlay and
the Gitea workflow; Playwright sign-in scenario.

Verified on docker-1: 171 server tests green inside the CI stack against
PostgreSQL and the Samba DC, 48 frontend tests, 8 Playwright scenarios against
the live stack (password and PIN sign-in, lock and unlock, Admin group map).
Findings recorded in the docs: the DC needs CAP_SYS_ADMIN, a hidden tab locks
only after a minute as the old build did,
and the app uses the alias ps-app for Playwright because "app" is on
Chromium's HSTS preload list.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
This commit is contained in:
2026-10-16 07:40:29 -06:00
co-authored by Claude Fable 5.1
parent 0b663bb2b2
commit 97b0973130
95 changed files with 9609 additions and 183 deletions
+90
View File
@@ -0,0 +1,90 @@
from __future__ import annotations
from pathlib import Path
from typing import Any
import pytest
from fakedir import FakeDirectory
from fastapi.testclient import TestClient
from pshelpers import ORIGIN_HEADERS, make_settings
from packetstation.api.origin import allowed
from packetstation.main import create_app
def scope(method: str = "POST", path: str = "/api/auth/lock", scheme: str = "https", **headers: str) -> dict[str, Any]:
return {
"type": "http",
"method": method,
"path": path,
"scheme": scheme,
"headers": [(k.replace("_", "-").encode(), v.encode()) for k, v in headers.items()],
}
def test_allowed_rules() -> None:
good = {"host": "ps.pesco.com", "origin": "https://ps.pesco.com", "x_requested_with": "PacketStation"}
assert allowed(scope(**good))
assert allowed(scope(method="GET", host="ps.pesco.com")) # safe methods pass
assert allowed(scope(path="/healthz", host="x")) # outside /api
assert not allowed(scope(**{**good, "origin": "https://evil.example"}))
assert not allowed(scope(**{**good, "origin": "http://ps.pesco.com"})) # scheme must match
assert not allowed(scope(host="ps.pesco.com", origin="https://ps.pesco.com")) # marker missing
assert not allowed(scope(**{**good, "x_requested_with": "XMLHttpRequest"}))
no_origin = {"host": "ps.pesco.com", "x_requested_with": "PacketStation"}
assert not allowed(scope(**no_origin))
assert allowed(scope(**no_origin, referer="https://ps.pesco.com/account"))
assert not allowed(scope(**no_origin, referer="https://ps.pesco.com.evil.example/"))
for method in ("PUT", "PATCH", "DELETE"):
assert not allowed(scope(method=method, host="ps.pesco.com"))
@pytest.fixture
def client(tmp_path: Path, dev_visual: None) -> TestClient:
return TestClient(create_app(make_settings(tmp_path), directory=FakeDirectory()))
def test_wrong_origin_refused_on_signin(client: TestClient) -> None:
body = {"login": "clerk.clara", "password": "x"}
r = client.post("/api/auth/signin/password", json=body, headers={**ORIGIN_HEADERS, "Origin": "http://evil.example"})
assert r.status_code == 403
assert r.json() == {"detail": "Cross-origin request refused", "code": "bad_origin"}
assert r.headers["x-frame-options"] == "DENY"
r = client.post("/api/auth/signin/password", json=body, headers={"Origin": "http://testserver"})
assert r.status_code == 403 and r.json()["code"] == "bad_origin"
def test_right_origin_passes_the_check(client: TestClient) -> None:
# No database here, so the request gets past the Origin check and stops at the database
r = client.post("/api/auth/signin/password", json={"login": "a", "password": "b"}, headers=ORIGIN_HEADERS)
assert r.status_code == 503
assert r.json()["code"] == "database_unavailable"
def test_me_without_cookie_is_no_session(client: TestClient) -> None:
r = client.get("/api/auth/me")
assert r.status_code == 401
assert r.json()["code"] == "no_session"
assert client.get("/api/events").json()["code"] == "no_session"
assert client.get("/api/account").status_code == 401
def test_context_without_device(client: TestClient) -> None:
r = client.get("/api/auth/context")
assert r.status_code == 200
assert r.json() == {"device": None, "stack": "ci", "insecure_ldap": False, "pin_digits": {"min": 6, "max": 12}}
def test_validation_error_carries_a_code(client: TestClient) -> None:
r = client.post("/api/auth/signin/password", json={"login": "x"}, headers=ORIGIN_HEADERS)
assert r.status_code == 422
assert r.json()["code"] == "invalid_request"
def test_missing_session_key_fails_cleanly(tmp_path: Path, dev_visual: None) -> None:
s = make_settings(tmp_path, session_key_file=tmp_path / "missing")
c = TestClient(create_app(s, directory=FakeDirectory()))
r = c.post("/api/auth/signin/password", json={"login": "a", "password": "b"}, headers=ORIGIN_HEADERS)
assert r.status_code == 503
assert r.json()["code"] == "server_misconfigured"
assert str(tmp_path) not in r.json()["detail"]