Lev XERO OAuth2 tokens refresh design considerations
Xero integration consists of numerous channels. Channels can be deployed from single or multiple Iguanas instances.
Xero is sensitive to frequency of connections to its servers. If every channel will try individually to refresh access token to same Xero App this may result with a non syncable havoc of refresh requests. The problem gets even more complex given the temporary, non-permanent, nature of Xero refresh token.
Creating multiple Xero App is not a workaround, nor an answer.
Because attempts to refresh access token cannot be synchronized across multiple individual channels - none of the channels is given the individual right to refesh access token for as long as last provided by Xero to any of the channels access token hasn't expired.
This rule can be applied to single Iguana instance.
This rule can be applied and shared among multiple Iguana instances with some basic but yet written code. The sharing will happen across HTTP, among channels from variety of Iguana instances, rather than file system.
One may say it is the sync, but it is not quite so. It is more of fluid role taken by any of channels. Role of producer vs. consumer. Each channel can be both.
We have one Producer only channel. Producer channel can be running on any Iguana. The channel is roducing valid access token and stores it in manner that it can be shared with rest of interested in this token channels. When any of consuming the access token channels needs one, they read the token from shared resource. Every shared access token has comlimentary shared correspomnding refresh token information.
Producer will refresh shared access token periodically if access token has been found expired. Or leaves it alone if not expired.
Any consumer channel can collect the shared access token and to refresh it, on consumer's own, if token is expired.
The updated values for access and refresh tokens becomes shared in similar manner as if the Producer channel would have done this.
Producer channel will not attempt the periodic refresh if access and refresh tokens had been refreshed by one of Consumers.
Here comes in the 'fluid role', of channels requiring the Xero connectivity, to assure existence of valid access credentials at all times, even that Xero refresh tokens expire; and Xero app is single app; and Xero has strict quota on number and frequency of connections from us.
Â