Skip to content

Share one Bluesky OAuth implementation across platforms - #704

Merged
micahflee merged 5 commits into
mainfrom
issue-674-shared-bluesky-oauth
Sep 13, 2026
Merged

micahflee merged 5 commits into
mainfrom
issue-674-shared-bluesky-oauth

Conversation

@micahflee

@micahflee micahflee commented Sep 11, 2026 •

Copy link
Copy Markdown
Member

Closes #674. Absorbs and closes #699.

Why

Cyd did Bluesky OAuth in exactly one place — the X-to-Bluesky migration — and the Bluesky platform needed the same flow. A second client would have meant two NodeOAuthClient setups, two credential stores, two callback routes, and a second browser sign-in for an identity Cyd already held a live session for.

What changed

One module. src/bluesky_oauth/ owns client-metadata resolution, the state and session stores, and authorize/callback/restore/release. Both platforms consume it; neither reimplements any part of it.

One DID-keyed store. Sessions move out of account-${id} vaults into a shared shared-bluesky-oauth vault, so an identity authorized through the migration wizard connects as a Bluesky account with no second authorization — and the reverse. Every account's existing sessions are carried forward at startup, before anything asks who holds one.

Holders, not a counter. A shared session is kept alive by its holders: an X account with the migration connected, or a connected Bluesky local account on that DID. Holders are derived from account state on every question, so a crash or a forced quit cannot leak a hold or drop one early. Releasing one hold leaves the others signed in; the last release revokes at the PDS and destroys every local trace. A session left with no holders by interrupted work is swept at startup. No account can force-revoke a session another still uses, and no UI offers to. See docs/adr/0034.

Flow-keyed callbacks. The flow identifier (X:7, Bluesky:12) rides inside the OAuth request as its application state, so it survives a browser round trip and a restart, and interleaved authorizations each return to their own initiator. This replaces the single global blueskyOAuthAccountID config key and the blueskyOAuthCallback-${accountID} event shape.

One spelling of the callback scheme. forge.config.ts registers the scheme with the operating system for the packaged .deb and .rpm, and the app matches incoming callbacks against it. Those two spelling it separately is what #699 was; both now derive it from blueskyOAuthCallbackSchemeForMode(), with a test asserting they agree across every mode. The mode is a parameter because the packaging config reads CYD_ENV at build time and the app reads CYD_MODE at runtime.

Bluesky connect page. The platform gains a connect wizard page: authorize a handle, refresh the identity's profile, disconnect without losing the local account or any Bluesky saved data.

Schema

One forward migration adds blueskyLocalAccount.connectedAt. A Bluesky local account keeps its DID after disconnecting — that DID is how an archive import recognizes the identity again — so whether Cyd is authorized is tracked separately.

BlueskyLocalAccountPaths.connectionPath and the connection/ directory it created are removed. They were reserved for per-account connection material that the shared DID-keyed store makes unnecessary.

Testing

npm run lint clean; 1435 tests passing. New coverage: the shared store and its forward migration, flow identifiers and interleaved callback routing, packaging-vs-app scheme parity, the full holder matrix in both release orders, revocation exactly once and not before, a restart interrupting a release, a sweep surviving one unreadable account, session reuse in both directions, and the Bluesky controller and connect page across success, failure, reconnect, handle change, disconnect, and offline access.

Not verified here

A real end-to-end authorization completes and reaches the app.

The remaining hops for #699 live in cyd-server and cyd.social, outside this repo. Cyd now derives its registered redirect URI, its OS scheme registration, and its callback matcher from the same two constants, but the actual round trip needs a manual run against a dev server — and the server-side spelling has to match social.cyd.dev-api:/atproto-oauth-callback/.

Worth a look during review

mockElectronAPI() in test_util.ts was silently dropping the DOM event constructors when it spread window, so no mounted-component test could type into a form or submit one. Restoring window.Event there touches every renderer test, though all pass.

🤖 Generated with Claude Code

micahflee and others added 3 commits September 11, 2026 12:43
Cyd did Bluesky OAuth in exactly one place, the X-to-Bluesky migration, and
the Bluesky platform needed the same flow. Standing up a second client would
have meant two NodeOAuthClient setups, two credential stores, two callback
routes, and a second browser sign-in for an identity Cyd already held a live
session for.

There is now one module, src/bluesky_oauth, that owns client-metadata
resolution, the state and session stores, authorize, callback, restore, and
release. Both platforms consume it and neither reimplements any part of it.

Sessions move out of per-account vaults into one DID-keyed shared store, so an
identity authorized through the migration wizard connects as a Bluesky account
with no second authorization, and the reverse. Sessions an account already
holds are carried forward when its database opens.

A shared session is kept alive by its holders - an X account with the
migration connected, or a connected Bluesky local account on that DID - which
are derived from account state on every question rather than counted, so a
crash cannot leak a hold or drop one early. Releasing one hold leaves the
others signed in; the last release revokes at the PDS and destroys every local
trace. A session left with no holders by interrupted work is swept at startup.
No account can force-revoke a session another still uses, and no UI offers to.

