Files
PacketStation/server/packetstation/auth/stepup.py
T
spencermandClaude Fable 5.1 97b0973130 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]>
2026-10-16 07:40:29 -06:00

137 lines
4.7 KiB
Python

"""Step-up checks inside a session (screen unlock and signatures): PIN or domain password.
Wrong attempts are recorded (account `user:<uuid>`), count toward the PIN's failures when the factor is the
PIN, and raise `sessions.stepup_failures`. At STEPUP_MAX_FAILURES the session ends. A right answer resets
the session's count.
"""
from __future__ import annotations
from typing import Any
from .. import audit
from ..api.errors import ApiError
from ..settings import Settings
from . import pin as pins
from . import ratelimit, sessions
from .ldap import DirectoryProtocol, LdapUnavailable
from .signin import ldap_down, rate_gate
FACTORS = ("pin", "password")
def end_session(conn: Any, actor: sessions.Actor, reason: str, ip: str | None) -> None:
with conn.transaction():
if sessions.revoke(conn, actor.session_id, reason):
audit.append(
conn,
action="auth.session_ended",
user_id=actor.user_id,
session_id=actor.session_id,
role_snapshot=actor.role_snapshot,
detail={"reason": reason},
ip=ip,
)
def verify(
conn: Any,
directory: DirectoryProtocol,
settings: Settings,
actor: sessions.Actor,
*,
factor: str,
secret: str,
kind: str,
ip: str | None,
) -> None:
"""Return when the secret is right; raise ApiError otherwise.
Wrong: 403 `wrong_secret` with `attempts_left`. The last allowed miss: 401 `session_ended`.
"""
if factor not in FACTORS:
raise ApiError(400, "bad_factor", "factor must be pin or password")
if kind not in ("unlock", "signature"):
raise ValueError(f"bad step-up kind {kind!r}")
account = f"user:{actor.user_id}"
secret = secret or ""
if factor == "pin":
stored = pins.stored_hash(conn, actor.user_id)
if stored is None:
raise ApiError(409, "pin_not_set", "No PIN is set. Use your password.")
if stored[1]:
raise ApiError(409, "pin_disabled", "Your PIN is disabled. Use your password.")
ok = pins.verify_hash(stored[0], secret)
else:
rate_gate(
conn,
settings,
accounts=[account, actor.sam_account.lower()],
ip=ip,
kind=kind,
record_account=account,
user_id=actor.user_id,
session_id=actor.session_id,
)
try:
u = directory.get_user_by_guid(actor.user_id)
if u is None or u.disabled:
end_session(conn, actor, "roles_lost", ip)
raise ApiError(401, "session_ended", "Your account is no longer active. Sign in again.")
ok = bool(secret) and directory.verify_password(u.dn, secret)
except LdapUnavailable as e:
raise ldap_down(e) from None
if ok:
with conn.transaction():
conn.execute(
"UPDATE sessions SET stepup_failures = 0 WHERE id = %s AND stepup_failures <> 0", (actor.session_id,)
)
if factor == "pin":
pins.reset_failures(conn, actor.user_id)
ratelimit.record(
conn, account=account, ip=ip, kind=kind, ok=True, user_id=actor.user_id, session_id=actor.session_id
)
return
pin_disabled = False
with conn.transaction():
ratelimit.record(
conn,
account=account,
ip=ip,
kind=kind,
ok=False,
reason="bad_pin" if factor == "pin" else "bad_password",
user_id=actor.user_id,
session_id=actor.session_id,
)
if factor == "pin":
pin_disabled = pins.register_failure(
conn,
settings,
actor.user_id,
session_id=actor.session_id,
role_snapshot=actor.role_snapshot,
ip=ip,
where=kind,
)
row = conn.execute(
"UPDATE sessions SET stepup_failures = stepup_failures + 1 WHERE id = %s RETURNING stepup_failures",
(actor.session_id,),
).fetchone()
failures = int(row[0]) if row else settings.stepup_max_failures
if failures >= settings.stepup_max_failures:
end_session(conn, actor, "stepup_failures", ip)
raise ApiError(
401,
"session_ended",
f"{settings.stepup_max_failures} wrong attempts. The session has ended. Sign in again.",
)
raise ApiError(
403,
"wrong_secret",
"Wrong PIN." if factor == "pin" else "Wrong password.",
extra={"attempts_left": settings.stepup_max_failures - failures, "pin_disabled": pin_disabled},
)