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>
This commit is contained in:
2026-11-13 09:44:40 +02:00
co-authored by Claude Opus 5.5
parent 1d7a521e05
commit 91da3aeb27
25 changed files with 1208 additions and 48 deletions
+37 -2
View File
@@ -49,8 +49,10 @@ def do_login(db: Session, request: Request, *, client: str, username: str | None
pin: str | None, password: str | None, legacy: bool = False) -> TokenResponse:
"""The one login path. Applies the client's login mode, its access key and the
single-session rule, then creates a session (core/sessions.py)."""
if client not in ("manager", "waiter"):
if client not in ("manager", "waiter", "terminal"):
raise XeniaError("validation.invalid")
if client == "terminal":
return _terminal_login(db, request, username=username, user_id=user_id, pin=pin, password=password)
# Support access (synthetic superadmin). >>> replaced by challenge codes in step 1.10
if (client == "manager" and settings.MASTER_USERNAME and settings.MASTER_PASSWORD
@@ -94,9 +96,42 @@ def do_login(db: Session, request: Request, *, client: str, username: str | None
return TokenResponse(access_token=make_token(user, row.id), user=UserOut.model_validate(user))
def _terminal_login(db: Session, request: Request, *, username, user_id, pin, password) -> TokenResponse:
"""A user on a paired device (D-8). Needs the X-Xenia-Device header; the device's login
mode applies (tap and none exist only here). The user needs '<app>.access'. The session
is bound to the device and replaces the device's previous one."""
from core.access.context import current_device
from core.modules.state import blocked_error, is_effective
from routers.devices import effective_login_mode
device = current_device(request, db)
if device is None:
raise XeniaError("device.not_paired")
if not is_effective(device.app_key, db):
raise blocked_error(device.app_key, db)
mode = effective_login_mode(db, device)
if mode == "none":
raise XeniaError("auth.login_mode_not_allowed", mode="none") # the device works without a user
if mode == "pin" and not pin:
raise XeniaError("auth.login_mode_not_allowed", mode="password")
if mode == "password" and not password:
raise XeniaError("auth.login_mode_not_allowed", mode="pin")
q = db.query(User).filter(User.is_active == True) # noqa: E712
user = q.filter(User.username == username).first() if username else (
q.filter(User.id == user_id).first() if user_id is not None else None)
if user is None or (mode != "tap" and not _check_secret(user, pin, password, db)):
raise XeniaError("auth.invalid_credentials")
if not holds(user, f"{device.app_key}.access"):
raise XeniaError("auth.client_not_allowed", client=device.app_key)
row = sessions.start(db, user, "terminal", request, device=device)
return TokenResponse(access_token=make_token(user, row.id), user=UserOut.model_validate(user))
@router.post("/login", response_model=TokenResponse, dependencies=[Depends(public())])
def login(body: LoginRequest, request: Request, db: Session = Depends(get_db)):
"""POST {client: "manager"|"waiter", username?, user_id?, pin?, password?}.
"""POST {client: "manager"|"waiter"|"terminal", username?, user_id?, pin?, password?}.
"terminal" = a user on a paired device: send the X-Xenia-Device header.
Without `client` (the current manager UI): a manager login with v1 rules (pin or password)."""
return do_login(db, request, client=body.client or "manager", username=body.username,
user_id=body.user_id, pin=body.pin, password=body.password, legacy=body.client is None)