You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Propose that Cesium.ITwinPlatform.defaultAccessToken be converted to Cesium.ITwinPlatform.getDefaultAccessToken as an async function instead of a string
The main justification of this change is to avoid dealing with that token expiring.
As a static string, unless the user resets it, the token will expire and if they set it to a variable the reference will be lost and again it will expire.
As an async function it always grabs the latest token. The consumer is forced to call it before they use it, but as tokens are always used for async requests to a service, this should not be a problem. Additionally, if they do save it on a variable the result is still a valid token. Effectively, you are free from this expiration contingency now.
Our Resource class has an option to use a retryCallback automatically when a request fails. I think this can be utilized to fetch a new token only when it's needed, ie when a request just failed due to Auth issues 403.
The question would just be how best to expose this in the API. I'm currently thinking of adding a function ITwinPlatform.requestNewAuthToken that takes no arguments and returns the string of the new token.
Then in each internal Resource we use for the API requests setting a retryCallback that wraps the logic to update the defaultAccessToken for users like this:
constresource=newResource({// ...retryCallback: asyncfunction(resource,error){if(error.statusCode!==403){// we only care about Auth failuresreturnfalse;}constnewToken=awaitITwinPlatform.requestNewAuthToken();if(!defined(newToken)){returnfalse;}resource.headers.Authorization=`Bearer ${newToken}`;ITwinPlatform.defaultAccessToken=newToken;returntrue;},retryAttempts: 1,});
The resulting code for users of this API would look something like this
There's a larger discussion to be had about how Resource handles auth tokens and "sharing" them between Resource objects and derived routes. See #10435.
Right now the token needs to be stored "somewhere else" which is the ITwinPlatform.defaultAccessToken but I don't think users should be required to remember to update that themselves in the ITwinPlatform.requestNewAuthToken function
Feature
Propose that
Cesium.ITwinPlatform.defaultAccessToken
be converted toCesium.ITwinPlatform.getDefaultAccessToken
as an async function instead of a stringThe main justification of this change is to avoid dealing with that token expiring.
As a static string, unless the user resets it, the token will expire and if they set it to a variable the reference will be lost and again it will expire.
As an async function it always grabs the latest token. The consumer is forced to call it before they use it, but as tokens are always used for async requests to a service, this should not be a problem. Additionally, if they do save it on a variable the result is still a valid token. Effectively, you are free from this expiration contingency now.
References:
iModel integration
The text was updated successfully, but these errors were encountered: