Skip to content

fix: save refreshed token before onRefreshEnd runs - #9

Merged
pjcdawkins merged 3 commits into
mainfrom
fix/save-token-before-refresh-end
Oct 1, 2026
Merged

pjcdawkins merged 3 commits into
mainfrom
fix/save-token-before-refresh-end

Conversation

@pjcdawkins

Copy link
Copy Markdown
Collaborator

Save the acquired token via the token-save callback before onRefreshEnd runs, for normal refreshes and for tokens returned by onRefreshStart or onRefreshError. onRefreshEnd still runs on failure.

Before this change, an application holding an inter-process lock between onRefreshStart and onRefreshEnd released it before the new token was saved. Another process could then read the old, already-rotated refresh token from storage and reuse it, which revokes the session.

Also fixes setAccessToken() throwing an Error when no token-save callback was set.

Once released, the Upsun CLI workaround from upsun/cli#194 (refreshing inside on_refresh_start via a Connector subclass) can be removed.

🤖 Generated with Claude Code

pjcdawkins and others added 3 commits November 25, 2024 19:19
getAccessToken() saved the token returned by acquireAccessToken() only
after acquireAccessToken() had called onRefreshEnd in its finally block.
Applications that hold an inter-process lock between onRefreshStart and
onRefreshEnd released the lock before the new token was stored, so
another process could take the lock, read the old refresh token from
storage, and reuse it. With refresh token rotation this counts as reuse
and revokes the session.

acquireAccessToken() now sets and saves the token itself, inside the try
block, so the save callback runs before onRefreshEnd. This applies to a
normal refresh and to tokens returned by onRefreshStart or
onRefreshError. onRefreshEnd still runs on failure.

Adds GuzzleMiddleware tests for the callback order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The $tokenSave typed property has no default, so reading it before
setTokenSaveCallback() was called threw an Error. Use isset(), as for
the other callbacks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pjcdawkins
pjcdawkins merged commit 38fd5e7 into main Oct 1, 2026
2 checks passed
@pjcdawkins
pjcdawkins deleted the fix/save-token-before-refresh-end branch October 1, 2026 00:15
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