Files
xenia-pos-local/local_backend/core/access/deps.py
T
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

82 lines
3.1 KiB
Python

"""Route access markers (APPS_PLATFORM §3.1, D-17).
Each marker is a FastAPI dependency with an `__xenia_access__` attribute. The route
guard (tests/guards/test_route_access.py) walks every route's dependency tree and fails
any route without a marker, so "forgot to protect" can't ship.
"""
from __future__ import annotations
from fastapi import Depends, Request
from sqlalchemy.orm import Session
from core.access.check import AccessContext, check
from core.access.context import current_device
from core.errors import XeniaError
from database import get_db
def _mark(fn, *marker):
fn.__xenia_access__ = marker
return fn
def require(key: str):
"""The user must hold `key`, and its module must be effective. Returns the user."""
# Keys are not validated here (routers are imported while the registry loads);
# tests/guards/test_route_access.py checks every marker's key exists.
from routers.deps import get_current_user
def _dep(request: Request, user=Depends(get_current_user), db: Session = Depends(get_db)):
check(AccessContext(user=user, device=current_device(request, db)), key, db)
return user
_dep.__name__ = f"require_{key.replace('.', '_')}"
return _mark(_dep, "require", key)
def require_any(*keys: str):
"""The user must hold at least one of `keys` (in an effective module). Returns the user."""
from routers.deps import get_current_user
def _dep(request: Request, user=Depends(get_current_user), db: Session = Depends(get_db)):
last = None
ctx = AccessContext(user=user, device=current_device(request, db))
for k in keys:
try:
check(ctx, k, db)
return user
except XeniaError as e:
last = e
raise last
_dep.__name__ = "require_any_" + "_".join(k.replace(".", "_") for k in keys)
return _mark(_dep, "require_any", *keys)
def public():
"""Deliberately open endpoint (health, identity, login, pairing request...)."""
def _dep():
return None
return _mark(_dep, "public")
def require_device(app_key: str | None = None):
"""Device-only endpoint (D-8): the request must carry a valid X-Xenia-Device token.
With `app_key`, the device must run that app and the app's module must be effective
(licensed and enabled). Without it, any paired device (e.g. GET /api/devices/me, which
must answer even when the app is turned off so the screen can say so). Returns the Device."""
def _dep(request: Request, db: Session = Depends(get_db)):
dev = current_device(request, db)
if dev is None:
raise XeniaError("device.not_paired")
if app_key is not None:
if dev.app_key != app_key:
raise XeniaError("device.out_of_scope", app=dev.app_key, key=app_key)
from core.modules.state import blocked_error, is_effective
if not is_effective(app_key, db):
raise blocked_error(app_key, db)
return dev
_dep.__name__ = f"require_device_{app_key or 'any'}"
return _mark(_dep, "require_device", app_key or "*")