Files
gabrielvidalandClaude Opus 5.5 c7da0f5008 feat(authz): roles, scoped credentials and one permission table for every route
- authz/catalog.py: 42 named permissions in groups and tiers (person, admin,
  host); authz/roles.py: built-in Operator (the owner), Admin (assignable with
  team mode), Member, Guest, Viewer + custom clones, in roles.json
- a person = role + allow/deny overrides (authz/policy.py); users.json's old
  switches (cli, mcp, createProjects, scratchBash, codeMode,
  computers.enabled) migrate once to exactly what they allowed, backup kept
- authz/scopes.py: what a credential acting for someone keeps (cli,
  worker.read|run, bot, phone) — a scope only narrows
- authz/routes.py: (method, path) -> permission for all 310 /api routes; the
  gate asks Caller.allows() for every caller kind, replacing GUEST_ROUTES,
  CLI_ROUTES and the worker READ/RUN_ROUTES regex lists
- golden route x caller matrix (authz/matrix.golden.txt, test_authz_matrix)
- the claude bot reads the admin app minus people/security/the machine; the
  phone key reads its unread notifications only
- loopback without the host token is logged (loopback_tokenless), refusals
  are perm_denied lines; the hub's own MCP calls send the token
- /api/roles CRUD, /api/me and /api/users carry role/allow/deny/perms
- worker-bound scheduled items and conversation-meta respect the binding

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 17:59:05 +01:00

83 lines
3.3 KiB
Python

"""
The owner, narrowed to one of his workers — what an MCP connection **bound
to a worker** is (Settings → AI apps → a connection → *Acts as*), so a
Claude Code on the Orus MacBook can drive the hub through the MCP server
without seeing the lab's other profiles.
The MCP tools call this backend's own API over loopback (mcpserver/), where a
caller is the owner. A bound connection's calls add :data:`AS_WORKER_HEADER`
(the worker's id) and :data:`AS_WORKER_MODE_HEADER` (``read`` for a read-only
connection, ``run`` for a full one), and the gate (core/auth.py) answers them
as :func:`worker_caller` — a downgrade of a caller already trusted, never an
upgrade:
* it holds the ``worker.read`` or ``worker.run`` credential scope
(authz/scopes.py): reading conversations, workers and plan usage, plus
starting, renaming and scheduling under ``run``;
* reads see the conversations of the worker's own profile only
(users/access.py, ``Caller.profile``);
* writes stay on that worker: a new run (``start_conversation``,
``schedule_conversation``) starts there, and send / fork / stop / rename /
archive / cancel only touch conversations (and scheduled turns) that live
there.
A worker's own sessions don't call the hub at all (it pulls them); their
``ai-agent`` CLI keeps only ``past``, through the outbox
(conversations/past.py ``outbox_lookup``), narrowed the same way.
"""
from __future__ import annotations
import dataclasses
from users.caller import OWNER, Caller
AS_WORKER_HEADER = "x-aia-as-worker"
AS_WORKER_MODE_HEADER = "x-aia-as-worker-mode"
READ, RUN = "read", "run"
MODES = (READ, RUN)
# The fields a bound connection's rename / archive may set.
META_FIELDS = frozenset({"id", "title", "titleSource", "archived"})
class Refused(Exception):
def __init__(self, status: int, detail):
super().__init__(str(detail))
self.status = status
self.detail = detail
def bindable(w: dict | None) -> bool:
"""A connection can be bound to one of the owner's workers — never to a
guest's computer."""
return bool(w) and not w.get("owner")
def worker_caller(deps, wid: str, mode: str | None = READ) -> Caller:
"""The owner, narrowed to worker ``wid``'s profile, that worker's runs and
``mode``'s routes. :class:`Refused` when the worker is unknown or a
guest's."""
w = deps.worker_store.get((wid or "").strip())
if not bindable(w):
raise Refused(403, "unknown worker")
profile = deps.profile_store.for_runner(w["id"])["id"]
mode = mode if mode in MODES else READ
return dataclasses.replace(OWNER, via_worker=w["id"], profile=profile,
worker_mode=mode).narrowed(f"worker.{mode}")
def pin_worker(deps, caller: Caller, worker: str | None) -> str:
"""The runner a bound caller's new run goes to: that worker — unset, or
named by its id or its name. Any other is refused."""
want = (worker or "").strip()
if not want or want == caller.via_worker:
return caller.via_worker
w = deps.worker_store.get(caller.via_worker) or {}
if want.lower() == (w.get("name") or "").strip().lower():
return caller.via_worker
raise Refused(403, {"message": f"this connection starts runs on "
f"{w.get('name') or caller.via_worker} only",
"kind": "worker_elsewhere"})