Repository navigation
Renew company authorization only when due and keep logins through provider outages - #454
Merged
Merged
Conversation
…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.
Deploying with
|
| 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
MCP clients are signed out when the company provider's signing keys download slowly.
Every MCP
refresh_tokenrequest 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 (getUserInforeturnsnull). Widefleet treated that as an invalid identity and answered400 invalid_grant, and MCP clients delete their tokens oninvalid_grant.Two things made this frequent:
App sessions had the same classification gap: a key outage during renewal returned
401and sent the browser back to sign-in. App access documents that a temporary provider failure returns503and keeps the login.Change
renewCompanyAuthorizationinpackages/auth-proxy/src/company-authorization.tsexchanges the provider credential, keeps a rotated credential, verifies the new identity and returns one ofrenewed,unavailable,deniedorsign-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.unavailable(503, ortemporarily_unavailablefor 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.invalid_client, for example after a client secret expired), renewal reportsunavailable. Signing in again would need the same credentials, so sessions and MCP grants survive until an administrator replaces the secret.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.503, keeps the session and stores the rotated credentialtemporarily_unavailable, and the retry succeeds with the rotated credentialinvalid_clientresponse keeps app sessions (503) and MCP grants, and renewal succeeds once it clearsNot 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.