Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dart): fix getAccessToken always null bug #67

Merged
merged 1 commit into from
Sep 6, 2024

Conversation

simeng-li
Copy link
Contributor

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, and scopes.

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, the getAccessToken method does not accept a scopes parameter and always attempts to read tokens from storage without considering scopes.

However, since the setAccessToken method builds storage keys with token scopes, and getAccessToken reads the storage without them, the method always returns null. This fallback triggers a new access token request every time.

Fixes:

  • Remove the scopes input parameter from the _tokenStorage.buildAccessTokenKey and _tokenStorage.getAccessToken method. The storage key should now be generated based only on resource and organization.
  • Update the _tokenStorage.save call in the client._handleSignInCallback method to align with the client._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 and organization.

Testing

test locally

UT updated

Checklist

  • .changeset
  • unit tests
  • integration tests
  • necessary TSDoc comments

fix getAccessToken always null bug
@simeng-li simeng-li requested review from a team, wangsijie and charIeszhao and removed request for a team September 5, 2024 10:04
@simeng-li simeng-li merged commit 554a0d3 into master Sep 6, 2024
2 checks passed
@simeng-li simeng-li deleted the simeng-access-token-bug-fix branch September 6, 2024 03:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants