Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

Merge remote-tracking branch 'origin/controller-socket-calls' into scale-core

Abel Dantasbb564f88e502
diff --cc core/switch_core/management/auth.py
index 2ae92259b,41498defb..7c274cf1b
--- a/core/switch_core/management/auth.py
+++ b/core/switch_core/management/auth.py
@@@ -87,17 -87,33 +87,33 @@@ class ManagementAuthenticator
reason_codes.INVALID_CREDENTIAL,
"The access token is not a valid controller token.",
) from exc
+ return await self.current(
+ ControllerPrincipal(
+ controller_id=claims.controller_id,
+ owner_id=claims.owner_id,
+ tenant_id=claims.tenant_id,
+ )
+ )
- cached = self._auth_cache.controller(claims.tenant_id, claims.controller_id)
- if cached is not None and cached.owner_id == claims.owner_id:
+ async def current(self, principal: ControllerPrincipal) -> ControllerPrincipal:
+ """The controller `principal` names, while it keeps the authority its
+ token gave it: not revoked, and owned by the same person. Every request
+ made with a controller access token is checked here, and every call on
+ a controller's socket, which authenticated once when it opened."""
+ cached = self._auth_cache.controller(
+ principal.tenant_id, principal.controller_id
+ )
+ if cached is not None and cached.owner_id == principal.owner_id:
return cached
- generation = self._auth_cache.generation
+ ticket = self._auth_cache.ticket()
- async with tenant_session(self._session_factory, claims.tenant_id) as session:
+ async with tenant_session(
+ self._session_factory, principal.tenant_id
+ ) as session:
controller = await self._controllers.get(
- session, claims.tenant_id, claims.controller_id
+ session, principal.tenant_id, principal.controller_id
)
- if controller is None or controller.owner_id != claims.owner_id:
+ if controller is None or controller.owner_id != principal.owner_id:
raise ControllerAuthError(
reason_codes.INVALID_CREDENTIAL,
"The access token names no controller.",
@@@ -106,13 -122,13 +122,13 @@@
raise ControllerAuthError(
reason_codes.CONTROLLER_REVOKED, "The controller has been revoked."
)
- principal = ControllerPrincipal(
+ current = ControllerPrincipal(
controller_id=controller.id,
owner_id=controller.owner_id,
- tenant_id=claims.tenant_id,
+ tenant_id=principal.tenant_id,
)
- self._auth_cache.put_controller(principal, ticket)
- return principal
- self._auth_cache.put_controller(current, generation)
++ self._auth_cache.put_controller(current, ticket)
+ return current
async def tenant_of_secret(self, secret: str) -> str | None:
"""The tenant an enrollment code or controller credential belongs to,