feat(backend): link cards to users; charger-qualified gun restrictions; config-driven gun labels

- Allowlist entries take "user": <username> — sessions started with that
  card attribute to the person, and the owner sees their card's history
  even without gun-wide permissions (ownership looked up live)
- "guns": ["eu092025:2"] (fnmatch) replaces the charger-blind
  "connectors": [2] restriction — safe with multiple chargers
- New "chargers" config section provides per-charger/per-gun display
  names; the panel no longer hardcodes the Anfu gun labels
- Panel admin tab: owner dropdown + guns field on card entries
- Smoke test: 42 checks, incl. retroactive history visibility after
  linking a card and rejection of links to unknown users
This commit is contained in:
2026-07-11 20:03:36 +02:00
parent 3326c8d16c
commit 70c2fe1210
8 changed files with 85 additions and 39 deletions
+6 -1
View File
@@ -17,7 +17,12 @@ First card tapped on the real charger: idTag `1000000000000393` → Authorize
Flow for each new card: tap once in learn mode → harvest its idTag from `seen_tags.json` / `GET /api/allowlist` → add to `authorized` with a name (`kind: "card"`).
Since 2026-07-11 a card can be **restricted per gun**: an optional `"connectors": [2]` on its allowlist entry makes `decide()` reject it on any other connector. The check fires at `StartTransaction` (the only message that carries the connector); the preceding `Authorize` (tap) still says Accepted — harmless on this charger, because `AuthorizeRemoteTxRequests=false` makes `StartTransaction` the decisive call anyway (see the probe facts below). ✅ verified with a fake charge point: gun-2-only card → `Invalid`/tx 0 on gun 1, Accepted on gun 2.
Since 2026-07-11 a card can be **linked to a user and restricted per gun**:
- `"user": "julian"` on the allowlist entry ties the card to a user account — sessions started with it attribute to that person in the history, and the owner sees their card's sessions even without gun-wide permissions (ownership is looked up live, so linking is retroactive for history visibility).
- `"guns": ["eu092025:2"]` restricts the card to specific guns, **charger-qualified** (fnmatch patterns, so `"eu092025:*"` = all guns of one charger) — multi-charger-safe by construction. The check fires at `StartTransaction` (the only message that carries the connector); the preceding `Authorize` (tap) still says Accepted — harmless on this charger, because `AuthorizeRemoteTxRequests=false` makes `StartTransaction` the decisive call anyway (see the probe facts below). ✅ verified with a fake charge point: gun-2-only card → `Invalid`/tx 0 on gun 1, Accepted on gun 2.
Both are editable from the panel's Admin tab (owner dropdown + guns field).
## 2. By vehicle / chassis — ❌ CLOSED: not supported (2026-07-09)
+2 -1
View File
@@ -40,7 +40,8 @@ backend/ (run: venv\Scripts\python -m backend.main, from the repo root)
## Design facts
- Carried over from the proven discovery code: the `decide()` gate ([[authorization]]) — now with optional per-gun card restriction (`"connectors": [2]`) — the `_WSAdapter` WebSocket bridge, learn mode, the JSONL event journal. `DataTransfer` frames are journaled too (the VIN long-shot lead in [[authorization]]).
- Carried over from the proven discovery code: the `decide()` gate ([[authorization]]) — now with card→user linking (`"user"`) and charger-qualified gun restriction (`"guns": ["eu092025:2"]`, fnmatch) — the `_WSAdapter` WebSocket bridge, learn mode, the JSONL event journal. `DataTransfer` frames are journaled too (the VIN long-shot lead in [[authorization]]).
- **Multi-charger by design**: a second charger only needs the same URL with a unique ChargerID; scopes, sessions, card restrictions, and the panel are all keyed by charger id. Gun display names come from the `"chargers"` config section (not hardcoded).
- **Multi-user auth** (2026-07-11, replaced the interim `api_token`): username/password users with permission scopes — `charger:<cp>:<gun>`, `relay:<name>`, `admin` — full model in [[authorization]]. Stdlib crypto only (PBKDF2 + HMAC sessions). Zero users = open API (startup warns); bootstrap via `python -m backend.users add <name> --scopes admin`.
- Remote starts run under the calling user's own idTag (`app-<name>`, auto-enrolled) → per-person session attribution.
- **Consumption tracking** (`store.py`, 2026-07-11): every `StartTransaction` opens a row in `sessions.db` with a resolved "who" (allowlist name or username); `StopTransaction` closes it and computes Wh from the meter diff; `MeterValues` rows land in a `samples` table (energy/power/SoC, kW→W normalized). Transaction ids continue from `MAX(tx)` across restarts, and stop-by-tx falls back to the store so sessions running through a backend restart stay stoppable.
+4
View File
@@ -71,3 +71,7 @@ Owner requirement: per-user permissions (gun 1 vs gun 2 of eu092025, per-relay o
## [2026-07-11] meta | Consumption tracking (SQLite) + the PWA control panel
Owner requirement: button-push start when a gun is ready (and permitted) + "who charged when and how many kWh". Built store.py (sessions.db: sessions with resolved who/started/ended/Wh + per-minute samples energy/power/SoC; tx ids survive restarts; stop falls back to store after restart), /api/sessions with visibility rules (own + in-scope guns; admin all), and the phase-3 web panel in backend/static/ (vanilla JS PWA at /: login, gun cards with scope- and readiness-gated Start/Stop, relay toggles, history tab, admin tab with allowlist/users/mode). Fixed a sqlite connection leak (with-connect commits but never closes). Smoke test 28 -> 37 checks, all passing. Updated [[backend]], [[architecture-plan]] (phases 1-3 all done), [[overview]], [[index]].
## [2026-07-11] meta | Cards linked to users; multi-charger gaps closed
Owner requirements: cards must belong to a person (not float around as "RFID card A (tapped first...)"), and adding a second charger must be clean. Allowlist entries now take "user" (sessions attribute to the person; owner sees their card's history) and charger-qualified "guns": ["eu092025:2"] (fnmatch; replaces the charger-blind "connectors" field). Gun display names moved from hardcoded panel JS to a "chargers" config section served via /api/status. Adding a charger = point it at the same URL with a unique ChargerID + grant charger:<id>:<gun> scopes. Panel admin tab: owner dropdown + guns field. Smoke test 37 -> 42 checks. Updated [[authorization]], [[backend]].