fix(dart): fix getAccessToken always null bug #67
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes the
getAccessToken
always fetches new access tokens using the refresh token bug. (See #65 for more details.)Root cause:
In the Logto Dart SDK, we store all token responses in local storage using a key-value format. Access tokens are stored based on their audience information and permissions. To generate a unique storage key for each access token, we use a combination of
resource
,organization
, andscopes
.When calling the
client.getAccessToken
method, the SDK first checks local storage for a valid access token. If no related token is found or the token has expired, it requests a new one using the refresh token.Currently, Logto does not support narrowing down scopes in token exchange requests, so scopes are not required in the
fetchTokenByRefreshToken
method. As a result, thegetAccessToken
method does not accept ascopes
parameter and always attempts to read tokens from storage without consideringscopes
.However, since the
setAccessToken
method builds storage keys with token scopes, andgetAccessToken
reads the storage without them, the method always returnsnull
. This fallback triggers a new access token request every time.Fixes:
scopes
input parameter from the_tokenStorage.buildAccessTokenKey
and_tokenStorage.getAccessToken
method. The storage key should now be generated based only onresource
andorganization
._tokenStorage.save
call in theclient._handleSignInCallback
method to align with theclient._getTokenByRefresh
flow. Ensure that the initial access token scopes are also saved in the token storage.Although token scopes should always be stored as part of the access token values, the storage key should be generated using only the
resource
andorganization
.Testing
test locally
UT updated
Checklist
.changeset