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

74 lines
2.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""The route × caller matrix: who reaches what, as text.
``test_authz_matrix.py`` compares :func:`render` with the checked-in
``authz/matrix.golden.txt``; a change of who may call a route is then a diff
someone reads in review. Rewrite the file with
``UPDATE_GOLDEN=1 python -m unittest test_authz_matrix``.
It uses the built-in roles' *defaults* (``roles.BUILTIN``), not this
install's edits — the matrix describes the code. Handlers still filter by
relation inside a route (whose conversation, which project's grant), so a ✓
means "the gate lets them in", not "they see everything there".
"""
from __future__ import annotations
import dataclasses
from authz import catalog as cat
from authz import roles
from authz.routes import TABLE, PUBLIC, iter_routes
from authz.scopes import SAFE_METHODS
def _person(rid: str):
from users.caller import Caller
return Caller(id=rid, role="guest", name=rid,
perms=frozenset(roles.BUILTIN[rid]["perms"]) | cat.CORE, role_id=rid)
def callers() -> list[tuple[str, object]]:
"""(column, caller) — None is the anonymous caller."""
from users.caller import OWNER
guest = _person(roles.GUEST)
owner_ro = dataclasses.replace(OWNER, readonly=True)
return [
("anon", None),
("viewer", _person(roles.VIEWER)),
("guest", guest),
("member", _person(roles.MEMBER)),
("cli", guest.narrowed("cli")),
("w.read", dataclasses.replace(OWNER, via_worker="w", profile="p").narrowed("worker.read")),
("w.run", dataclasses.replace(OWNER, via_worker="w", profile="p").narrowed("worker.run")),
("bot", owner_ro.narrowed("bot")),
("phone", owner_ro.narrowed("phone")),
("owner", OWNER),
]
def decide(caller, perm: str | None, method: str) -> bool:
if caller is None:
return perm == PUBLIC
if caller.readonly and method not in SAFE_METHODS:
return False
return caller.allows(perm, method)
def render(app) -> str:
cols = callers()
rows = []
for path, r in iter_routes(app.routes):
if not path.startswith("/api/"):
continue
for m in sorted(r.methods or ()):
perm = TABLE.get((m, path))
marks = "".join("✓" if decide(c, perm, m) else "·" for _, c in cols)
rows.append((path, m, perm or "UNMAPPED", marks))
rows.sort()
head = [f"# who reaches each API route — authz/matrix.py (columns: "
f"{' '.join(name for name, _ in cols)})", ""]
width = max(len(f"{m} {p}") for p, m, _, _ in rows)
pw = max(len(perm) for _, _, perm, _ in rows)
return "\n".join(head + [f"{(m + ' ' + p).ljust(width)} {perm.ljust(pw)} {marks}"
for p, m, perm, marks in rows]) + "\n"