Skip to content

fix: keep the rotated refresh token after session refresh - #1864

Merged
stomita merged 3 commits into
mainfrom
fix/refresh-token-rotation
Nov 20, 2026
Merged

stomita merged 3 commits into
mainfrom
fix/refresh-token-rotation

Conversation

@stomita

@stomita stomita commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Summary

When Refresh Token Rotation is enabled on the connected app or External Client App, Salesforce returns a new refresh token on each refresh and invalidates the old one. oauthRefreshFn did not store the new token in conn.refreshToken, so the next refresh on the same connection sent the invalidated token and failed.

This PR passes refresh_token from the token response to _establish. When the response has no refresh_token, _establish keeps the current one, so behavior without rotation is unchanged.

Starting March 30, 2027, Salesforce blocks new installations of connected apps and External Client Apps that do not enable and lock Refresh Token Rotation, along with PKCE and a proof-of-possession control. Salesforce's guidance for apps that use Refresh Token Rotation is to save the new refresh token returned on every refresh.

To persist the rotated token, listen to the refresh event. Its second argument is the token response:

conn.on('refresh', (accessToken, res) => {
  if (res?.refresh_token) {
    // store res.refresh_token
  }
});

Based on #1837 by @hyperair, with tests added. Supersedes #1837. Also fixes the problem reported in #1789.

Tests

Added test/connection-refresh.mock.test.ts, which mocks the token endpoint and checks that:

  • the rotated refresh token is stored in conn.refreshToken
  • the refresh event receives the rotated refresh token
  • the current refresh token is kept when no new one is issued

References

When Refresh Token Rotation is enabled, Salesforce returns a new refresh
token on each refresh and invalidates the old one. Store it in
conn.refreshToken so that the next refresh on the same connection works.

Co-authored-by: Chow Loong Jin <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@hyperair

Copy link
Copy Markdown
Contributor

Thanks for this, I'll close my PR then

@stomita
stomita merged commit 63b7e36 into main Nov 20, 2026
16 checks passed
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