Code exchange: a rotating provider's grant without a refresh token fails the callback

Joystick rotates refresh tokens, so a code-exchange answer whose refresh_token
is absent or empty would store a credential the first refresh revokes. The
shared exchange now throws a typed ProviderResponseError before the identity
is resolved or anything is written. The residual-risk comment on the in-flight
refresh map cites its follow-up.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-19 14:35:11 +02:00
commit 1d856ebd40
3 changed files with 45 additions and 6 deletions

View file

@ -68,8 +68,10 @@ function withoutTokens(identity: StoredLinkedIdentity): StoredLinkedIdentity {
* attempt is in flight join it and receive its outcome, success or failure.
* The entry lives exactly as long as the attempt: a later request retries.
*
* Accepted residual risk (docs/service-contract.md): one presentation per
* process per burst, so N processes may present one token N times.
* Known residual risk (docs/service-contract.md): one presentation per
* process per burst, so N processes may present one token N times. The
* cross-process failed-refresh marker that would close it is
* joystick:follow_up:cross-process-failed-refresh-marker.
*/
const refreshesInFlight = new WeakMap<HeimdallStore, Map<string, Promise<ManagedCredentialOutcome>>>();

View file

@ -244,12 +244,20 @@ function usableExpiresIn(value: unknown): number | undefined {
}
function sharedRuntimeExchange(provider: Provider): OAuthProviderRuntime["exchangeAuthorizationCode"] {
return (options) =>
requestProviderTokens(provider, options.config, {
return async (options) => {
const tokenSet = await requestProviderTokens(provider, options.config, {
grant_type: "authorization_code",
code: options.code,
redirect_uri: options.redirectUri,
});
// A rotating provider's refresh token is the only way back to the
// credential: a grant that arrives without one would be stored as a row
// the first refresh revokes, so the callback fails before it writes.
if (providerCatalog[provider].rotatesRefreshTokens && !tokenSet.refreshToken) {
throw new ProviderResponseError(200, "provider_body_not_token_set", `${providerCatalog[provider].displayName} token exchange failed`);
}
return tokenSet;
};
}
function sharedRuntimeRefresh(provider: Provider): NonNullable<OAuthProviderRuntime["refreshAccessToken"]> {