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
+1
View File
@@ -30,3 +30,4 @@ import models.sync_event # noqa: F401
import models.site_module # noqa: F401 SiteModule (module enablement, step 1.3)
import models.user_permission # noqa: F401 UserPermission (permission keys, step 1.5)
import models.session # noqa: F401 LoginSession (step 1.6)
import models.device # noqa: F401 Device, DevicePairRequest (step 1.8)
+52
View File
@@ -0,0 +1,52 @@
"""Paired devices and pairing requests (APPS_PLATFORM D-8, §6.3, plan step 1.8).
A device is shared or unattended hardware (counter PC, KDS screen, TV, tablet) that runs
exactly one app module (`app_key`). It proves itself with a long random token, sent as
header `X-Xenia-Device`; only its sha256 is stored. Personal phones and the manager's
browser are NOT devices (they are user sessions).
Pairing: the device asks for a code (device_pair_requests row, 10 min), a manager enters
the code and creates the Device row, the device's next poll generates the token, stores
its hash and deletes the request. So the plain token is never stored anywhere.
Devices are never deleted: revoked rows keep their token hash so the device is told
`device.revoked` (not "unknown"), and old sessions/audit rows keep a valid device_id.
Read and written through routers/devices.py and core/access/context.py.
"""
from sqlalchemy import JSON, Column, ForeignKey, Integer, String
from core.timeutil import UTCDateTime, utcnow
from database import Base
class Device(Base):
__tablename__ = "devices"
__table_args__ = {"sqlite_autoincrement": True} # ids are never reused (sessions point at them)
id = Column(Integer, primary_key=True, autoincrement=True)
uuid = Column(String, nullable=False, unique=True) # public identifier
name = Column(String, nullable=False)
app_key = Column(String, nullable=False, index=True) # a kind="app" module with device_app
config = Column(JSON, nullable=True) # app-specific (e.g. KDS prep zone)
login_mode = Column(String, nullable=True) # NULL = site default auth.default_login_mode
token_hash = Column(String, nullable=True, unique=True) # sha256 hex; NULL until the device picks it up
linked_device_id = Column(Integer, ForeignKey("devices.id", ondelete="SET NULL"), nullable=True)
user_agent = Column(String, nullable=True) # from the pairing request
paired_at = Column(UTCDateTime, nullable=False, default=utcnow)
paired_by = Column(Integer, ForeignKey("users.id", ondelete="SET NULL"), nullable=True)
claimed_at = Column(UTCDateTime, nullable=True) # when the device received its token
last_seen_at = Column(UTCDateTime, nullable=True)
revoked_at = Column(UTCDateTime, nullable=True, index=True)
revoked_by = Column(Integer, ForeignKey("users.id", ondelete="SET NULL"), nullable=True)
class DevicePairRequest(Base):
__tablename__ = "device_pair_requests"
code = Column(String, primary_key=True) # 6 chars, shown on the device
secret_hash = Column(String, nullable=False) # only the requester may poll
created_at = Column(UTCDateTime, nullable=False, default=utcnow)
expires_at = Column(UTCDateTime, nullable=False, index=True)
device_id = Column(Integer, ForeignKey("devices.id", ondelete="CASCADE"), nullable=True) # set on approval
ip = Column(String, nullable=True)
user_agent = Column(String, nullable=True)
+2 -2
View File
@@ -17,11 +17,11 @@ class LoginSession(Base):
id = Column(String, primary_key=True) # uuid4 hex
user_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=True, index=True)
# NULL user_id = the support account (synthetic, no users row; step 1.10)
device_id = Column(Integer, nullable=True) # step 1.8
device_id = Column(Integer, ForeignKey("devices.id"), nullable=True, index=True) # terminal sessions: the device
client = Column(String, nullable=False) # manager | waiter | terminal | support
created_at = Column(UTCDateTime, nullable=False, default=utcnow)
last_seen_at = Column(UTCDateTime, nullable=False, default=utcnow)
ended_at = Column(UTCDateTime, nullable=True, index=True)
end_reason = Column(String, nullable=True) # logout | replaced | ended_by_manager | user_blocked | refreshed
end_reason = Column(String, nullable=True) # logout | replaced | ended_by_manager | user_blocked | refreshed | device_revoked | device_reassigned
ip = Column(String, nullable=True)
user_agent = Column(String, nullable=True)