Skip to content

feat(auth): portal_delegate key scope + single-route fence (ent#163) - #1869

Merged
vybe merged 2 commits into
devfrom
feat/163-portal-delegate-scope
Jul 29, 2026
Merged

vybe merged 2 commits into
devfrom
feat/163-portal-delegate-scope

Conversation

@dolho

@dolho dolho commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

The OSS half of delegated portal identity (trinity-enterprise#163). Core owns the scope, its containment, and the admin-only mint; the entitled module owns the endpoint that decides whether a given email may be delegated — the same split as PORTAL_SESSION_SCOPE (#78) and users.suspended_at (#995).

Pairs with trinity-enterprise#164. Land this first — the enterprise endpoint reads User.portal_delegate.

What and why

A licensee wants to run their own customer portal with their own IdP, using Trinity as the agent backend. Their server holds one API key and needs to say "this request is for [email protected]".

Why a new scope instead of reusing scope='user': the capability lets its holder act as another person and read their conversations. Riding it on an ordinary user key would silently turn every user key into a fleet-wide impersonation key.

The fence is the load-bearing part. A delegate key resolves to the key owner, exactly like every other MCP key — so unfenced it would simply be an admin's credential handed to a third party. get_current_user confines it to a single (method, path) at the one auth entry point, mirroring the connector fence (ent#46): centrally, not in the portal router, because the many endpoints that do inline access checks resolve this principal to the owner and would otherwise treat it as that human.

Deliberately one route, not a prefix — the minted portal session (not this key) drives the portal surface afterwards, so the key never needs breadth, and a prefix would silently grant every portal endpoint added in future.

Minting is admin AND human-only. assert_admin alone is insufficient: it rejects connector principals but not agent-scoped ones, and an agent key resolves to its owner carrying the owner's role, which on a default admin-owned install passes a bare role check (trinity-ops-agent#232). The db layer independently refuses any scope outside {user, portal_delegate}, so agent/connector/system keys — which carry an agent binding and are minted by their own paths — can never be forged through this endpoint (validate at the boundary and at the sink).

Incidental fix

create_mcp_api_key now sets is_active=1 explicitly instead of relying on a column default that exists in only one of the two schema sources: schema.py declares is_active INTEGER DEFAULT 1, but db/tables.py (the Core/Alembic source) declares a bare Column("is_active", Integer). A table built from the metadata yields NULL, and validate_mcp_api_key treats falsy is_active as revoked — so every minted key would be born invalid. Harmless on both live schema paths; surfaced because it broke a test built from the metadata. Flagging it as a latent tables.py↔schema.py parity gap worth a separate sweep.

Tests — tests/unit/test_163_portal_delegate_scope.py (19)

Structural: the fence is exactly one route (not a prefix), User.portal_delegate defaults to False (absence must mean "no"), the db creatable-set excludes agent-bound scopes.

Behavioural, driving the real get_current_user with a delegate key owned by an admin (the worst case — if the fence leaks, the caller is an admin principal):

  • accepted on the exchange route, and still resolves to the owner (which is why the endpoint branches on the flag, not the user)
  • 403 on the fleet list, the user list, key minting, the portal surface itself, and the right path with the wrong method
  • an ordinary user key is untouched on the very paths the delegate key is refused

Verified the behavioural half fails (5 tests) with the fence disabled, so it guards something.

Verification

  • test_163_portal_delegate_scope.py — 19 passed
  • auth-surface suites (test_1310_auth_wiring, test_1310_auth_consolidation, test_186_enumeration_uniformity, test_models_centralized) — 103 passed
  • wider -k "mcp or key or auth or portal" selection — 692 passed, 4 skipped
  • enterprise-docs guard run locally — clean (the doc addition names the generic primitive only, never the module)

Scope

dependencies.py (scope + fence), models.py (User.portal_delegate), db_models.py + db/mcp_keys.py (optional scope at mint), routers/mcp_keys.py (admin+human gate), architecture.md (scope table row + the DDL comment). No migration — mcp_api_keys.scope is an existing free-text column.

Related to trinity-enterprise#163

dolho added 2 commits July 29, 2026 12:05
The OSS half of delegated portal identity: the scope, its containment, and the
admin-only mint. The endpoint that decides *whether* an email may be delegated
stays in the entitled module — core owns the enforcement primitive only, the
same split as `PORTAL_SESSION_SCOPE` (#78) and `users.suspended_at` (#995).

Why a new scope rather than reusing `scope='user'`: the capability lets its
holder act as another person and read their conversations. Riding it on an
ordinary user key would silently turn EVERY user key into a fleet-wide
impersonation key. It is admin-issued, listed and revocable like any other key,
and revoking it stops delegation on the next request.

The fence is the load-bearing part. A delegate key resolves to the KEY OWNER,
exactly like every other MCP key, so unfenced it would simply BE an admin's
credential handed to a third party. `get_current_user` confines it to one
(method, path) — the exchange route — at the single auth entry point, mirroring
the connector fence (ent#46): centrally, not in the portal router, because the
many endpoints doing inline access checks resolve this principal to the owner
and would otherwise treat it as that human.

Deliberately ONE route, not a prefix: the minted portal session — not this key —
drives the portal surface afterwards, so the key never needs breadth, and a
prefix would silently grant every portal endpoint added in future.

Minting is admin AND human-only. `assert_admin` alone is insufficient: it
rejects connector principals but not agent-scoped ones, and an agent key
resolves to its owner carrying the owner's role, which on a default admin-owned
install passes a bare role check (trinity-ops-agent#232). The db layer
independently refuses any scope outside `{user, portal_delegate}` so
agent/connector/system keys — which carry an agent binding and are minted by
their own paths — can never be forged through this endpoint.

Incidental fix in the same function: `create_mcp_api_key` now sets `is_active=1`
explicitly instead of relying on a column default that exists in only one of the
two schema sources — `schema.py` declares `is_active INTEGER DEFAULT 1` but
`db/tables.py` (the Core/Alembic source) declares a bare `Column(...)`. A table
built from the metadata yields NULL, and `validate_mcp_api_key` treats falsy
`is_active` as revoked, so every minted key would be born invalid. Harmless on
both live schema paths; found because it broke a test built from the metadata.

Tests: `test_163_portal_delegate_scope.py` — structural (the fence is one route,
the model defaults to False, the db creatable-set excludes agent scopes) plus
behavioural, driving the real `get_current_user`: a delegate key is accepted on
the exchange route and 403s on the fleet, the user list, key minting, the portal
surface itself, and the right path with the wrong method. Verified the
behavioural half fails (5 tests) with the fence disabled.

103 passed across the auth-surface suites; 692 passed over the wider
key/auth/portal selection. Enterprise-docs guard clean.

Related to trinity-enterprise#163
The issue's AC asks for the scope to be "admin-issued, listed and revocable in
Settings → MCP Keys". Listing and revoking already worked — but a
`portal_delegate` key rendered with NO badge, falling through the
`agent`/`system` v-if chain and looking exactly like an ordinary user key. For a
credential that can act as any end user with portal access, that is misleading
in precisely the place an admin audits keys. And there was no way to create one
from the UI at all, so "admin-issued" meant "curl only".

Two small additions:

* an amber **Portal Delegate** badge, deliberately distinct from the purple
  Agent / red System badges, with a title explaining what the key can do
* an admin-only checkbox on the create modal (`v-if="isAdmin"`), opt-in rather
  than a default — minting a key that acts as other people should be a
  conscious choice, never a stray click. The scope is omitted entirely (not sent
  as null) for an ordinary key, so nothing changes for existing callers.

Verified live: badge renders on both delegate keys; the checkbox appears for an
admin and mints a working `scope=portal_delegate` key. The end-to-end flow was
driven against local dev — mint → fenced (403 on /api/agents, /api/users,
/api/mcp/keys) → exchange for two different end users from ONE key → disjoint
rosters (bob→[evt-boss], carol→[evt-worker]) → unauthorized email 403 → revoke →
401.

Related to trinity-enterprise#163

@vybe vybe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OSS seam for ent#163: portal_delegate key scope + single-route fence. Scope validated in dependencies.py at the single auth entry point (connector-fence pattern), unit test present (test_163_portal_delegate_scope.py), architecture.md updated. Security greps clean.

@vybe
vybe merged commit dc4f490 into dev Jul 29, 2026
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants