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
+21 -13
View File
@@ -10,6 +10,7 @@ import asyncio
import json
import logging
from collections import deque
from fnmatch import fnmatch
from typing import Optional
from fastapi import WebSocket, WebSocketDisconnect
@@ -46,21 +47,24 @@ TRIGGERABLE = {
}
def decide(id_tag: str, context: str, connector: Optional[int] = None) -> AuthorizationStatus:
def decide(id_tag: str, context: str, connector: Optional[int] = None,
cp: Optional[str] = None) -> AuthorizationStatus:
"""The one gate every auth method flows through: is this idTag allowed?
An allowlist entry may carry "connectors": [1] to restrict a card to
specific guns. Only StartTransaction knows the connector, so the per-gun
check fires there; a plain Authorize (card tap) still accepts a known card
— which matches this charger, since AuthorizeRemoteTxRequests=false means
An allowlist entry may carry "guns": ["eu092025:2"] to restrict a card to
specific guns of specific chargers (fnmatch patterns, so "eu092025:*"
works). Only StartTransaction knows the connector, so the per-gun check
fires there; a plain Authorize (card tap) still accepts a known card —
which matches this charger, since AuthorizeRemoteTxRequests=false means
StartTransaction is the decisive call anyway.
"""
entry = CONFIG["authorized"].get(id_tag)
known = entry is not None
if known and connector is not None and entry.get("connectors") \
and connector not in entry["connectors"]:
log.warning("[NO] REJECTED idTag=%r (%s) not allowed on connector %s (allowed: %s)",
id_tag, entry.get("name", "?"), connector, entry["connectors"])
gun = f"{cp}:{connector}" if cp and connector is not None else None
if known and gun and entry.get("guns") \
and not any(fnmatch(gun, pat) for pat in entry["guns"]):
log.warning("[NO] REJECTED idTag=%r (%s) not allowed on %s (allowed: %s)",
id_tag, entry.get("name", "?"), gun, entry["guns"])
status = AuthorizationStatus.invalid
elif known:
name = entry.get("name", "?")
@@ -82,10 +86,14 @@ def decide(id_tag: str, context: str, connector: Optional[int] = None) -> Author
def resolve_who(id_tag: str) -> str:
"""Human label for a session: allowlist entry name, username, or the raw tag."""
"""Human label for a session: the card's owning user, the allowlist entry
name, the username behind an app idTag, or the raw tag — in that order."""
entry = CONFIG["authorized"].get(id_tag)
if entry and entry.get("name"):
return entry["name"]
if entry:
if entry.get("user"):
return entry["user"]
if entry.get("name"):
return entry["name"]
for username, user in CONFIG["users"].items():
if user.get("id_tag") == id_tag:
return username
@@ -125,7 +133,7 @@ class ChargePoint(OcppChargePoint):
@on(Action.start_transaction)
async def on_start_transaction(self, connector_id, id_tag, meter_start, timestamp, **kwargs):
status = decide(id_tag, f"StartTransaction (connector {connector_id})",
connector=connector_id)
connector=connector_id, cp=self.id)
if status == AuthorizationStatus.accepted:
_tx_counter["n"] += 1
tx = _tx_counter["n"]