Files
bonaminandClaude Opus 5.5 91da3aeb27 feat(backend): paired devices with device tokens, scope, limits and terminal login
Platform v2 step 1.8 (APPS_PLATFORM D-8, section 6.3).
- devices + device_pair_requests tables (Alembic 0007_devices); sessions.device_id
  becomes a FK. Device tokens are stored as sha256 only; the plain token is generated
  when the device picks it up, once.
- /api/devices: pair-request (public, rate-limited, LAN-only in the proxy) + polling
  with a per-request secret; manager approve/list/rename/config/login mode/link/
  reassign/revoke (key core.devices.manage); the device's own /me and /me/users.
- Approving or reassigning checks the app's licence and its `devices` limit
  (license.limit_reached); after a downgrade the newest devices beyond the limit are
  refused, the oldest keep working.
- X-Xenia-Device: core/access/context.py loads the device (device.not_paired /
  device.revoked), tags logs with device=, throttles last_seen_at. check() and
  module_required() apply the app's scope (device.out_of_scope); require_device(app).
- POST /api/auth/login {client: "terminal"} uses the device's login mode (device ->
  auth.default_login_mode -> app's first mode); tap/none only here. The session is
  bound to the device (device.required without its header) and replaces the
  device's previous user. Revoke/reassign end the device's sessions.
- Registry: a device app with user logins must declare '<app>.access'.
- Proxy: location /api/devices/pair-request LAN-only on the :443 servers (both copies).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 09:44:40 +02:00

63 lines
2.3 KiB
Python

"""Access guard (D-7, D-17): every route declares who may call it.
Each API route must have, somewhere in its dependency tree, one of the markers from
core/access/deps.py (require / require_any / require_device / public) or the "signed_in"
marker on routers.deps.get_current_user. A route with none fails this test: either
protect it, or mark it public() on purpose.
Also checks every key used by a marker exists in a manifest (keys aren't validated at
import time because the registry imports the routers).
"""
from fastapi.routing import APIRoute, APIWebSocketRoute
ALLOWLIST = {"/openapi.json", "/docs", "/docs/oauth2-redirect", "/redoc"}
def _markers(dependant, seen=None):
seen = set() if seen is None else seen
out = []
for d in dependant.dependencies:
call = d.call
marker = getattr(call, "__xenia_access__", None)
if marker:
out.append(marker)
if id(d) not in seen:
seen.add(id(d))
out += _markers(d, seen)
return out
def _routes():
import main
for r in main.app.routes:
if isinstance(r, (APIRoute, APIWebSocketRoute)) and r.path not in ALLOWLIST:
yield r
def test_every_route_declares_access():
missing = sorted(f"{','.join(sorted(getattr(r, 'methods', None) or {'WS'}))} {r.path}"
for r in _routes() if not _markers(r.dependant))
assert missing == [], "routes without an access marker (require/require_any/public/signed_in):\n" + "\n".join(missing)
def test_every_marker_key_exists():
from core.modules.registry import all_permissions
known = set(all_permissions())
bad = set()
for r in _routes():
for kind, *keys in _markers(r.dependant):
if kind in ("require", "require_any"):
bad |= set(keys) - known
assert bad == set(), f"unknown permission keys in route markers: {sorted(bad)}"
def test_every_require_device_names_a_device_app():
from core.modules.registry import registry
apps = {k for k, m in registry().items() if m.device_app}
bad = set()
for r in _routes():
for kind, *args in _markers(r.dependant):
if kind == "require_device" and args[0] != "*" and args[0] not in apps:
bad.add(args[0])
assert bad == set(), f"require_device() with modules that aren't device apps: {sorted(bad)}"