Files

67 lines
3.0 KiB
Python

"""Credential scopes: what a caller that *acts for* someone may do.
A scope only ever removes: the caller keeps the route permissions it holds
that the scope lists — all of them for ``full``, safe methods (GET/HEAD) only
for ``read`` — and every feature permission it has (those decide what a run
*gets*, and a run a CLI call starts must get what its person's runs get).
============= ================================================== =====================
scope who holds it narrows
============= ================================================== =====================
cli a guest run's ``ai-agent`` CLI (the guard's that guest
broker), a guest's AI-app (MCP) connection
worker.read an MCP connection bound to a worker, read mode the owner, + one profile
worker.run the same, run mode the owner, + one profile
bot the ``READONLY_USERS`` Authelia account (the the owner
screenshot skills' ``claude`` bot)
phone ``READONLY_API_KEY`` (the desk phone) the owner
metrics ``METRICS_TOKEN`` (Prometheus' scrape) the owner
============= ================================================== =====================
The ``bot`` reads what the Admin role reads, minus people, security and the
machine — no guest's records, no security log, no Claude logins.
"""
from __future__ import annotations
import dataclasses
from authz import catalog as cat
SAFE_METHODS = frozenset({"GET", "HEAD", "OPTIONS"})
@dataclasses.dataclass(frozen=True)
class Scope:
name: str
full: frozenset = frozenset()
read: frozenset = frozenset()
# Mutating methods refused outright, whatever the permission.
read_only: bool = False
SCOPES: dict[str, Scope] = {s.name: s for s in (
Scope("cli",
full=frozenset({"conversations.read", "conversations.edit", "runs.start",
"inbox.read", "cron.own", "plugins.use"}),
read=frozenset({"whoami", "app.read", "projects.read"})),
Scope("worker.read",
full=frozenset({"conversations.read", "conversations.past"}),
read=frozenset({"app.read", "workers.read", "usage.read"})),
Scope("worker.run",
full=frozenset({"conversations.read", "conversations.past", "conversations.edit",
"runs.start"}),
read=frozenset({"app.read", "workers.read", "usage.read"})),
Scope("bot", read=(cat.ADMIN_PERMS - {"users.manage", "security.manage"}),
read_only=True),
Scope("phone", read=frozenset({"notifications.ledger"}), read_only=True),
Scope("metrics", read=frozenset({"metrics.read"}), read_only=True),
)}
def narrow(perms: frozenset[str], scope: Scope) -> tuple[frozenset[str], frozenset[str]]:
"""``(full, read_only)`` permission sets of a caller holding ``perms``
through ``scope``."""
features = perms & cat.FEATURE_PERMS
return (features | (perms & scope.full)), (perms & scope.read)