Repository navigation
feat(auth): portal_delegate key scope + single-route fence (ent#163) - #1869
Merged
Merged
Conversation
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
approved these changes
Jul 29, 2026
vybe
left a comment
Contributor
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 asPORTAL_SESSION_SCOPE(#78) andusers.suspended_at(#995).Pairs with
trinity-enterprise#164. Land this first — the enterprise endpoint readsUser.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_userconfines 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_adminalone 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}, soagent/connector/systemkeys — 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_keynow setsis_active=1explicitly instead of relying on a column default that exists in only one of the two schema sources:schema.pydeclaresis_active INTEGER DEFAULT 1, butdb/tables.py(the Core/Alembic source) declares a bareColumn("is_active", Integer). A table built from the metadata yieldsNULL, andvalidate_mcp_api_keytreats falsyis_activeas 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 latenttables.py↔schema.pyparity 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_delegatedefaults toFalse(absence must mean "no"), the db creatable-set excludes agent-bound scopes.Behavioural, driving the real
get_current_userwith a delegate key owned by an admin (the worst case — if the fence leaks, the caller is an admin principal):userkey is untouched on the very paths the delegate key is refusedVerified the behavioural half fails (5 tests) with the fence disabled, so it guards something.
Verification
test_163_portal_delegate_scope.py— 19 passedtest_1310_auth_wiring,test_1310_auth_consolidation,test_186_enumeration_uniformity,test_models_centralized) — 103 passed-k "mcp or key or auth or portal"selection — 692 passed, 4 skippedScope
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.scopeis an existing free-text column.Related to trinity-enterprise#163