@@ -37,6 +37,8 @@ AuthenticationResult execute() throws Exception {
3737 clientApplication .serviceBundle ()
3838 );
3939
40+ CacheRefreshReason cacheRefreshReason = CacheRefreshReason .NOT_APPLICABLE ;
41+
4042 if (!managedIdentityParameters .forceRefresh ) {
4143 LOG .debug ("ForceRefresh set to false. Attempting cache lookup" );
4244
@@ -63,23 +65,37 @@ AuthenticationResult execute() throws Exception {
6365 this .clientApplication ,
6466 silentRequest );
6567
66- return supplier .execute ();
68+ AuthenticationResult result = supplier .execute ();
69+ cacheRefreshReason = SilentRequestHelper .NeedsRefresh (
70+ parameters ,
71+ result ,
72+ LOG );
73+
74+ // If the token does not need a refresh, return the cached token
75+ // Else refresh the token if it is either expired, proactively refreshable, or if the claims are passed.
76+ if (cacheRefreshReason == CacheRefreshReason .NOT_APPLICABLE ) {
77+ LOG .debug ("Returning token from cache" );
78+ result .metadata ().tokenSource (TokenSource .CACHE );
79+ return result ;
80+ } else {
81+ LOG .debug (String .format ("Refreshing access token. Cache refresh reason: %s" , cacheRefreshReason ));
82+ }
6783 } catch (MsalClientException ex ) {
6884 if (ex .errorCode ().equals (AuthenticationErrorCode .CACHE_MISS )) {
6985 LOG .debug (String .format ("Cache lookup failed: %s" , ex .getMessage ()));
70- return fetchNewAccessTokenAndSaveToCache (tokenRequestExecutor );
86+ return fetchNewAccessTokenAndSaveToCache (tokenRequestExecutor , cacheRefreshReason );
7187 } else {
7288 LOG .error (String .format ("Error occurred while cache lookup: %s" , ex .getMessage ()));
7389 throw ex ;
7490 }
7591 }
7692 }
7793
78- LOG .info ("Skipped looking for an Access Token in the cache because forceRefresh or Claims were set. " );
79- return fetchNewAccessTokenAndSaveToCache (tokenRequestExecutor );
94+ LOG .info ("Skipped looking for an Access Token in the cache because forceRefresh was set. Or the token in the cache needs refresh " );
95+ return fetchNewAccessTokenAndSaveToCache (tokenRequestExecutor , cacheRefreshReason );
8096 }
8197
82- private AuthenticationResult fetchNewAccessTokenAndSaveToCache (TokenRequestExecutor tokenRequestExecutor ) {
98+ private AuthenticationResult fetchNewAccessTokenAndSaveToCache (TokenRequestExecutor tokenRequestExecutor , CacheRefreshReason cacheRefreshReason ) throws Exception {
8399
84100 ManagedIdentityClient managedIdentityClient = new ManagedIdentityClient (msalRequest , tokenRequestExecutor .getServiceBundle ());
85101
@@ -91,7 +107,10 @@ private AuthenticationResult fetchNewAccessTokenAndSaveToCache(TokenRequestExecu
91107
92108 AuthenticationResult authenticationResult = createFromManagedIdentityResponse (managedIdentityResponse );
93109 clientApplication .tokenCache .saveTokens (tokenRequestExecutor , authenticationResult , clientApplication .authenticationAuthority .host );
94- return authenticationResult ;
110+ AuthenticationResult result = authenticationResult ;
111+ result .metadata ().tokenSource (TokenSource .IDENTITY_PROVIDER );
112+ result .metadata ().cacheRefreshReason (cacheRefreshReason );
113+ return result ;
95114 }
96115
97116 private AuthenticationResult createFromManagedIdentityResponse (ManagedIdentityResponse managedIdentityResponse ) {
0 commit comments