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:
@@ -0,0 +1,63 @@
|
||||
"""Registered devices (shop tablets). Only a registered device may offer PIN sign-in."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
from typing import Any
|
||||
|
||||
from psycopg.rows import dict_row
|
||||
|
||||
from .sessions import new_token, token_hash
|
||||
|
||||
NAME_MAX = 80
|
||||
|
||||
|
||||
def register(conn: Any, key: bytes, *, name: str, user_id: Any) -> tuple[str, uuid.UUID]:
|
||||
"""Insert a device; returns (cookie token, device id)."""
|
||||
token = new_token()
|
||||
row = conn.execute(
|
||||
"INSERT INTO devices (name, token_hash, registered_by, registered_at, last_seen_at)"
|
||||
" VALUES (%s, %s, %s, now(), now()) RETURNING id",
|
||||
(name, token_hash(key, token), user_id),
|
||||
).fetchone()
|
||||
return token, row[0]
|
||||
|
||||
|
||||
def find(conn: Any, key: bytes, token: str | None) -> dict[str, Any] | None:
|
||||
"""The unrevoked device for a cookie token, or None."""
|
||||
if not token:
|
||||
return None
|
||||
with conn.cursor(row_factory=dict_row) as cur:
|
||||
row: dict[str, Any] | None = cur.execute(
|
||||
"SELECT id, name, registered_at, last_seen_at FROM devices WHERE token_hash = %s AND revoked_at IS NULL",
|
||||
(token_hash(key, token),),
|
||||
).fetchone()
|
||||
return row
|
||||
|
||||
|
||||
def touch(conn: Any, device_id: Any) -> None:
|
||||
conn.execute(
|
||||
"UPDATE devices SET last_seen_at = now() WHERE id = %s"
|
||||
" AND (last_seen_at IS NULL OR last_seen_at < now() - interval '1 minute')",
|
||||
(device_id,),
|
||||
)
|
||||
|
||||
|
||||
def get(conn: Any, device_id: Any) -> dict[str, Any] | None:
|
||||
with conn.cursor(row_factory=dict_row) as cur:
|
||||
row: dict[str, Any] | None = cur.execute(
|
||||
"SELECT id, name, revoked_at FROM devices WHERE id = %s", (device_id,)
|
||||
).fetchone()
|
||||
return row
|
||||
|
||||
|
||||
def revoke(conn: Any, device_id: Any, by: Any) -> bool:
|
||||
row = conn.execute(
|
||||
"UPDATE devices SET revoked_at = now(), revoked_by = %s WHERE id = %s AND revoked_at IS NULL RETURNING id",
|
||||
(by, device_id),
|
||||
).fetchone()
|
||||
return row is not None
|
||||
|
||||
|
||||
def public(row: dict[str, Any] | None) -> dict[str, Any] | None:
|
||||
return None if row is None else {"id": str(row["id"]), "name": row["name"]}
|
||||
Reference in New Issue
Block a user