Why Centralised Token Refresh Is Required
When a refresh token is used:- Pleo issues a new access token and a new refresh token
- All previous tokens are immediately invalidated
Core Requirements
Your token refresh mechanism must:- Monitor the
expires_infield returned with access tokens - Refresh tokens before access tokens expire
- Ensure only one refresh request is in flight at a time per
- Update and persist both the access token and refresh token on success
- Immediately discard all previously issued tokens
Retry and Failure Handling
- Retry refresh requests using exponential backoff for transient failures
(for example: network timeouts or temporary service unavailability) - Do not retry indefinitely
- If refresh fails due to an invalid or expired refresh token:
- Stop retrying
- Restart the OAuth 2.0 flow
- Prompt the user to reauthenticate
Concurrency and Race Condition Prevention
To prevent multiple refresh attempts:- Use locking, queuing, or a shared refresh promise/future
- Ensure all API requests depend on the same refreshed token result
- Block or delay outgoing API calls while a refresh is in progress
Outcome
- Tokens remain consistent across your system
- Refresh token invalidation errors are avoided
- Users are only prompted to reauthenticate when strictly necessary
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