Detecting Expired or Revoked Refresh Tokens
- Failed token refresh attempts typically indicate expiry or revocation.
- The token endpoint will return an error response such as:
invalid_grantinvalid_token
- Do not attempt to reuse the expired or revoked token.
Recovery Strategies
When a refresh token is no longer valid:-
Redirect Users to Reauthenticate
- Start a new OAuth 2.0 authorisation flow.
- Prompt the user to grant consent again, generating a new access and refresh token pair.
- Preserve the user’s context if possible (e.g., use the
stateparameter to remember post-login destination).
-
Fallback Mechanisms
- Implement temporary measures to avoid broken sessions. For example:
- Queue API requests until a new token is obtained.
- Display a friendly message prompting the user to reconnect.
- Implement temporary measures to avoid broken sessions. For example:
-
Secure Cleanup
- Immediately discard the expired or revoked refresh token.
- Ensure no copies remain in storage or memory to prevent accidental reuse.
Best Practices
- Centralise token refresh logic to reduce errors and race conditions.
- Monitor refresh failures to detect potential security incidents.
- Ensure users can recover with minimal friction to maintain trust and continuity.
- Use consistent logging and error handling to aid debugging and operational monitoring.
Related Reading
- OAuth 2.0 Overview - introduction to OAuth 2.0 for Pleo integrations
- API Keys Overview – alternative authentication method
- Tokens Overview – understanding access and refresh tokens
- Secure Token Storage – storing credentials securely
- Race Condition Prevention – avoid duplicate token refresh attempts
- OAuth 2.0 Setup Workflow Guide - Step-by-step guide to configure OAuth 2.0 for your integration