refactor(map): remove access gate

This commit is contained in:
lingniu
2026-08-11 19:24:32 +08:00
parent d1ba129e99
commit 357dd490e9
9 changed files with 140 additions and 580 deletions
+2 -218
View File
@@ -1,10 +1,7 @@
"""Vehicle Map static server and server-side proxy for the vehicle open platform."""
from concurrent.futures import ThreadPoolExecutor
import base64
import binascii
import hashlib
import hmac
from http.server import HTTPServer, SimpleHTTPRequestHandler
import json
import os
@@ -36,134 +33,10 @@ OPEN_PLATFORM_APP_KEY = os.getenv("OPEN_PLATFORM_APP_KEY", "").strip()
UPSTREAM_TIMEOUT_SECONDS = float(os.getenv("UPSTREAM_TIMEOUT_SECONDS", "15"))
DASHBOARD_CACHE_SECONDS = int(os.getenv("DASHBOARD_CACHE_SECONDS", "5"))
STATION_CACHE_SECONDS = int(os.getenv("STATION_CACHE_SECONDS", "3600"))
AUTH_PROVIDER = os.getenv("VEHICLE_MAP_AUTH_PROVIDER", "local").strip().lower()
LOCAL_ACCESS_CODE = os.getenv("VEHICLE_MAP_ACCESS_CODE", "").strip()
SESSION_SECRET = os.getenv("VEHICLE_MAP_SESSION_SECRET", "").encode("utf-8")
SESSION_TTL_SECONDS = int(os.getenv("VEHICLE_MAP_SESSION_TTL_SECONDS", "1800"))
COOKIE_SECURE = os.getenv("VEHICLE_MAP_COOKIE_SECURE", "true").strip().lower() not in {"0", "false", "no"}
AUTH_CENTER_INTROSPECTION_URL = os.getenv("VEHICLE_MAP_AUTH_CENTER_INTROSPECTION_URL", "").strip()
AUTH_CENTER_CLIENT_TOKEN = os.getenv("VEHICLE_MAP_AUTH_CENTER_CLIENT_TOKEN", "").strip()
OPERATIONS_READ_SCOPE = "operations:read"
SESSION_COOKIE_NAME = "ln_map_session"
_cache_lock = threading.Lock()
_cache = {}
class AuthenticationError(RuntimeError):
"""The request does not carry a valid operations-read session."""
class AuthenticationUnavailable(RuntimeError):
"""The selected authentication provider is not configured."""
class AccessAuthorizer:
"""Small provider boundary: replace this class when the auth center contract is ready."""
def authorize_access_code(self, access_code):
raise NotImplementedError
class LocalAccessCodeAuthorizer(AccessAuthorizer):
def authorize_access_code(self, access_code):
if not LOCAL_ACCESS_CODE or not SESSION_SECRET:
raise AuthenticationUnavailable("local access-code authentication is not configured")
if not hmac.compare_digest(str(access_code or ""), LOCAL_ACCESS_CODE):
raise AuthenticationError("invalid access code")
return {"subject": "local-access-code", "scopes": [OPERATIONS_READ_SCOPE]}
class AuthCenterAccessCodeAuthorizer(AccessAuthorizer):
"""Adapter for an auth-center access-code introspection endpoint.
The endpoint contract is deliberately small: it receives accessCode and audience,
and returns {active, subject, scopes}. No frontend change is needed when enabled.
"""
def authorize_access_code(self, access_code):
if not AUTH_CENTER_INTROSPECTION_URL:
raise AuthenticationUnavailable("auth-center introspection URL is not configured")
headers = {"Content-Type": "application/json", "Accept": "application/json"}
if AUTH_CENTER_CLIENT_TOKEN:
headers["Authorization"] = "Bearer " + AUTH_CENTER_CLIENT_TOKEN
request = Request(
AUTH_CENTER_INTROSPECTION_URL,
data=_json_bytes({"accessCode": str(access_code or ""), "audience": "vehicle-map"}),
method="POST",
headers=headers,
)
try:
with urlopen(request, timeout=UPSTREAM_TIMEOUT_SECONDS) as response:
payload = json.load(response)
except (HTTPError, URLError, ValueError) as exc:
raise AuthenticationUnavailable("auth-center is unavailable") from exc
scopes = payload.get("scopes") or []
if not payload.get("active") or OPERATIONS_READ_SCOPE not in scopes:
raise AuthenticationError("access code does not grant operations read")
return {"subject": str(payload.get("subject") or "auth-center"), "scopes": scopes}
def _authorizer():
if AUTH_PROVIDER == "local":
return LocalAccessCodeAuthorizer()
if AUTH_PROVIDER in {"auth-center", "auth_center"}:
return AuthCenterAccessCodeAuthorizer()
raise AuthenticationUnavailable("unknown authentication provider")
def _b64encode(value):
return base64.urlsafe_b64encode(value).decode("ascii").rstrip("=")
def _b64decode(value):
padded = str(value) + "=" * (-len(str(value)) % 4)
return base64.urlsafe_b64decode(padded.encode("ascii"))
def _create_session(principal):
if not SESSION_SECRET:
raise AuthenticationUnavailable("session signing secret is not configured")
payload = {
# Incrementing the session format invalidates earlier long-lived sessions.
"v": 2,
"sub": principal["subject"],
"scopes": principal.get("scopes") or [],
"exp": int(time.time()) + max(300, SESSION_TTL_SECONDS),
}
encoded = _b64encode(_json_bytes(payload))
signature = _b64encode(hmac.new(SESSION_SECRET, encoded.encode("ascii"), hashlib.sha256).digest())
return encoded + "." + signature
def _read_session(cookie_header):
cookies = {}
for item in str(cookie_header or "").split(";"):
if "=" in item:
key, value = item.strip().split("=", 1)
cookies[key] = value
token = cookies.get(SESSION_COOKIE_NAME, "")
try:
encoded, signature = token.split(".", 1)
expected = _b64encode(hmac.new(SESSION_SECRET, encoded.encode("ascii"), hashlib.sha256).digest())
if not SESSION_SECRET or not hmac.compare_digest(signature, expected):
return None
payload = json.loads(_b64decode(encoded))
if payload.get("v") != 2 or int(payload.get("exp", 0)) <= int(time.time()):
return None
if OPERATIONS_READ_SCOPE not in (payload.get("scopes") or []):
return None
return {"subject": str(payload.get("sub") or ""), "scopes": payload["scopes"]}
except (ValueError, TypeError, UnicodeDecodeError, binascii.Error, json.JSONDecodeError):
return None
def _session_cookie(value, max_age):
secure = "; Secure" if COOKIE_SECURE else ""
return f"{SESSION_COOKIE_NAME}={value}; Path=/; Max-Age={max_age}; HttpOnly; SameSite=Lax{secure}"
class ThreadingHTTPServer(ThreadingMixIn, HTTPServer):
daemon_threads = True
allow_reuse_address = True
@@ -220,29 +93,6 @@ def _load_stations():
)
def _public_station(station):
"""Public station directory contract. Keep this a positive field allowlist."""
fields = (
"id", "name", "shortName", "province", "city", "district", "address",
"longitude", "latitude", "cooperative",
)
return {field: station.get(field) for field in fields if station.get(field) is not None}
def _load_public_stations():
stations = _load_stations()
return {
"status": "ok",
"access": {"level": "public", "stations": "directory", "vehicles": "restricted", "hydrogen": "restricted"},
"asOf": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
"summary": {
"totalStations": len(stations),
"cooperativeStations": sum(1 for station in stations if station.get("cooperative")),
},
"stations": [_public_station(station) for station in stations],
}
def _load_dashboard():
today = time.strftime("%Y-%m-%d", time.localtime())
with ThreadPoolExecutor(max_workers=3) as executor:
@@ -262,7 +112,7 @@ def _load_dashboard():
for row in mileage_rows
if row.get("vin") and row.get("status") == "NORMAL"
}
# The operations card is a daily activity view: every vehicle that reported
# The daily activity view places every vehicle that reported
# today is placed in exactly one bucket using its latest selected speed.
active_vehicles = [item for item in vehicles if item.get("activeToday")]
driving = sum(1 for item in active_vehicles if float(item.get("speedKmh") or 0) > 3)
@@ -312,32 +162,9 @@ class VehicleMapHandler(SimpleHTTPRequestHandler):
"status": "ok",
"service": "vehicle-map",
"openPlatformConfigured": bool(OPEN_PLATFORM_APP_KEY),
"authProvider": AUTH_PROVIDER,
})
return
if path == "/api/public/stations":
try:
payload = _cached("public-stations", DASHBOARD_CACHE_SECONDS, _load_public_stations)
self._write_json(200, payload)
except Exception as exc: # keep upstream details server-side only
self.log_error("public station refresh failed: %s", exc)
self._write_json(502, {
"status": "error",
"code": "UPSTREAM_UNAVAILABLE",
"message": "加氢站目录暂时不可用,请稍后重试",
})
return
if path == "/api/session":
principal = _read_session(self.headers.get("Cookie"))
self._write_json(200, {
"status": "ok",
"authorized": bool(principal),
"principal": principal,
})
return
if path == "/api/dashboard":
if not self._require_operations_access():
return
try:
payload = _cached("dashboard", DASHBOARD_CACHE_SECONDS, _load_dashboard)
self._write_json(200, payload)
@@ -346,54 +173,11 @@ class VehicleMapHandler(SimpleHTTPRequestHandler):
self._write_json(502, {
"status": "error",
"code": "UPSTREAM_UNAVAILABLE",
"message": "运营数据暂时不可用,请稍后重试",
"message": "地图数据暂时不可用,请稍后重试",
})
return
super().do_GET()
def do_POST(self):
path = urlparse(self.path).path
if path != "/api/session":
self._write_json(404, {"status": "error", "code": "NOT_FOUND"})
return
try:
content_length = int(self.headers.get("Content-Length", "0"))
if content_length < 1 or content_length > 4096:
raise ValueError("invalid request length")
payload = json.loads(self.rfile.read(content_length))
principal = _authorizer().authorize_access_code(payload.get("accessCode"))
session = _create_session(principal)
except AuthenticationError:
self._write_json(401, {"status": "error", "code": "ACCESS_DENIED", "message": "访问码无效"})
return
except AuthenticationUnavailable:
self._write_json(503, {"status": "error", "code": "AUTH_UNAVAILABLE", "message": "授权服务暂不可用"})
return
except (ValueError, TypeError, json.JSONDecodeError):
self._write_json(400, {"status": "error", "code": "INVALID_REQUEST", "message": "访问码格式不正确"})
return
self._write_json(200, {
"status": "ok",
"authorized": True,
"principal": principal,
}, headers={"Set-Cookie": _session_cookie(session, max(300, SESSION_TTL_SECONDS))})
def do_DELETE(self):
if urlparse(self.path).path != "/api/session":
self._write_json(404, {"status": "error", "code": "NOT_FOUND"})
return
self._write_json(200, {"status": "ok", "authorized": False}, headers={"Set-Cookie": _session_cookie("", 0)})
def _require_operations_access(self):
if _read_session(self.headers.get("Cookie")):
return True
self._write_json(401, {
"status": "error",
"code": "AUTH_REQUIRED",
"message": "运营数据需要授权访问",
})
return False
def _serve_index(self):
try:
template = (ROOT / "index.html").read_text(encoding="utf-8")