Skip to content

Renew company authorization only when due and keep logins through provider outages - #454

Merged
tnkuehne merged 4 commits into
mainfrom
company-authorization-renewal
Nov 27, 2026
Merged

tnkuehne merged 4 commits into
mainfrom
company-authorization-renewal

Conversation

@tnkuehne

@tnkuehne tnkuehne commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Problem

MCP clients are signed out when the company provider's signing keys download slowly.

Every MCP refresh_token request renewed the company authorization at the provider: discovery, token exchange, then a download of the provider's signing keys to verify the new ID token. When that download hits jose's 5-second timeout, Better Auth reports the ID token as unverified (getUserInfo returns null). Widefleet treated that as an invalid identity and answered 400 invalid_grant, and MCP clients delete their tokens on invalid_grant.

Two things made this frequent:

  • The access token lives 300 seconds. Clients that refresh five minutes before expiry, such as Claude Code, refresh on nearly every connection, and each refresh contacted the provider.
  • A new Better Auth instance per request means each renewal downloads the signing keys again.

App sessions had the same classification gap: a key outage during renewal returned 401 and sent the browser back to sign-in. App access documents that a temporary provider failure returns 503 and keeps the login.

Change

  • One renewal for both paths. renewCompanyAuthorization in packages/auth-proxy/src/company-authorization.ts exchanges the provider credential, keeps a rotated credential, verifies the new identity and returns one of renewed, unavailable, denied or sign-in-required. App sessions (renewal.ts) and MCP grants (company-oauth.ts, mcp-oauth.ts) map that result to cookies and HTTP statuses or to OAuth errors. They previously followed different rules for concurrent exchanges, rotation retries and error mapping.
  • Key outages keep the login. The signing key is resolved before Better Auth verifies the ID token. A failure there returns unavailable (503, or temporarily_unavailable for MCP) and keeps the rotated credential. Better Auth's key set cache then serves its own verification. An invalid signature or claim, a rejected credential or a changed identity still requires a new sign-in.
  • Rejected client credentials keep the login. When the provider rejects Widefleet's own client credentials (invalid_client, for example after a client secret expired), renewal reports unavailable. Signing in again would need the same credentials, so sessions and MCP grants survive until an administrator replaces the secret.
  • MCP renews only when due. A refresh issues the next token from the authorization already verified and contacts the provider only when that authorization can no longer cover a full access token. Tokens never outlive the authorization.
  • App user activity. Like app sessions, MCP reports activity on sign-in and on provider renewal, not for every token issued from an existing authorization.

Checks

  • pnpm check (runtime build, typecheck, format, lint) passes.
  • RUN_EDGE_TESTS=1 pnpm exec vitest run apps/control-plane/tests/edge/ apps/control-plane/tests/mcp-login-runtime.test.ts packages/auth-proxy: 191 passed, including the Cloudflare and celld proxy runtimes.
  • New tests:
    • a key outage during app session renewal returns 503, keeps the session and stores the rotated credential
    • a key outage during MCP renewal returns temporarily_unavailable, and the retry succeeds with the rotated credential
    • MCP refreshes within a current authorization make no provider request
    • an invalid_client response keeps app sessions (503) and MCP grants, and renewal succeeds once it clears
  • The synthetic identity provider can now withhold its key set and issue shorter tokens.

Not changed

Two requests that renew the same MCP grant in different isolates can still race on a provider that rotates refresh tokens strictly. This existed before and was more likely when every refresh contacted the provider.

…vider outages

App sessions and MCP grants renewed the company authorization through two
implementations with different rules. MCP renewed at the provider on every
refresh_token request, and both paths treated a signing-key download that
failed as an invalid identity, so a slow provider signed people out.

Both paths now share renewCompanyAuthorization in the auth proxy. It
resolves the ID token's signing key before Better Auth verifies the token,
so a key outage keeps the login and returns 503 with the rotated provider
credential retained. An invalid token, a rejected credential or a changed
identity still requires a new sign-in.

MCP refreshes issue tokens from the authorization already verified and
contact the provider only when it can no longer cover a full access token.
Like app sessions, only a sign-in or a provider renewal is reported as app
user activity.
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
widefleet-docs 95729c3 Nov 27 2026, 02:57 PM

A key set without the token's key or an unsupported algorithm is a property
of the token, not an outage, and now requires a new sign-in. Download
failures still keep the login.

Claims that the provider will keep returning, such as an unverified email,
another tenant or organization, or invalid identity fields, are rejected as
unauthorized instead of failing as unexpected errors that renewal treats as
outages.

A temporarily_unavailable or 408 response from the token endpoint keeps the
authorization. Failed provider discovery now reaches renewal as a missing
provider, so a missing or expired credential still requires sign-in first,
and MCP answers temporarily_unavailable instead of an unhandled error.
Keep the session tests' identity provider state reset between tests.
An expired client secret makes every refresh fail with invalid_client until an administrator replaces it. Signing in again needs the same credentials, so renewal reports it as unavailable instead of ending app sessions and revoking MCP grants.
@tnkuehne
tnkuehne merged commit c2ddf42 into main Nov 27, 2026
15 checks passed
@tnkuehne
tnkuehne deleted the company-authorization-renewal branch November 27, 2026 15:00
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.

1 participant