microsoft-authentication-library-for-android icon indicating copy to clipboard operation
microsoft-authentication-library-for-android copied to clipboard

The `acquireTokenSilent` method is not performant when fetching token result from cache

Open aj-kueterman opened this issue 2 months ago • 4 comments

Describe the bug The acquireTokenSilent method, used to fetch access tokens (including the ability to refresh tokens when needed) from the MSAL SDK is not performant enough for it's use case.

In our practical use of the MSAL library, we use an OkHttp Interceptor to append tokens to each request. These tokens are pulled from MSAL using acquireTokenSilent. Based on the documentation, this should mean on most requests that the access tokens are pulled from a cache and returned, except in the case where there is no valid access token. This works, but the time to fetch a token from the MSAL using this method is not performant - on the order of hundreds of milliseconds. When caching the access token locally instead we're able to fetch new tokens in less than 100ms.

Smartphone Observed on multiple combinations of Android devices, operating systems, and browsers. com.microsoft.identity.client:msal version 4.0.1.

Stacktrace N/A

To Reproduce Steps to reproduce the behavior:

  • Follow vanilla setup for MSAL Android project
  • Use acquireTokenSilent to fetch tokens on every network call
  • Make network calls as normal, and log the duration of each acquireTokenSilent call

Expected behavior When access token is valid, calls to acquireTokenSilent should return tokens almost instantaneously.

Actual Behavior When access token is valid, calls to acquireTokenSilent are returning tokens in between 100-1000ms.

Screenshots Timing the time it takes to get a token from MSAL vs. pulling it from our local cache.

Using MSAL Local Cache
Screenshot 2024-05-09 at 11 50 43 AM Screenshot 2024-05-09 at 11 14 47 AM

Additional context Related SO Post summarizing the same issue.

aj-kueterman avatar May 09 '24 15:05 aj-kueterman