Skip to content

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

Let stable Console link chat accounts: show the code in the claim's 409 (CHOO-4119) (#1547)

Christian McDermott26137956dc20
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3a5fd40b1..a6ab3cfc2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -617,6 +617,16 @@ version of their own to them without also giving them a release of their own.
token again; revocation is still checked on every request.
#### Fixed
+- **Stable Switch Console can link chat accounts again.** Since links have to
+ be confirmed from the account with a one-time code, a claim from a Console
+ that cannot show the code was refused with "Update Switch Console", and no
+ stable release could show it, so nobody on stable could link a new account.
+ Switch now issues the code anyway and returns it as the 409's text, which
+ stable Switch Console shows in the link dialog: what to post from the
+ account and that the code expires in 30 minutes. An account already
+ confirmed links normally. An admin linking someone else from an older client
+ is still refused while the link needs confirming, since the code would be
+ shown to the admin.
- **A file that names nobody now follows the conversation like text does.**
A post with one or more files and no mention never reached the room's
assigned agent, the agent whose thread it was posted in, or the agent the
diff --git a/console/apps/switch-console-desktop/src/main/core/switch-servers/gateway-client.ts b/console/apps/switch-console-desktop/src/main/core/switch-servers/gateway-client.ts
index 9d1a235f3..babb17191 100644
--- a/console/apps/switch-console-desktop/src/main/core/switch-servers/gateway-client.ts
+++ b/console/apps/switch-console-desktop/src/main/core/switch-servers/gateway-client.ts
@@ -2386,9 +2386,9 @@ export async function searchBridgeDirectory(
* with that status.
*
* A current server answers with the link still pending and a code to post from
- * the account (`link_request`); see {@link issueIdentityLinkCode}. It refuses a
- * claim that does not say it can show that code (`expects_confirmation`), since
- * a client that cannot would leave the link pending with no way to finish it.
+ * the account (`link_request`); see {@link issueIdentityLinkCode}. A claim that
+ * does not say it can show that code (`expects_confirmation`) gets it as the
+ * text of a 409 instead, which is all a Console from before then shows.
*/
export async function claimBridgeIdentity(
server: SwitchServer,
diff --git a/core/switch_core/gateway/collaborations.py b/core/switch_core/gateway/collaborations.py
index c8b584707..b95bcb87b 100644
--- a/core/switch_core/gateway/collaborations.py
+++ b/core/switch_core/gateway/collaborations.py
@@ -852,7 +852,8 @@ async def claim_bridge_identity(
(CHOO-3881), and counts for nothing before then: who an account is
recognised as is for the account's holder to say. A claim for yourself
comes back with the code; one an admin makes for someone else does not,
- and the person asks for their own.
+ and the person asks for their own. A Console from before confirmation is
+ given its code as the text of a 409, the one part of the answer it shows.
Claiming one for someone else is an admin action; claiming one for
yourself is not. Claims are **not exclusive** — several Switch users may
@@ -865,16 +866,6 @@ async def claim_bridge_identity(
# session holds, the caller included, and an expired row cannot be
# reloaded from synchronous attribute access.
caller_id = user.id
- if not payload.expects_confirmation:
- raise HTTPException(
- status_code=409,
- detail=(
- "Linking a chat account now has to be confirmed from that "
- "account with a one-time code, and this version of Switch "
- "Console cannot show one. Update Switch Console, then link the "
- "account again."
- ),
- )
bridge = await bridge_store.get(session, bridge_id)
if bridge is None:
raise HTTPException(status_code=404, detail="Bridge not found")
@@ -882,6 +873,11 @@ async def claim_bridge_identity(
target_user_id = _require_self_or_admin(
user, is_admin, payload.user_id, "claim a messaging identity"
)
+ # An older client is shown the code in place of a refusal, and a code is
+ # only ever shown to the person it links.
+ older_client_for_someone_else = (
+ not payload.expects_confirmation and target_user_id != caller_id
+ )
# Standing in this tenant, not existence: `users` is deployment-wide, and
# a claim names who the account is recognised as inside this tenant. An
# organization's `org_admin` stands in every workspace of it.
@@ -902,6 +898,10 @@ async def claim_bridge_identity(
session, bridge_id, payload.external_user_id
)
if external_user is None:
+ if older_client_for_someone_else:
+ # An account never seen has nothing confirmed to answer with, so
+ # refuse before provisioning leaves a row behind.
+ raise _older_client_linking_someone_else()
# Nobody has seen this person speak yet, which is the normal case right
# after connecting a workspace. Provision the identity now rather than
# making them post something first — but only once the platform agrees
@@ -951,6 +951,9 @@ async def claim_bridge_identity(
session, external_user_store, user_store, external_user, None
)
+ if older_client_for_someone_else:
+ raise _older_client_linking_someone_else()
+
request = await external_user_store.request_link(
session, external_user, target_user_id, requested_by_user_id=caller_id
)
@@ -989,6 +992,8 @@ async def claim_bridge_identity(
details={"bridge_id": bridge_id, "user_id": target_user_id},
)
await session.commit()
+ if code is not None and not payload.expects_confirmation:
+ raise HTTPException(status_code=409, detail=_code_as_refusal_text(code))
return await _identity_summary(
session,
@@ -999,6 +1004,36 @@ async def claim_bridge_identity(
)
+def _older_client_linking_someone_else() -> HTTPException:
+ return HTTPException(
+ status_code=409,
+ detail=(
+ "Linking a chat account now has to be confirmed from that "
+ "account with a one-time code, and this version of Switch "
+ "Console cannot show one. Update Switch Console, then link the "
+ "account again."
+ ),
+ )
+
+
+def _code_as_refusal_text(code: LinkCode) -> str:
+ """A link code worded as the next step, for a Console from before links
+ had to be confirmed.
+
+ Those Consoles ignore the code in a claim's response but show a 409's
+ detail in the link dialog, so the code travels there. They show it as
+ plain text, so the instructions lose their backticks: a command copied
+ with one would not start with its prefix, and the bridge would not read
+ it as a command.
+ """
+ minutes = code.expires_in_seconds // 60
+ return (
+ "One more step: confirm this account is yours. "
+ f"{code.instructions.replace('`', '')} "
+ f"The code expires in {minutes} minutes."
+ )
+
+
async def _issue_code(
session: AsyncSession,
external_user_store: ExternalUserStore,
diff --git a/core/switch_core/gateway/schemas.py b/core/switch_core/gateway/schemas.py
index 2d2397127..5d053bbe9 100644
--- a/core/switch_core/gateway/schemas.py
+++ b/core/switch_core/gateway/schemas.py
@@ -839,8 +839,10 @@ class ClaimIdentityRequest(BaseModel):
user_id: str | None = None
# That the caller knows the link will wait for the account to confirm it
# (CHOO-3881). A Console from before then leaves it out and would report
- # a link that does not count as made, so the claim is refused with a
- # reason it shows instead.
+ # a link that does not count as made, so its claim is answered with a 409
+ # whose text carries the code, which it shows. One for someone else is
+ # refused while the link needs confirming, since that text would show the
+ # code to the wrong person.
expects_confirmation: bool = False
diff --git a/core/tests/switch_core/gateway/test_identity_link_confirmation.py b/core/tests/switch_core/gateway/test_identity_link_confirmation.py
index d2bab61b5..88509f845 100644
--- a/core/tests/switch_core/gateway/test_identity_link_confirmation.py
+++ b/core/tests/switch_core/gateway/test_identity_link_confirmation.py
@@ -8,6 +8,7 @@ every owner check trusts.
from __future__ import annotations
+import re
import uuid
from collections.abc import Awaitable, Callable
from datetime import datetime
@@ -224,6 +225,12 @@ async def _claim_of(world: _World, user: User) -> ExternalUserClaim | None:
return await _STORE.get_claim(session, world.account.id, user.id)
+def _code_in(text: str) -> str:
+ found = re.search(r"!link-account (\w{4}-\w{4})", text)
+ assert found is not None, text
+ return found.group(1)
+
+
class TestClaimWaitsForTheAccount:
async def test_a_claim_for_yourself_comes_back_with_a_code(
self, world: _World
@@ -269,13 +276,104 @@ class TestClaimWaitsForTheAccount:
assert refused.value.status_code == 404
- async def test_a_console_from_before_confirmation_is_told_to_update(
+ async def test_a_stand_in_account_cannot_be_linked(self, world: _World) -> None:
+ with pytest.raises(HTTPException) as refused:
+ await _claim(world, world.ada, external_user_id=_STAND_IN)
+
+ assert refused.value.status_code == 409
+ assert "Many people post as it." in refused.value.detail
+
+
+class TestAConsoleFromBeforeConfirmation:
+ """A Console from before confirmation leaves `expects_confirmation` out and
+ ignores a claim's code, but shows a 409's detail in its link dialog."""
+
+ async def test_it_is_shown_the_code_as_the_next_step(self, world: _World) -> None:
+ with pytest.raises(HTTPException) as answered:
+ await _claim(world, world.ada, expects_confirmation=False)
+
+ assert answered.value.status_code == 409
+ detail = answered.value.detail
+ assert f"On slack, post !link-account {_code_in(detail)}." in detail
+ assert "`" not in detail
+ assert "30 minutes" in detail
+ assert await _claim_of(world, world.ada) is None
+
+ async def test_the_code_it_was_shown_confirms_the_link(self, world: _World) -> None:
+ with pytest.raises(HTTPException) as answered:
+ await _claim(world, world.ada, expects_confirmation=False)
+
+ async with world.session_factory() as session:
+ confirmed = await _STORE.redeem_link_code(
+ session, world.account, _code_in(answered.value.detail), UserStore()
+ )
+ await session.commit()
+ assert confirmed is not None
+ assert confirmed.user_id == world.ada.id
+ claim = await _claim_of(world, world.ada)
+ assert claim is not None and claim.verified_at is not None
+
+ async def test_an_account_already_confirmed_is_claimed_normally(
+ self, world: _World
+ ) -> None:
+ summary = await _claim(world, world.ada)
+ async with world.session_factory() as session:
+ assert await _STORE.redeem_link_code(
+ session,
+ world.account,
+ summary.link_request.code.code, # type: ignore[attr-defined]
+ UserStore(),
+ )
+ await session.commit()
+
+ again = await _claim(world, world.ada, expects_confirmation=False)
+
+ assert again.link_request is None # type: ignore[attr-defined]
+ assert [
+ (c.user_id, c.verified)
+ for c in again.claimed_by # type: ignore[attr-defined]
+ ] == [(world.ada.id, True)]
+
+ async def test_an_admin_relinking_someone_already_confirmed_is_answered(
+ self, world: _World
+ ) -> None:
+ summary = await _claim(world, world.ada)
+ async with world.session_factory() as session:
+ assert await _STORE.redeem_link_code(
+ session,
+ world.account,
+ summary.link_request.code.code, # type: ignore[attr-defined]
+ UserStore(),
+ )
+ await session.commit()
+
+ again = await _claim(
+ world,
+ world.bob,
+ for_user=world.ada,
+ is_admin=True,
+ expects_confirmation=False,
+ )
+
+ assert again.link_request is None # type: ignore[attr-defined]
+ assert [
+ (c.user_id, c.verified)
+ for c in again.claimed_by # type: ignore[attr-defined]
+ ] == [(world.ada.id, True)]
+
+ async def test_an_admin_linking_someone_else_is_told_to_update(
self, world: _World
) -> None:
- # It would otherwise drop the code and report a link that counts for
- # nothing as made.
+ # The code belongs to the person being linked, and this response would
+ # show it to the admin instead.
with pytest.raises(HTTPException) as refused:
- await _claim(world, world.ada, expects_confirmation=False)
+ await _claim(
+ world,
+ world.bob,
+ for_user=world.ada,
+ is_admin=True,
+ expects_confirmation=False,
+ )
assert refused.value.status_code == 409
assert "Update Switch Console" in refused.value.detail
@@ -285,12 +383,26 @@ class TestClaimWaitsForTheAccount:
is None
)
- async def test_a_stand_in_account_cannot_be_linked(self, world: _World) -> None:
+ async def test_an_admin_linking_an_unseen_account_provisions_nothing(
+ self, world: _World
+ ) -> None:
with pytest.raises(HTTPException) as refused:
- await _claim(world, world.ada, external_user_id=_STAND_IN)
+ await _claim(
+ world,
+ world.bob,
+ external_user_id="U-unseen",
+ for_user=world.ada,
+ is_admin=True,
+ expects_confirmation=False,
+ )
assert refused.value.status_code == 409
- assert "Many people post as it." in refused.value.detail
+ assert "Update Switch Console" in refused.value.detail
+ async with world.session_factory() as session:
+ provisioned = await session.scalar(
+ select(ExternalUser).where(ExternalUser.external_user_id == "U-unseen")
+ )
+ assert provisioned is None
class TestIssuingACode: