From 8d1eb6eace00d9c8d641a22f44b9a698b9f770f4 Mon Sep 17 00:00:00 2001 From: Udara Pathum <46132469+hwupathum@users.noreply.github.com> Date: Thu, 23 Jan 2025 18:41:58 +0530 Subject: [PATCH] Revert "Validate AuthenticationResultCache expiry" --- .../cache/AuthenticationResultCache.java | 18 ----- .../framework/util/FrameworkUtils.java | 2 +- .../framework/util/FrameworkUtilsTest.java | 66 ------------------- 3 files changed, 1 insertion(+), 85 deletions(-) diff --git a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/cache/AuthenticationResultCache.java b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/cache/AuthenticationResultCache.java index 0331aae5a098..871a439893d6 100644 --- a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/cache/AuthenticationResultCache.java +++ b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/cache/AuthenticationResultCache.java @@ -22,8 +22,6 @@ import org.apache.commons.logging.LogFactory; import org.wso2.carbon.base.MultitenantConstants; import org.wso2.carbon.identity.application.authentication.framework.store.SessionDataStore; -import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants; -import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkUtils; import org.wso2.carbon.identity.core.util.IdentityTenantUtil; import org.wso2.carbon.identity.core.util.IdentityUtil; @@ -95,30 +93,14 @@ public void addToCache(AuthenticationResultCacheKey key, AuthenticationResultCac * @return Cached entry. */ public AuthenticationResultCacheEntry getValueFromCache(AuthenticationResultCacheKey key) { - AuthenticationResultCacheEntry entry = super.getValueFromCache(key); if (entry == null && isTemporarySessionDataPersistEnabled) { entry = (AuthenticationResultCacheEntry) SessionDataStore.getInstance(). getSessionData(key.getResultId(), CACHE_NAME); - if (entry != null && isCacheEntryExpired(entry)) { - return null; - } } return entry; } - private boolean isCacheEntryExpired(AuthenticationResultCacheEntry entry) { - - String createdTimestamp = entry.getResult().getProperty(FrameworkConstants.CREATED_TIMESTAMP).toString(); - if (createdTimestamp != null && - (FrameworkUtils.getCurrentStandardNano() > - entry.getValidityPeriod() + Long.parseLong(createdTimestamp) * 1000000)) { - log.debug("Authentication result cache is expired"); - return true; - } - return false; - } - /** * Clears a cache entry. * diff --git a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/util/FrameworkUtils.java b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/util/FrameworkUtils.java index a48380da58eb..e346a3c25f52 100644 --- a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/util/FrameworkUtils.java +++ b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/main/java/org/wso2/carbon/identity/application/authentication/framework/util/FrameworkUtils.java @@ -1193,7 +1193,7 @@ public static void addAuthenticationResultToCache(String key, AuthenticationResu AuthenticationResultCacheKey cacheKey = new AuthenticationResultCacheKey(key); AuthenticationResultCacheEntry cacheEntry = new AuthenticationResultCacheEntry(); cacheEntry.setResult(authenticationResult); - cacheEntry.setValidityPeriod(TimeUnit.MINUTES.toNanos(IdentityUtil.getTempDataCleanUpTimeout())); + cacheEntry.setValidityPeriod(TimeUnit.MINUTES.toNanos(IdentityUtil.getOperationCleanUpTimeout())); AuthenticationResultCache.getInstance().addToCache(cacheKey, cacheEntry); } diff --git a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/util/FrameworkUtilsTest.java b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/util/FrameworkUtilsTest.java index b38f09bacd34..2daf35c873b7 100644 --- a/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/util/FrameworkUtilsTest.java +++ b/components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/util/FrameworkUtilsTest.java @@ -60,7 +60,6 @@ import org.wso2.carbon.identity.application.authentication.framework.internal.FrameworkServiceComponent; import org.wso2.carbon.identity.application.authentication.framework.internal.FrameworkServiceDataHolder; import org.wso2.carbon.identity.application.authentication.framework.model.AuthenticationResult; -import org.wso2.carbon.identity.application.authentication.framework.store.SessionDataStore; import org.wso2.carbon.identity.application.common.model.ClaimConfig; import org.wso2.carbon.identity.application.common.model.ClaimMapping; import org.wso2.carbon.identity.application.common.model.IdentityProvider; @@ -86,18 +85,15 @@ import javax.servlet.http.HttpServletResponse; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mockStatic; import static org.mockito.Mockito.never; -import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; -import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertNull; import static org.testng.Assert.assertTrue; import static org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.REQUEST_PARAM_SP; @@ -150,15 +146,6 @@ public class FrameworkUtilsTest extends IdentityBaseTest { @Mock private ClaimMapping mockedClaimMapping; - @Mock - private SessionDataStore mockedSessionDataStore; - - @Mock - private AuthenticationResultCacheEntry mockedAuthenticationResultCacheEntry; - - @Mock - private AuthenticationResult mockedAuthenticationResult; - @Captor ArgumentCaptor cookieCaptor; @@ -500,59 +487,6 @@ public void testGetAuthenticationResultFromCache() { } } - @Test - public void testGetAuthenticationResultFromSessionDataStore() { - - try (MockedStatic sessionDataStore = mockStatic(SessionDataStore.class); - MockedStatic identityUtil = mockStatic(IdentityUtil.class)) { - - sessionDataStore.when(SessionDataStore::getInstance).thenReturn(mockedSessionDataStore); - identityUtil.when(() -> IdentityUtil.getProperty("JDBCPersistenceManager.SessionDataPersist.Temporary")) - .thenReturn("true"); - - AuthenticationResultCache authenticationCacheSpy = spy(AuthenticationResultCache.getInstance()); - - AuthenticationResultCacheKey key = new AuthenticationResultCacheKey(DUMMY_CACHE_KEY); - - when(authenticationCacheSpy.getValueFromCache(eq(key), anyString())).thenReturn(null); - - when(mockedSessionDataStore.getSessionData(anyString(), anyString())) - .thenReturn(mockedAuthenticationResultCacheEntry); - when(mockedAuthenticationResultCacheEntry.getResult()).thenReturn(mockedAuthenticationResult); - when(mockedAuthenticationResultCacheEntry.getValidityPeriod()).thenReturn(60000000L); - when(mockedAuthenticationResult.getProperty(anyString())).thenReturn(System.currentTimeMillis()); - - AuthenticationResultCacheEntry result = authenticationCacheSpy.getValueFromCache(key); - assertNotNull(result); - } - } - - @Test - public void testGetAuthenticationResultFromSessionDataStoreExpired() { - - try (MockedStatic sessionDataStore = mockStatic(SessionDataStore.class); - MockedStatic identityUtil = mockStatic(IdentityUtil.class)) { - - sessionDataStore.when(SessionDataStore::getInstance).thenReturn(mockedSessionDataStore); - identityUtil.when(() -> IdentityUtil.getProperty("JDBCPersistenceManager.SessionDataPersist.Temporary")) - .thenReturn("true"); - - AuthenticationResultCache authenticationCacheSpy = spy(AuthenticationResultCache.getInstance()); - - AuthenticationResultCacheKey key = new AuthenticationResultCacheKey(DUMMY_CACHE_KEY); - - when(authenticationCacheSpy.getValueFromCache(eq(key), anyString())).thenReturn(null); - - when(mockedSessionDataStore.getSessionData(anyString(), anyString())) - .thenReturn(mockedAuthenticationResultCacheEntry); - when(mockedAuthenticationResultCacheEntry.getResult()).thenReturn(mockedAuthenticationResult); - when(mockedAuthenticationResult.getProperty(anyString())).thenReturn(System.currentTimeMillis()); - - AuthenticationResultCacheEntry result = authenticationCacheSpy.getValueFromCache(key); - assertNull(result); - } - } - @Test public void testRemoveAuthenticationResultFromCache() {