refactor(backend): single SQLite store — config.json eliminated

backend.db (renamed from sessions.db) now holds everything: the sessions
and samples tables plus a settings table with the app-state sections
(users, allowlist, relays, chargers, learn_mode, session_secret) as JSON
blobs. All mutations go through the API/panel and take effect immediately;
there is no config file and no hand-edit-then-restart ambiguity.

- legacy config.json auto-imports into the db on first start (renamed to
  config.json.migrated as a backup)
- new admin endpoints: PUT/DELETE /api/relays/{name} (how tinytuya wizard
  results are registered now) and PUT /api/chargers/{cp_id} (panel labels)
- BACKEND_DB env override so tests run on an isolated temp database
- smoke test: 45 checks, real db verified untouched
This commit is contained in:
2026-07-11 20:15:38 +02:00
parent 70c2fe1210
commit 56c9020d9f
10 changed files with 132 additions and 96 deletions
+3 -3
View File
@@ -6,7 +6,7 @@ tags: [auth, requirements]
# Authorization Methods
The three planned ways to authorize a charging session. Architecturally they all collapse to one gate: **`decide(id_tag, connector)` in the [[backend]]** (`backend/csms.py`; formerly `station.py` in [[csms-toolkit]]) returns Accepted/Rejected against the allowlist in `backend/config.json`. Per [[2026-07-09-findings-and-requirements]]. On top of these charger-side methods sits the **per-user API permission system** (last section).
The three planned ways to authorize a charging session. Architecturally they all collapse to one gate: **`decide(id_tag, connector)` in the [[backend]]** (`backend/csms.py`; formerly `station.py` in [[csms-toolkit]]) returns Accepted/Rejected against the allowlist in `backend.db`. Per [[2026-07-09-findings-and-requirements]]. On top of these charger-side methods sits the **per-user API permission system** (last section).
## 1. RFID cards — ✅ verified live (2026-07-09)
@@ -55,7 +55,7 @@ A mobile-friendly web control panel (no app store):
The [[backend]] now has real multi-user authorization on top of the three methods above, because not every household member may control everything. Requirement (owner, 2026-07-11): some users control gun 1 only, some gun 2, some only certain [[wifi-relays|relays]].
- **Users** live in `backend/config.json` → `"users"`: username + PBKDF2 password hash + scope list + a personal `id_tag` (`app-<name>`) used for their remote starts, so charging sessions are **attributable per person**.
- **Users** live in the `settings` table of `backend.db`: username + PBKDF2 password hash + scope list + a personal `id_tag` (`app-<name>`) used for their remote starts, so charging sessions are **attributable per person**.
- **Login**: `POST /api/login` (username/password) → 30-day HMAC-signed session, delivered as an HttpOnly cookie (for the PWA) and as a bearer token (for curl). Stateless — survives backend restarts; deleting a user kills their sessions.
- **Scopes** (fnmatch wildcards work):
@@ -67,7 +67,7 @@ The [[backend]] now has real multi-user authorization on top of the three method
| `admin` | everything, incl. allowlist/user CRUD, learn-mode flip, setconfig, events |
- **Bootstrap**: zero users = open API (startup warns loudly). First user: `venv\Scripts\python -m backend.users add <name> --scopes admin`.
- ✅ Verified by the 28-check smoke test (login, wrong password, cookie + bearer, per-scope 403s, relay visibility filtering, user CRUD, session invalidation on delete). ⚠️ Not yet exercised from a real browser/PWA (doesn't exist yet).
- ✅ Verified by the 45-check smoke test (login, wrong password, cookie + bearer, per-scope 403s, relay visibility filtering, user CRUD, session invalidation on delete). ⚠️ Not yet exercised from a real phone browser.
This resolves the ❓ "web app auth" open decision in [[overview]]: username+password with per-resource scopes, chosen over plain tokens by the owner.