fix(sso): share one MSAL4J application so Entra ID silent refresh can work - #3227
Open
marevol wants to merge 1 commit into
Open
fix(sso): share one MSAL4J application so Entra ID silent refresh can work#3227marevol wants to merge 1 commit into
marevol wants to merge 1 commit into
Conversation
… work
getAccessToken() and refreshTokenSilently() each built their own
ConfidentialClientApplication. Every instance owns its own in-memory TokenCache,
and msal4j's AcquireTokenSilentSupplier throws
MsalClientException(NO_TOKEN_IN_CACHE, CACHE_MISS) when the account is not in it:
res = clientApplication.tokenCache.getCachedAuthenticationResult(account, ...);
if (res == null) {
throw new MsalClientException(AuthenticationErrorMessage.NO_TOKEN_IN_CACHE, ...);
}
The tokens acquired at login therefore went into an instance that was discarded
immediately afterwards, and acquireTokenSilently could never find them.
refreshTokenSilently() always threw, always returned null, and
EntraIdCredential.refresh() never took its success branch -- so updateMemberOf()
never ran again for the life of the session and a user's group membership was
whatever it had been at login.
Hold one application per authenticator, which is what msal4j documents as the
intended usage, and rebuild it when the client id, secret or tenant changes,
since all three are editable from the admin screen while Fess is running. The
build is double-checked so concurrent logins share one token cache.
This does not change what happens when a refresh genuinely fails:
refresh() still returns false and FessBaseAction still ignores that. Acting on
it would affect every FessUser implementation, not just Entra ID.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
getAccessToken()(both overloads) andrefreshTokenSilently()each built their own clientapplication:
Every
ConfidentialClientApplicationowns its own in-memoryTokenCache(
AbstractClientApplicationBase:super.tokenCache = new TokenCache(builder.tokenCacheAccessAspect)),and no
ITokenCacheAccessAspectis configured, so nothing persists it. msal4j'sAcquireTokenSilentSupplierthen does:The tokens acquired at login went into an instance discarded immediately afterwards, so
acquireTokenSilentlylooked in an empty cache every time.refreshTokenSilently()alwaysthrew, always returned
null, andEntraIdCredential.refresh()never took its success branch:So
updateMemberOf()never ran again after login. A user's groups and roles were whatever theyhad been at login for the whole life of the session, no matter what changed in Entra ID.
Fix
Hold one application per authenticator — which is what msal4j documents as the intended usage,
and what keeps the token cache alive between the login and its refresh.
getClientApplication()rebuilds it when the client id, secret or tenant changes, because allthree are editable from the admin screen while Fess is running. The build is double-checked under
a lock so concurrent logins share one cache rather than racing to create two. The cache key
reduces the secret to a hash so it cannot travel into a log line or a heap dump label.
Also drops a now-unused local in
refreshTokenSilently().What this deliberately does not change
When a refresh genuinely fails,
refresh()still returnsfalseandFessBaseAction.godHandProloguestill discards that value:Acting on it would log the user out, and it would affect every
FessUserimplementation — LDAP,OpenID Connect, SAML — not just Entra ID. That belongs in its own change.
Note also that the cache is in-memory: it does not survive a restart, and it is not shared across
a cluster. In both cases the behaviour degrades to what it is today (the refresh fails, the
session keeps its existing token until it expires), not to something worse.
Tests
EntraIdAuthenticator's unit test class, 3 new tests:test_getClientApplication_isReusedSoItsTokenCacheSurvivestest_getClientApplication_isRebuiltWhenTheConfigurationChanges— secret, client id and tenanteach independently
test_getClientApplication_isBuiltOnceUnderConcurrentAccess— 8 threads must share one instanceFalsification: with the reuse check removed so the application is rebuilt every call, the first
and third fail.
The end-to-end refresh — login, wait for expiry, silent re-acquisition — needs a real tenant and
is not covered here. What is covered is the mechanism that made it impossible.
(the whole
org.codelibs.fess.ssopackage)