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>
59 lines
2.0 KiB
Python
59 lines
2.0 KiB
Python
"""The one access decision (APPS_PLATFORM §3.1)."""
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
from typing import Any, Optional
|
|
|
|
from core.errors import XeniaError
|
|
from core.modules.registry import all_permissions
|
|
|
|
|
|
@dataclass
|
|
class AccessContext:
|
|
user: Any = None # User row, or the support ghost (step 1.10); None for device-only calls
|
|
session: Any = None # session id (sid) of the user's login
|
|
device: Any = None # Device row when the call comes from a paired device (D-8)
|
|
override_keys: frozenset = frozenset() # step 1.9
|
|
|
|
|
|
def module_of(key: str) -> str:
|
|
try:
|
|
return all_permissions()[key][1]
|
|
except KeyError:
|
|
raise ValueError(f"unknown permission key {key!r}: declare it in a module manifest") from None
|
|
|
|
|
|
def holds(user, key: str) -> bool:
|
|
"""Does the user have the key (ignoring modules)? Superadmin has every key (D-7)."""
|
|
if user is None:
|
|
return False
|
|
if getattr(user, "role", None) == "superadmin":
|
|
return True
|
|
return key in user_keys(user)
|
|
|
|
|
|
def user_keys(user) -> frozenset[str]:
|
|
rows = getattr(user, "permission_rows", None) or []
|
|
return frozenset(r.key for r in rows)
|
|
|
|
|
|
def can(user, key: str, db=None) -> bool:
|
|
"""Full check without raising: module effective AND user holds the key."""
|
|
from core.modules.state import is_effective
|
|
return is_effective(module_of(key), db) and holds(user, key)
|
|
|
|
|
|
def check(ctx: AccessContext, key: str, db=None) -> None:
|
|
"""Raise the right XeniaError unless `key` is allowed for this request."""
|
|
from core.modules.state import blocked_error, is_effective
|
|
module = module_of(key)
|
|
if not is_effective(module, db):
|
|
raise blocked_error(module, db)
|
|
if ctx.device is not None:
|
|
from core.access.context import in_scope
|
|
if not in_scope(ctx.device.app_key, key):
|
|
raise XeniaError("device.out_of_scope", app=ctx.device.app_key, key=key)
|
|
if holds(ctx.user, key) or key in ctx.override_keys:
|
|
return
|
|
raise XeniaError("access.denied", key=key)
|