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
+109
View File
@@ -0,0 +1,109 @@
"""The optional Packet Station PIN: Argon2id hash on the server, set by the user, cleared by an Admin."""
from __future__ import annotations
from dataclasses import dataclass
from typing import Any
from argon2 import PasswordHasher
from argon2.exceptions import InvalidHashError, VerificationError, VerifyMismatchError
from .. import audit
from ..settings import Settings
_hasher = PasswordHasher()
@dataclass
class PinState:
set: bool
disabled: bool
failed_count: int = 0
def public(self) -> dict[str, bool]:
return {"set": self.set, "disabled": self.disabled}
def valid_format(pin: str, settings: Settings) -> bool:
"""Digits only (ASCII), PIN_MIN_DIGITS to PIN_MAX_DIGITS long."""
return pin.isascii() and pin.isdigit() and settings.pin_min_digits <= len(pin) <= settings.pin_max_digits
def hash_pin(pin: str) -> str:
return _hasher.hash(pin)
def verify_hash(stored: str, pin: str) -> bool:
if not pin:
return False
try:
return _hasher.verify(stored, pin)
except (VerifyMismatchError, VerificationError, InvalidHashError):
return False
def state(conn: Any, user_id: Any) -> PinState:
row = conn.execute("SELECT failed_count, disabled_at FROM user_pins WHERE user_id = %s", (user_id,)).fetchone()
if row is None:
return PinState(False, False)
return PinState(True, row[1] is not None, int(row[0]))
def stored_hash(conn: Any, user_id: Any) -> tuple[str, bool] | None:
"""(hash, disabled) or None when the user has no PIN."""
row = conn.execute("SELECT hash, disabled_at FROM user_pins WHERE user_id = %s", (user_id,)).fetchone()
return None if row is None else (str(row[0]), row[1] is not None)
def set_pin(conn: Any, user_id: Any, pin: str) -> None:
"""Store a new PIN hash and clear the failure count and the disabled mark."""
h = hash_pin(pin)
conn.execute(
"INSERT INTO user_pins (user_id, hash, failed_count, disabled_at, set_at) VALUES (%s, %s, 0, NULL, now())"
" ON CONFLICT (user_id) DO UPDATE SET hash = EXCLUDED.hash, failed_count = 0, disabled_at = NULL,"
" set_at = now()",
(user_id, h),
)
def reset_failures(conn: Any, user_id: Any) -> None:
conn.execute("UPDATE user_pins SET failed_count = 0 WHERE user_id = %s AND failed_count <> 0", (user_id,))
def register_failure(
conn: Any,
settings: Settings,
user_id: Any,
*,
session_id: Any = None,
role_snapshot: str | None = None,
ip: str | None = None,
where: str = "signin",
) -> bool:
"""Count one wrong PIN. At PIN_MAX_FAILURES the PIN is disabled and `pin.disabled` audited. True if disabled now."""
with conn.transaction():
row = conn.execute(
"UPDATE user_pins SET failed_count = failed_count + 1 WHERE user_id = %s AND disabled_at IS NULL"
" RETURNING failed_count",
(user_id,),
).fetchone()
if row is None or int(row[0]) < settings.pin_max_failures:
return False
conn.execute("UPDATE user_pins SET disabled_at = now() WHERE user_id = %s", (user_id,))
audit.append(
conn,
action="pin.disabled",
user_id=user_id,
session_id=session_id,
role_snapshot=role_snapshot,
subject_type="user",
subject_id=user_id,
detail={"failures": int(row[0]), "where": where},
ip=ip,
)
return True
def clear(conn: Any, user_id: Any) -> bool:
row = conn.execute("DELETE FROM user_pins WHERE user_id = %s RETURNING user_id", (user_id,)).fetchone()
return row is not None