Callback routing is keyed by a flow identifier carried inside the OAuth
request, replacing the single global blueskyOAuthAccountID key and the
blueskyOAuthCallback-${accountID} event shape, so interleaved authorizations
each return to their own initiator. The callback scheme and path come from one
shared constant, fixing Cyd's half of #699.

The Bluesky platform gains a connect wizard page: authorize a handle, refresh
the identity's profile, and disconnect without losing the local account or any
Bluesky saved data.

Closes #674
Closes #699

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Delete the dead connection directory. BlueskyLocalAccountPaths.connectionPath
was reserved for per-account connection material that the shared DID-keyed
store made unnecessary, so it is removed rather than documented as dead.

forge.config.ts still spelled the callback scheme out by hand for the packaged
.deb and .rpm scheme handlers, which is the second independent spelling #699
was about. Both it and the app now derive the scheme from one function; the
mode is passed in because the packaging config reads CYD_ENV at build time and
the app reads CYD_MODE at runtime.

Carry every account's Bluesky OAuth credentials forward at startup rather than
only when an X account's database opens. Someone who upgraded and added a
Bluesky account before opening the X account that authorized the identity was
still sent back to a browser for a session Cyd already held.

An X account whose database cannot be read deliberately refuses to say whether
it holds an identity, so a live session is never revoked on a guess. That
refusal was aborting the whole orphan sweep; it is now caught per identity, so
one unreadable account cannot disable the sweep for every other DID.

Reusing an existing session in the X migration now binds the identity and
confirms its profile reads, the same as finishing a browser authorization.

A callback Cyd holds no state for now says so instead of doing nothing
visible.

Remove connectWithExistingSession and blueskyIdentityIsHeld: reuse already
happens inside authorizeBlueskyIdentity, and neither had a caller.

Give BlueskyHolder its own type instead of aliasing BlueskyOAuthFlow, which
CONTEXT.md defines as a separate concept, and move the X-side holder
enumeration into account_x so the shared module asks each platform about its
own accounts rather than reaching into their shapes.

Spell "blueskyDID" once, in credentials/keys.ts, beside the credential names
it points at. The legacy sweep, the migration service, and the holder
derivation sit in three layers and all have to agree on it.

Stop naming an account in the credential-migration log line, and log only the
error class when an X account's database cannot be read, since a SQLite
message carries the path.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The authorization server sent the browser to Cyd's published HTTPS redirect,
which bounced through cyd.social to reach a private-use URL. Cyd now names its
own redirect URI when it authorizes, so the answer comes straight back to the
app and two hops in two other codebases leave the path.

blueskyOAuthCallbackURL() was referenced only by its own test, and its comment
claimed to be the redirect URI registered in Cyd's client metadata, which it
was not. Passing it to authorize() and callback() makes that true. The token
exchange has to name the same URI the authorization did, or the authorization
server refuses it, so both call sites pass it.

This depends on the API publishing that URI in Cyd's client metadata: asking
for a redirect URI the metadata does not list fails before a browser opens.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
micahflee and others added 2 commits September 13, 2026 13:48
Cyd registers its callback scheme with the operating system through the bundle
or desktop entry a packaged build installs. A run from the source tree has
neither, so setAsDefaultProtocolClient fails, the browser refuses the handoff,
and an authorization ends with nothing arriving and nothing said.

npm run finish-oauth takes the URL the browser refused and spawns a second
Electron against the source tree. That instance loses the single-instance lock
and hands its command line to the running app, which is what the operating
system would have done, so the callback travels the same path it does in a
packaged build rather than a path that only exists in development.

The failed registration now says which scheme went unclaimed. Discarding that
return value is why a source run looked identical whether or not the protocol
worked, and why the symptom only appeared three hops later in a browser.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
AccountButton branched on X and Facebook for both the icon and the hover
popup, and fell through to the bare platform icon for anything else. A
Bluesky account therefore sat in the rail as a generic butterfly whose
tooltip named nothing, so a window with several accounts gave no way to
tell which identity each one was.

AccountHeader already had a Bluesky branch. The same window drew the
avatar and handle across the top while the rail beside it stayed
anonymous, which made the gap look like missing data rather than a
component that had never been taught about the platform. The profile is
in hand either way: the account controller converts the PDS avatar to a
data URI when it refreshes the identity, so the rail draws local bytes
and reaches for nothing.

The popup falls back to an invitation to connect, matching how X and
Facebook read before a login, because a Bluesky local account exists
before it holds an identity.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@micahflee
micahflee merged commit aec45f4 into main Sep 13, 2026
1 check passed
@micahflee
micahflee deleted the issue-674-shared-bluesky-oauth branch September 13, 2026 21:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant