Files
bonaminandClaude Opus 5.5 7706bf2470 feat(backend,waiter,terminal,manager): sign-in: ask before signing a waiter's other phone out, usernames on paired screens
- auth.single_session_waiter gains 'ask', the new default (Alembic 0051 moves venues on 'off'
  to it): a second phone gets auth.session_elsewhere {device, since}; Continue retries with
  replace=true and the first phone is signed out at once (live session.ended, reason
  "replaced") and says why on its sign-in page. Only phones count (terminal sessions are
  their own client). refuse_new / end_old unchanged.
- auth.session_ended now carries the reason; the waiter app keeps it until the next sign-in.
- The waiter app sends queued offline ops on every (re)connection, so an order queued on a
  phone that was signed out by another goes out after the next sign-in there (e2e checked).
- auth.terminal_require_username (venue) + Device.require_username (per device, wins):
  GET /api/devices/me/users lists nobody, the screen asks for a username then PIN/password,
  a picked user_id is refused (auth.username_required), "tap" falls back to PIN.
- Manager: Sign-in settings (ask, the new switch), the device dialog's usernames choice.

Plan platform-v2 6F.7.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 12:51:37 +02:00

54 lines
3.1 KiB
Python

"""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, Boolean, 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
require_username = Column(Boolean, nullable=True) # NULL = site default auth.terminal_require_username (6F.7)
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)