Skip to content

HUD login: PIN required for control, viewing stays open - #56

Draft
KoganTheDev wants to merge 7 commits into
mainfrom
feature/hud-login
Draft

KoganTheDev wants to merge 7 commits into
mainfrom
feature/hud-login

Conversation

@KoganTheDev

Copy link
Copy Markdown
Owner

Tech debt item #8. Stacked on #55 (split HUD sources); I'll retarget it to main once #55 merges. Draft until it has been tested on the board.

Why

Notes s9: any device on the LAN could drive the turret. The Origin check only stops cross-site pages, not other clients on the LAN.

Decisions (made by the owner)

  • Only control needs a login: anyone can still watch the video and status.
  • The login uses the existing WiFi-setup PIN, through the same PinGuard, so the WiFi page and the HUD share one lockout.
  • A session lasts until reboot (RAM only), and is cleared when the PIN is set or on wifi-reset.
  • The hotspot follows the same rules.

How it works

  • POST /api/hud/login (form field pin; same Host, Origin, body-size and deadline rules as the WiFi POSTs):
    • right PIN: {"ok":true} and Set-Cookie: vs_session=<128-bit hex>; HttpOnly; SameSite=Strict; Path=/;
    • wrong PIN: 403; locked out: 429 with locked_s; no PIN set yet: 400.
  • POST /api/hud/logout removes the caller's session and clears the cookie.
  • Sessions: prov::SessionTable (pure, natively tested) holds 4 sessions with LRU eviction, and the token comparison takes the same time however much of it is right.
  • /commands: the handshake stores the session token in the httpd session context. Every message is then checked against WifiManager::auth_level(), so a logout or PIN change takes effect at once. remote::gate turns a viewer's control commands into a Poll, which still gets exactly one reply. Every command needs a login except Poll, including move:stop.
  • Reply: new last key auth: 0 view only, 1 logged in, 2 no PIN (control open). Worst-case reply is 750 of 832 B.
  • HUD:
    • an ACCESS chip with a lock icon, and controls greyed out with a "Log in to control" prompt;
    • a PIN dialog that shows wrong-PIN and lockout messages, and log out;
    • a dismissible hint when no PIN is set;
    • control keys are ignored for viewers.
  • Unchanged: the joystick and button are never gated, and the single-poll-loop rule holds.
  • Mock and end-to-end: the mock implements the same auth (--no-pin). New checks cover view only, a wrong PIN, login then jog, and logout locking control again.

Verification

Check Result
native tests 399 -> 422
firmware build OK, RAM 19.6%, Flash 59.8%
cppcheck 5/5 envs
clang-format 19 clean
web / mock tests 43 / 74 OK
end-to-end 48/48 on 3 runs
on the board pending

Known limit

The board has no HTTPS, so anyone who can sniff the LAN can capture the PIN or the cookie. This is documented in notes s9.

…and the viewer gate

prov::SessionTable keeps up to 4 random 128-bit login tokens in RAM (LRU eviction, constant-time
lookup), prov::session_cookie() reads vs_session=<32 hex> from a Cookie header, and
remote::requires_auth()/gate() turn every control command from a viewer into a Poll. AuthLevel
(viewer, authed, open when no PIN is set) is shared through ESP32/include/auth_level.h.
Natively tested (new test_hud_session suite, test_remote).
POST /api/hud/login checks the WiFi PIN through WifiManager's PinGuard (lockouts shared with the
WiFi API) and sets vs_session=<128-bit random token>; HttpOnly; SameSite=Strict; Path=/.
POST /api/hud/logout ends that session. Both get the same IPv4-Host, origin, size and deadline
rules as the WiFi POSTs and are registered before the catch-all.

/commands keeps a live login token in the httpd session context at the handshake and asks
WifiManager::auth_level() on every message (atomic + spinlock, never the manager mutex), so a
logout or a new PIN takes control away from an open socket at once. A viewer's control commands
reach the controller as a Poll (remote::gate); every message still gets its one reply, which now
ends with "auth" (0 view only, 1 logged in, 2 no PIN set, control open). Sessions live in RAM,
so a reboot ends them; setting the PIN and wifi-reset clear them. The joystick is unaffected.

The mock board sends the new key and applies the same viewer gate (reply_keys.json contract).
An ACCESS chip (lock icon) shows VIEW ONLY, CONTROL or OPEN from the reply's auth key. While view
only, the d-pad, mode segment, calibration, tap-to-aim, speed, FIRE and auto-fire are disabled
with a "Log in" callout, and control keys only toast a login hint. The login dialog (numeric PIN,
autocomplete off, Esc closes) POSTs /api/hud/login, shows wrong-PIN and lockout answers and on
success reconnects /commands (App.restart, which also serves the stall watchdog and detaches the
old socket's handlers so only one poll loop can run). The CONTROL chip offers log out. With no
PIN set a dismissible hint points to the WiFi page. Login and logout are fetch() POSTs, never
/commands messages.
The mock board now mirrors the firmware's control access: PIN 1234 through a PinGuard model shared
by the WiFi API and POST /api/hud/login|logout (5th wrong PIN locks out, escalating), RAM-only
session tokens (max 4, LRU) in an HttpOnly vs_session cookie, the cookie read at the /commands
handshake and re-checked per message. --no-pin starts with no PIN (auth 2) and the first WiFi add
sets one, ending every session, like the board. Unit tests cover the guard, cookie parsing, login,
eviction and the per-socket behaviour (login needs a new socket, logout cuts an open one).

hud_e2e.py adds an access scenario (viewer sees video and status with controls locked, M refused,
wrong PIN error, Esc closes, right PIN unlocks and jogs, log out locks again) and a --no-pin
scenario; the existing control scenarios log in through the HUD first. 48 checks on 10 mocks.
CLAUDE.md: the login routes, the per-message session check on /commands, the auth reply key,
sessions owned by WifiManager and cleared on a new PIN or wifi-reset, App.restart for socket
replacement. Notes s9: why the existing PIN and a cookie, why control only, why the check is per
message, and what remains (plain HTTP: a LAN sniffer can capture the PIN or the cookie). README:
how to log in. The HUD spec's reply table gains auth.
The click variant failed once in five full runs under the e2e's load (10 mocks); a key reaches the
same pressDir guard without depending on where the d-pad sits after the login callout appears.
The check now reports the toasts, link and access state when it fails.
@github-actions github-actions Bot added documentation Improvements or additions to documentation Tests Create new Tests/Rebuild current tests and check if all of them pass Network Tools Web labels Oct 30, 2026
@KoganTheDev
KoganTheDev changed the base branch from refactor/split-hud-page to main October 31, 2026 05:40
…d on Linux

Headless Chrome on Linux does not give the page focus, so element.focus() did
nothing and Input.insertText typed into nothing: the PIN was submitted empty,
the login failed in CI, and the wrong-PIN check passed for the wrong reason.
Enable Emulation.setFocusEmulationEnabled and fail if the typed PIN is not in
the field.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation Network Tests Create new Tests/Rebuild current tests and check if all of them pass Tools Web

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant