Skip to content

Move Link sign-in and token refresh into the SDK - #510

Closed
ksn-stripe wants to merge 2 commits into
mainfrom
ksn/sdk-auth
Closed

ksn-stripe wants to merge 2 commits into
mainfrom
ksn/sdk-auth

Conversation

@ksn-stripe

@ksn-stripe ksn-stripe commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

SDK apps can now sign a user in to Link and keep their token fresh the same way link-cli auth login does. Until now the SDK only accepted accessToken or getAccessToken, so every SDK agent had to re-implement device sign-in and refresh. The reusable parts of link-cli's auth code move into @stripe/link-sdk, and the CLI now uses them, the same way #489 did for MPP.

New SDK exports

  • LinkAuthResource: device sign-in (start and poll), refresh, and revoke. clientId is now a required option, so an SDK app can't silently present itself as the link-cli client. The CLI passes its own ID and clientName: 'Link CLI'; the SDK's default name is Link SDK.
  • createAccessTokenProvider(authResource, storage, options): plugs into new Link({ getAccessToken }). The app supplies an AuthStorage (getTokens, setTokens, clearTokens).
  • Scopes, buildAuthorizationDetails, the auth error classes, and the auth types.

Behavior change: callers that overlap now share one in-flight refresh. Link replaces the refresh token on every refresh, so two parallel refreshes (common with agents making parallel tool calls) would invalidate the session. The CLI only refreshes from one place, so this doesn't change CLI behavior.

Unchanged in the CLI: its credential file, pending sign-in state, auth upgrade merge logic, commands, and user-facing messages. A thin wrapper passes the CLI's exact messages to the SDK provider. Refresh at /auth/token with a client secret (registered clients) is out of scope, because the CLI doesn't do it either.

Validation: SDK typecheck and tests (375) pass, including the moved auth tests and a new concurrent-refresh test. CLI and Eve typecheck pass, Eve tests pass, and Biome is clean. The full CLI suite runs locally on a rolldown build, because esbuild can't run on my machine: 714 pass, and the 20 that fail are UI rendering tests that fail identically on the base branch. CI runs the real tsup build.

🤖 Generated with Claude Code

@ksn-stripe ksn-stripe self-assigned this Nov 3, 2026
@ksn-stripe
ksn-stripe marked this pull request as ready for review November 4, 2026 14:53
@ksn-stripe
ksn-stripe requested a review from a team as a code owner November 4, 2026 14:53
ksn-stripe and others added 2 commits November 4, 2026 10:26
LinkAuthResource (device authorization, refresh, revocation), the
createAccessTokenProvider token-refresh wrapper, scope/authorization-detail
helpers, and auth-specific types/errors move from packages/cli/src/auth/ into
packages/sdk/src/auth/ and are exported from @stripe/link-sdk, so an app built
on the SDK can sign in and keep a token fresh without copying CLI internals
(same pattern #489 used for MPP).

LinkAuthResource now requires a clientId option instead of a hardcoded
constant, so an SDK-based app can't silently pose as link-cli.
createAccessTokenProvider now coalesces concurrent refreshes into a single
refreshToken call, since Link rotates the refresh token on every use and two
parallel refreshes would otherwise race.

The CLI keeps its credential storage, pending-device-auth workflow state,
auth upgrade merge logic, and login/logout/status commands. Its own
auth/session.ts becomes a thin wrapper over the SDK's
createAccessTokenProvider that supplies the CLI's exact, unchanged
user-facing messages via the new message options.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Committed-By-Agent: claude
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Committed-By-Agent: claude
@ksn-stripe
ksn-stripe changed the base branch from raubrey/mpp-pay-sdk to main November 4, 2026 15:39

@raubrey-stripe raubrey-stripe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I'm not sure we want this/might want to get some context first?

I know @stevekaliski-stripe specifically left auth open / just accepting an access token so that integrating agents would be able to integrate with various OAuth strategies (critically, Muse and other agents don't use device oauth and we don't want them to). @jlau-stripe to confirm as well?

@ksn-stripe
ksn-stripe marked this pull request as draft November 4, 2026 20:18
@ksn-stripe

ksn-stripe commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor Author

Tossed up this PR while trying to remove friction from building an agent around the Link SDK. That rationale makes sense for leaving the SDK open for different forms of auth. Closing.

@ksn-stripe ksn-stripe closed this Nov 4, 2026
@ksn-stripe
ksn-stripe deleted the ksn/sdk-auth branch November 4, 2026 20:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants