a9b8436f2c
'app token of <name>' synthetic allowlist entries are gone: decide() recognizes a user's personal idTag directly, enforces the user's own charger:<cp>:<gun> scopes at the charger-side gate, and attributes the session (who=user, method=app). charger_start no longer auto-enrolls; PUT /api/allowlist/<user-tag> returns 400; stale entries auto-drop at startup. Zero-user open mode still authorizes 'app-remote' for bootstrap. Smoke: 63 checks.
76 lines
2.9 KiB
Python
76 lines
2.9 KiB
Python
"""App state sections, backed by the settings table in backend.db (store.py).
|
|
|
|
CONFIG is the in-memory runtime state; save() persists it. Every mutation goes
|
|
through the API/panel (or the backend.users CLI), takes effect immediately,
|
|
and is durable — there is no config file to hand-edit and no restart-to-apply
|
|
semantics. A legacy config.json (pre-2026-07-11 layout) is imported into the
|
|
database once on startup and renamed to config.json.migrated.
|
|
|
|
Sections:
|
|
users name -> {"password": pbkdf2 hash, "scopes": [...], "id_tag"}
|
|
authorized idTag -> {"name", "kind": "card|app|car",
|
|
optional "user": owner, "guns": ["eu092025:2"]}
|
|
pending_cards unknown idTags tapped on a charger, awaiting the admin's
|
|
approve (-> authorized, linked to a user) or reject decision;
|
|
idTag -> {"first_seen", "last_seen", "count", "cp", "connector"}
|
|
denied_cards rejected idTags; silently refused, never re-pend
|
|
relays name -> {"id", "key", "ip", "version", "switch", "label"}
|
|
chargers cp_id -> {"label", "guns": {"1": "display name"}} (UI only)
|
|
session_secret signs login sessions; auto-generated once
|
|
|
|
There is no learn mode anymore: an unknown card is ALWAYS refused, but lands
|
|
in pending_cards for the admin to approve or reject from the panel.
|
|
"""
|
|
|
|
import json
|
|
import logging
|
|
import secrets
|
|
from pathlib import Path
|
|
|
|
from . import store
|
|
|
|
HERE = Path(__file__).resolve().parent
|
|
LEGACY_CONFIG = HERE / "config.json"
|
|
|
|
log = logging.getLogger("config")
|
|
|
|
DEFAULTS = {
|
|
"users": {},
|
|
"authorized": {},
|
|
"pending_cards": {},
|
|
"denied_cards": {},
|
|
"relays": {},
|
|
"chargers": {},
|
|
}
|
|
|
|
|
|
def _load() -> dict:
|
|
cfg = store.load_settings()
|
|
if not cfg and LEGACY_CONFIG.exists():
|
|
cfg = json.loads(LEGACY_CONFIG.read_text(encoding="utf-8"))
|
|
LEGACY_CONFIG.rename(LEGACY_CONFIG.with_suffix(".json.migrated"))
|
|
log.warning("imported legacy config.json into %s (file kept as "
|
|
"config.json.migrated)", store.DB_PATH.name)
|
|
for key, default in DEFAULTS.items():
|
|
cfg.setdefault(key, default if not isinstance(default, dict) else dict(default))
|
|
cfg.pop("learn_mode", None) # superseded by the pending-cards workflow
|
|
# users' app idTags used to be auto-enrolled as "app token of <name>"
|
|
# allowlist entries; they now authorize via the user record directly
|
|
user_tags = {u.get("id_tag") for u in cfg["users"].values()}
|
|
for tag in [t for t in cfg["authorized"] if t in user_tags]:
|
|
del cfg["authorized"][tag]
|
|
log.info("dropped redundant allowlist entry %r (a user's app token)", tag)
|
|
if not cfg.get("session_secret"):
|
|
cfg["session_secret"] = secrets.token_hex(32)
|
|
return cfg
|
|
|
|
|
|
CONFIG = _load()
|
|
|
|
|
|
def save() -> None:
|
|
store.save_settings(CONFIG)
|
|
|
|
|
|
save() # persist migration / generated defaults immediately
|