-
Notifications
You must be signed in to change notification settings - Fork 47
Share SharedPreferencesInMemoryCache across cache instances Fixes AB#3428107 #2813
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
Changes from 1 commit
56cdb78
9c24275
757b178
9102d98
90223bc
280cffa
5a67472
d439457
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,7 +52,9 @@ | |
| import java.util.HashSet; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| import edu.umd.cs.findbugs.annotations.Nullable; | ||
| import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
|
|
@@ -1641,24 +1643,7 @@ private static MicrosoftFamilyOAuth2TokenCache initializeFociCache(@NonNull fina | |
| private static <T extends MsalOAuth2TokenCache> T getTokenCache(@NonNull final IPlatformComponents components, | ||
| @NonNull final INameValueStorage<String> spfm, | ||
| boolean isFoci) { | ||
| final ICacheKeyValueDelegate cacheKeyValueDelegate = new CacheKeyValueDelegate(); | ||
| final boolean isFlightEnabled = CommonFlightsManager.INSTANCE | ||
| .getFlightsProvider() | ||
| .isFlightEnabled(CommonFlight.USE_IN_MEMORY_CACHE_FOR_ACCOUNTS_AND_CREDENTIALS); | ||
| final IAccountCredentialCache accountCredentialCache; | ||
| if (isFlightEnabled) { | ||
| accountCredentialCache = new SharedPreferencesAccountCredentialCacheWithMemoryCache( | ||
| cacheKeyValueDelegate, | ||
| spfm | ||
| ); | ||
| SpanExtension.current().setAttribute(AttributeName.in_memory_cache_used_for_accounts_and_credentials.name(), true); | ||
| } else { | ||
| accountCredentialCache = new SharedPreferencesAccountCredentialCache( | ||
| cacheKeyValueDelegate, | ||
| spfm | ||
| ); | ||
| SpanExtension.current().setAttribute(AttributeName.in_memory_cache_used_for_accounts_and_credentials.name(), false); | ||
| } | ||
| final IAccountCredentialCache accountCredentialCache = getCacheToBeUsed(spfm); | ||
| final MicrosoftStsAccountCredentialAdapter accountCredentialAdapter = | ||
| new MicrosoftStsAccountCredentialAdapter(); | ||
|
|
||
|
|
@@ -1678,6 +1663,35 @@ private static <T extends MsalOAuth2TokenCache> T getTokenCache(@NonNull final I | |
| ); | ||
| } | ||
|
|
||
| private static final Map<INameValueStorage<String>, SharedPreferencesAccountCredentialCacheWithMemoryCache> | ||
| inMemoryCacheMapByStorage = new ConcurrentHashMap<>(); | ||
|
||
|
|
||
| /** | ||
| * Determines which cache implementation to use based on flighting. | ||
| * @param spfm | ||
| * @return | ||
siddhijain marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| private static IAccountCredentialCache getCacheToBeUsed(@NonNull final INameValueStorage<String> spfm) { | ||
| final boolean isFlightEnabled = CommonFlightsManager.INSTANCE | ||
| .getFlightsProvider() | ||
| .isFlightEnabled(CommonFlight.USE_IN_MEMORY_CACHE_FOR_ACCOUNTS_AND_CREDENTIALS); | ||
| final ICacheKeyValueDelegate cacheKeyValueDelegate = new CacheKeyValueDelegate(); | ||
| SpanExtension.current().setAttribute(AttributeName.in_memory_cache_used_for_accounts_and_credentials.name(), isFlightEnabled); | ||
| if (isFlightEnabled) { | ||
| return inMemoryCacheMapByStorage.computeIfAbsent(spfm, s -> | ||
siddhijain marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| new SharedPreferencesAccountCredentialCacheWithMemoryCache( | ||
| cacheKeyValueDelegate, | ||
| spfm | ||
| ) | ||
| ); | ||
|
||
| } else { | ||
| return new SharedPreferencesAccountCredentialCache( | ||
| cacheKeyValueDelegate, | ||
| spfm | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| @Nullable | ||
| private MsalOAuth2TokenCache getTokenCacheForClient(@Nullable final BrokerApplicationMetadata metadata) { | ||
| final String methodName = ":getTokenCacheForClient(bam)"; | ||
|
|
@@ -1736,53 +1750,4 @@ private MsalOAuth2TokenCache getTokenCacheForClient(@NonNull final String client | |
|
|
||
| return getTokenCacheForClient(metadata); | ||
| } | ||
|
|
||
| /** | ||
| * Sets the SSO state for the supplied Account, relative to the provided uid. | ||
| * | ||
| * @param uidStr The uid of the app whose SSO token is being inserted. | ||
| * @param account The account for which the supplied token is being inserted. | ||
| * @param refreshToken The token to insert. | ||
| */ | ||
| public void setSingleSignOnState(@NonNull final String uidStr, | ||
| @NonNull final GenericAccount account, | ||
| @NonNull final GenericRefreshToken refreshToken) { | ||
| final String methodName = ":setSingleSignOnState"; | ||
|
|
||
| final boolean isFrt = refreshToken.getIsFamilyRefreshToken(); | ||
|
|
||
| MsalOAuth2TokenCache targetCache; | ||
|
|
||
| final int uid = Integer.parseInt(uidStr); | ||
|
|
||
| if (isFrt) { | ||
| Logger.verbose(TAG + methodName, "Saving tokens to foci cache."); | ||
|
|
||
| targetCache = mFociCache; | ||
| } else { | ||
| // If there is an existing cache for this client id, use it. Otherwise, create a new | ||
| // one based on the supplied uid. | ||
| targetCache = initializeProcessUidCache(getComponents(), uid); | ||
| } | ||
| try { | ||
| targetCacheSetSingleSignOnState(account, refreshToken, targetCache); | ||
| updateApplicationMetadataCache( | ||
| refreshToken.getClientId(), | ||
| refreshToken.getEnvironment(), | ||
| refreshToken.getFamilyId(), | ||
| uid | ||
| ); | ||
| } catch (ClientException e) { | ||
| Logger.warn( | ||
| TAG + methodName, | ||
| "Failed to save account/refresh token. Skipping." | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| // Suppressing unchecked warning as the generic type was not provided for targetCache | ||
| @SuppressWarnings(WarningType.unchecked_warning) | ||
| private void targetCacheSetSingleSignOnState(@NonNull GenericAccount account, @NonNull GenericRefreshToken refreshToken, MsalOAuth2TokenCache targetCache) throws ClientException { | ||
| targetCache.setSingleSignOnState(account, refreshToken); | ||
| } | ||
| } | ||

Uh oh!
There was an error while loading. Please reload this page.