Skip to content
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

Revert "Validate AuthenticationResultCache expiry" #6347

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<Cookie> cookieCaptor;

Expand Down Expand Up @@ -500,59 +487,6 @@ public void testGetAuthenticationResultFromCache() {
}
}

@Test
public void testGetAuthenticationResultFromSessionDataStore() {

try (MockedStatic<SessionDataStore> sessionDataStore = mockStatic(SessionDataStore.class);
MockedStatic<IdentityUtil> 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> sessionDataStore = mockStatic(SessionDataStore.class);
MockedStatic<IdentityUtil> 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() {

Expand Down
Loading