Skip to content

fix(oauth): tell expired grants apart and stop retrying them - #16

Open
gopkg-dev wants to merge 1 commit into
mainfrom
fix/oauth-refresh-errors
Open

gopkg-dev wants to merge 1 commit into
mainfrom
fix/oauth-refresh-errors

Conversation

@gopkg-dev

Copy link
Copy Markdown
Owner

Summary

Every OAuth token refresh failure was reported as "authorization has expired, re-authorize", including proxy timeouts and provider 5xx errors. Once a refresh token was dead for real, the Graph worker kept calling the token endpoint every minute, and the IMAP worker kept reconnecting with backoff, with no end.

  • tokenRequest now throws OAuthTokenError, which keeps the OAuth error code and the HTTP status.
  • refreshAccessToken sorts failures into three groups:
    • invalid_grant / interaction_required / consent_required: asks the user to re-authorize the mailbox and marks the error reauthRequired.
    • invalid_client / unauthorized_client: tells the user to check the Client ID and secret in the OAuth app settings.
    • Network errors, timeouts, 5xx and non-JSON responses: says the refresh failed and will be retried automatically.
  • Graph and IMAP workers stop polling or reconnecting after a reauthRequired error. Re-authorizing already restarts the worker (syncEngine.restartAccount in api/oauth.ts). Other errors are retried the same way as before.
  • The Graph, IMAP and SMTP error describers return the refresh error message as-is, so it no longer gets replaced by another message.
  • Re-authorizing clears the cached token and any refresh still in progress. A refresh that overlaps a re-authorization never writes the old grant's tokens back to the database.

Test plan

  • npm run typecheck
  • npm test: 195/195 pass, including the new test/mailbox-oauth-refresh.test.ts (error classification and messages, workers stopping vs retrying, token cache reset after re-authorization)

🤖 Generated with Claude Code

Token refresh failures were all reported as "authorization has expired,
re-authorize", even for proxy timeouts or provider 5xx, and a dead grant
kept workers hitting the token endpoint forever.

- tokenRequest throws OAuthTokenError with the OAuth error code and status
- invalid_grant / interaction_required / consent_required ask the user to
  re-authorize and mark the error reauthRequired; invalid_client /
  unauthorized_client point at the OAuth app settings; network errors,
  timeouts and 5xx say the refresh will be retried
- Graph and IMAP workers stop polling / reconnecting on a reauthRequired
  error; re-authorization already restarts the worker via
  syncEngine.restartAccount
- Graph / IMAP / SMTP error describers keep the refresh error message
- Re-authorization clears the cached token and in-flight refresh, and a
  refresh that races it never writes the old grant's tokens back
- Tests for the error classification, worker stop/retry and cache reset

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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