refactor(backend): user app idTags validated against the user record, not the allowlist

'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.
This commit is contained in:
2026-07-12 09:50:27 +02:00
parent 25fa1f9fc5
commit a9b8436f2c
6 changed files with 54 additions and 10 deletions
+37 -4
View File
@@ -26,7 +26,7 @@ from ocpp.v16.enums import (
RegistrationStatus,
)
from . import store
from . import store, users
from .config import CONFIG, HERE, save
from .journal import now, record_event
@@ -68,11 +68,16 @@ def decide(id_tag: str, context: str, connector: Optional[int] = None,
which matches this charger, since AuthorizeRemoteTxRequests=false means
StartTransaction is the decisive call anyway.
A user's app idTag (users.<name>.id_tag, used for their in-app starts) is
validated directly against the user record — it is NOT an allowlist entry,
and it must satisfy the user's own charger:<cp>:<gun> scopes.
Unknown idTags are always refused: silently if the admin already rejected
the card, otherwise they land in pending_cards for the admin to decide.
"""
entry = CONFIG["authorized"].get(id_tag)
known = entry is not None
app_user = user_for_tag(id_tag)
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"]):
@@ -83,6 +88,20 @@ def decide(id_tag: str, context: str, connector: Optional[int] = None,
name = entry.get("name", "?")
log.info("[OK] AUTHORIZED idTag=%r (%s) via %s", id_tag, name, context)
status = AuthorizationStatus.accepted
elif app_user is not None:
known = True
scopes = CONFIG["users"][app_user].get("scopes", [])
if gun and not users.allowed(scopes, f"charger:{cp}:{connector}"):
log.warning("[NO] REJECTED app idTag of %r on %s (no permission)",
app_user, gun)
status = AuthorizationStatus.invalid
else:
log.info("[OK] AUTHORIZED app start by user %r via %s", app_user, context)
status = AuthorizationStatus.accepted
elif id_tag == "app-remote" and not CONFIG["users"]:
# bootstrap: with zero users the API is open and starts run as "app-remote"
log.info("[OK] AUTHORIZED app-remote (open mode, no users yet)")
status = AuthorizationStatus.accepted
elif id_tag in CONFIG["denied_cards"]:
log.warning("[NO] DENIED idTag=%r (admin-rejected card) via %s", id_tag, context)
status = AuthorizationStatus.invalid
@@ -98,6 +117,14 @@ def decide(id_tag: str, context: str, connector: Optional[int] = None,
return status
def user_for_tag(id_tag: str) -> Optional[str]:
"""Username whose personal app idTag this is, if any."""
for username, user in CONFIG["users"].items():
if user.get("id_tag") == id_tag:
return username
return None
def resolve_who(id_tag: str) -> str:
"""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."""
@@ -151,9 +178,15 @@ class ChargePoint(OcppChargePoint):
_tx_counter["n"] += 1
tx = _tx_counter["n"]
who = resolve_who(id_tag)
# audit: HOW was this session authorized? kind of the allowlist
# entry -> "card" (physical tap) or "app" (in-app button press)
method = CONFIG["authorized"].get(id_tag, {}).get("kind", "unknown")
# audit: HOW was this session authorized? allowlist kind ->
# "card" (physical tap); a user's app idTag -> "app" (in-app button)
entry = CONFIG["authorized"].get(id_tag)
if entry is not None:
method = entry.get("kind", "unknown")
elif user_for_tag(id_tag) or id_tag == "app-remote":
method = "app"
else:
method = "unknown"
ACTIVE_TX[tx] = {
"cp": self.id, "connector": connector_id, "id_tag": id_tag,
"who": who, "method": method,