Skip to content

Connect and disconnect a Bluesky identity with OAuth shared across platforms #674

Closed

Description

@micahflee

Parent

What to build

One shared Bluesky OAuth implementation, used by both the X platform's "migrate to Bluesky" feature and the Bluesky platform, backed by a DID-keyed credential store so that authorizing a Bluesky identity once is enough for every part of Cyd that needs it.

A user can authorize a Bluesky identity through OAuth, refresh its DID-backed profile, and disconnect or reconnect without losing saved data.

Why the sharing matters

Cyd currently performs Bluesky OAuth in exactly one place: the X-to-Bluesky migration, in src/account_x/controller/bluesky/BlueskyService.ts. The Bluesky platform needs the same flow. Standing up a second, parallel implementation would mean two NodeOAuthClient setups, two state/session stores, two callback routes, and two places for a bug like #699 to hide.

It would also be visible to users as a papercut: signing into Bluesky through X's migration wizard and then adding that same Bluesky account as a Bluesky platform account would demand a second browser authorization for an identity Cyd already holds a live session for.

Three concrete things in the current code stand in the way:

  1. Session storage is namespaced per Cyd account, not per Bluesky identity. Entries are keyed by DID (blueskySessionStore-${did}, src/credentials/keys.ts), but they are written into accountCredentials(xAccountID), which resolves to an account-${accountID} vault (src/credentials/store.ts:52). A session authorized under an X account is therefore invisible to a Bluesky local account, even for the same DID.
  2. Disconnect is namespace-wide. BlueskyService.disconnect() calls credentials.deleteWithPrefix(BLUESKY_OAUTH_STATE_PREFIX) and deleteWithPrefix(BLUESKY_OAUTH_SESSION_PREFIX) (BlueskyService.ts:286-287), wiping every Bluesky session in that vault. Against a shared store, that would sign out unrelated accounts.
  3. Callback routing assumes an X account. openCydURL() reads a single global blueskyOAuthAccountID config key and emits blueskyOAuthCallback-${accountID} (src/main.ts:126-141). Only one flow can be in flight, and its dispatch is hardcoded to X's semantics.

Acceptance criteria

Shared OAuth

  • A single Bluesky OAuth module owns client-metadata resolution, NodeOAuthClient construction, the state and session stores, and authorize/callback/restore/refresh. Both the X migration feature and the Bluesky platform consume it; neither reimplements any part of it.
  • Bluesky OAuth sessions live in one DID-keyed credential store shared across platforms, not in per-Cyd-account vaults. The X migration's existing sessions migrate forward into it.
  • A Bluesky identity already holding a valid session connects without a new browser authorization, in both directions: X migration first then Bluesky platform, and Bluesky platform first then X migration.
  • A shared Bluesky session is retained by its holders, and is revoked when and only when its last holder goes away. See "Session lifetime" below.
  • Callback routing is keyed by a flow identifier that resolves to whichever platform started the flow, replacing the single global blueskyOAuthAccountID key and the blueskyOAuthCallback-${accountID} event shape.
  • The callback scheme and path come from one shared constant rather than being spelled out independently (fixes Bluesky OAuth callback never reaches the desktop app: redirect scheme and path do not match what Cyd registers #699, whose remaining hops live in cyd-server and cyd.social). A real end-to-end authorization completes and reaches the app.

Session lifetime

A single authorized DID can be held by more than one thing at once: an X account with the migration attached, and a Bluesky local account for the same identity. Releasing one hold must not sign out the other; releasing the last one must genuinely sign out.

  • A holder is any account that depends on the session: an X account with Bluesky migration connected, or a Bluesky local account bound to that DID.
  • Holders are derived from existing account state rather than tracked in a separate mutable counter, so a crash, a forced quit, or a partial delete cannot leak a hold (leaving a session that can never be revoked) or drop one prematurely (signing out an account that is still connected).
  • Disconnecting X's migration while a Bluesky local account still holds the DID releases only X's hold. The session stays alive and the Bluesky account stays connected, with no re-authorization.
  • Deleting the Bluesky local account while an X account still holds the DID releases only that hold. The X migration stays connected, with no re-authorization.
  • Disconnecting a Bluesky local account, without deleting it, releases its hold the same way.
  • When the last holder releases the DID, Cyd revokes the session at the PDS and deletes all local state, session, and DPoP key material for it. Dropping local material without revoking upstream is not sufficient.
  • Releasing a hold always reflects immediately in that account's own connection state, whether or not the underlying session survives. A user who disconnects X sees X as disconnected.
  • A user is never asked to force-revoke a session another account is still using, and no UI offers it. Each account only ever releases its own hold.
  • A DID holding a session with zero holders — reachable only through interrupted work — is swept and revoked rather than left orphaned.

Connection behavior

  • Authorization uses Bluesky OAuth and direct AT Protocol APIs, never app passwords, scraping, interception, or MITM.
  • Connection binds the authenticated DID and refreshes current profile data; handle changes do not change UUID or storage paths.
  • Disconnect removes authorization but retains the local account and offline saved data.
  • Connection material uses the credential facility from Protect all persisted account credentials from plaintext storage #671 and is excluded from Bluesky account data, Cyd Bluesky archives, and diagnostics, per docs/adr/0029-minimize-bluesky-diagnostics.md and docs/adr/0005-exclude-credentials-from-archives.md.
  • Linux clearly discloses the basic_text fallback when protected credential storage is unavailable.

Tests

  • Controller and mounted-view tests cover success, failure, reconnect, handle change, disconnect, and offline access.
  • Tests cover session reuse across platforms in both directions and concurrent/interleaved authorization flows routing to the correct initiator.
  • Tests cover the full holder matrix for one DID held twice: disconnect X (Bluesky account stays connected), delete the Bluesky account (X stays connected), then release the remaining holder and assert the session is revoked upstream and all local material is gone.
  • Tests assert revocation happens exactly once, at the last release and not before, in either release order.
  • Tests cover a restart interrupting a release, asserting no leaked hold and no premature sign-out.
  • The X migration's existing Bluesky tests continue to pass against the shared module.

Blocked by

Absorbs

(This was written by an LLM.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    ready-for-agent

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions