Files
xenia-pos-local/local_backend/migrations/versions/0007_devices.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

87 lines
3.9 KiB
Python

"""devices, pairing requests, session device FK (D-8, §6.3, step 1.8)
Hand-edited: the sessions FK gets a name (SQLite batch mode can't create or drop an
unnamed constraint). No data moves: sessions.device_id was always NULL before this.
Revision ID: 0007_devices
Revises: 0006_sessions
Create Date: 2026-11-13 09:33:18.938342
"""
from alembic import op
import sqlalchemy as sa
from core.timeutil import UTCDateTime
revision = '0007_devices'
down_revision = '0006_sessions'
branch_labels = None
depends_on = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('devices',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('uuid', sa.String(), nullable=False),
sa.Column('name', sa.String(), nullable=False),
sa.Column('app_key', sa.String(), nullable=False),
sa.Column('config', sa.JSON(), nullable=True),
sa.Column('login_mode', sa.String(), nullable=True),
sa.Column('token_hash', sa.String(), nullable=True),
sa.Column('linked_device_id', sa.Integer(), nullable=True),
sa.Column('user_agent', sa.String(), nullable=True),
sa.Column('paired_at', UTCDateTime(), nullable=False),
sa.Column('paired_by', sa.Integer(), nullable=True),
sa.Column('claimed_at', UTCDateTime(), nullable=True),
sa.Column('last_seen_at', UTCDateTime(), nullable=True),
sa.Column('revoked_at', UTCDateTime(), nullable=True),
sa.Column('revoked_by', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['linked_device_id'], ['devices.id'], ondelete='SET NULL'),
sa.ForeignKeyConstraint(['paired_by'], ['users.id'], ondelete='SET NULL'),
sa.ForeignKeyConstraint(['revoked_by'], ['users.id'], ondelete='SET NULL'),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('token_hash'),
sa.UniqueConstraint('uuid'),
sqlite_autoincrement=True
)
with op.batch_alter_table('devices', schema=None) as batch_op:
batch_op.create_index(batch_op.f('ix_devices_app_key'), ['app_key'], unique=False)
batch_op.create_index(batch_op.f('ix_devices_revoked_at'), ['revoked_at'], unique=False)
op.create_table('device_pair_requests',
sa.Column('code', sa.String(), nullable=False),
sa.Column('secret_hash', sa.String(), nullable=False),
sa.Column('created_at', UTCDateTime(), nullable=False),
sa.Column('expires_at', UTCDateTime(), nullable=False),
sa.Column('device_id', sa.Integer(), nullable=True),
sa.Column('ip', sa.String(), nullable=True),
sa.Column('user_agent', sa.String(), nullable=True),
sa.ForeignKeyConstraint(['device_id'], ['devices.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('code')
)
with op.batch_alter_table('device_pair_requests', schema=None) as batch_op:
batch_op.create_index(batch_op.f('ix_device_pair_requests_expires_at'), ['expires_at'], unique=False)
with op.batch_alter_table('sessions', schema=None) as batch_op:
batch_op.create_index(batch_op.f('ix_sessions_device_id'), ['device_id'], unique=False)
batch_op.create_foreign_key('fk_sessions_device_id_devices', 'devices', ['device_id'], ['id'])
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('sessions', schema=None) as batch_op:
batch_op.drop_constraint('fk_sessions_device_id_devices', type_='foreignkey')
batch_op.drop_index(batch_op.f('ix_sessions_device_id'))
with op.batch_alter_table('device_pair_requests', schema=None) as batch_op:
batch_op.drop_index(batch_op.f('ix_device_pair_requests_expires_at'))
op.drop_table('device_pair_requests')
with op.batch_alter_table('devices', schema=None) as batch_op:
batch_op.drop_index(batch_op.f('ix_devices_revoked_at'))
batch_op.drop_index(batch_op.f('ix_devices_app_key'))
op.drop_table('devices')
# ### end Alembic commands ###