Search in sources :

Example 26 with SessionContext

use of org.wso2.carbon.identity.application.authentication.framework.context.SessionContext in project carbon-identity-framework by wso2.

the class ServerSessionManagementServiceImpl method terminateSession.

/**
 * Terminate the session by sessionId
 *
 * @param sessionContext - session context for the sessionId
 * @param sessionId - Session id of the federated user
 */
private void terminateSession(SessionContext sessionContext, String sessionId) {
    if (FrameworkServiceDataHolder.getInstance().getAuthnDataPublisherProxy() != null && FrameworkServiceDataHolder.getInstance().getAuthnDataPublisherProxy().isEnabled(null) && sessionContext != null) {
        Object authenticatedUserObj = sessionContext.getProperty(FrameworkConstants.AUTHENTICATED_USER);
        AuthenticatedUser authenticatedUser = new AuthenticatedUser();
        if (authenticatedUserObj != null) {
            authenticatedUser = (AuthenticatedUser) authenticatedUserObj;
        }
        FrameworkUtils.publishSessionEvent(sessionId, null, null, sessionContext, authenticatedUser, FrameworkConstants.AnalyticsAttributes.SESSION_TERMINATE);
    }
    if (sessionContext == null) {
        if (log.isDebugEnabled()) {
            log.debug("The session context is not available for " + sessionId);
        }
        return;
    }
    Object tenantDomainObj = sessionContext.getProperty(FrameworkUtils.TENANT_DOMAIN);
    if (tenantDomainObj != null) {
        SessionContextCache.getInstance().clearCacheEntry(sessionId, (String) tenantDomainObj);
    } else {
        SessionContextCache.getInstance().clearCacheEntry(sessionId);
    }
}
Also used : AuthenticatedUser(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser)

Example 27 with SessionContext

use of org.wso2.carbon.identity.application.authentication.framework.context.SessionContext in project carbon-identity-framework by wso2.

the class ServerSessionManagementServiceImpl method removeSession.

@Override
public boolean removeSession(String sessionId) {
    if (StringUtils.isBlank(sessionId)) {
        return false;
    }
    // Retrieve session information from cache in order to publish event
    SessionContext sessionContext = FrameworkUtils.getSessionContextFromCache(sessionId, FrameworkUtils.getLoginTenantDomainFromContext());
    terminateSession(sessionContext, sessionId);
    return true;
}
Also used : SessionContext(org.wso2.carbon.identity.application.authentication.framework.context.SessionContext)

Example 28 with SessionContext

use of org.wso2.carbon.identity.application.authentication.framework.context.SessionContext in project carbon-identity-framework by wso2.

the class UserSessionManagementServiceImpl method getActiveSessionList.

/**
 * Returns the active sessions from given list of session IDs.
 *
 * @param sessionIdList list of sessionIds
 * @return list of user sessions
 * @throws SessionManagementServerException if an error occurs when retrieving the UserSessions.
 */
private List<UserSession> getActiveSessionList(List<String> sessionIdList) throws SessionManagementServerException {
    List<UserSession> sessionsList = new ArrayList<>();
    for (String sessionId : sessionIdList) {
        if (sessionId != null) {
            SessionContext sessionContext = FrameworkUtils.getSessionContextFromCache(sessionId, FrameworkUtils.getLoginTenantDomainFromContext());
            if (sessionContext != null) {
                UserSessionDAO userSessionDTO = new UserSessionDAOImpl();
                UserSession userSession = userSessionDTO.getSession(sessionId);
                if (userSession != null) {
                    sessionsList.add(userSession);
                }
            }
        }
    }
    return sessionsList;
}
Also used : UserSession(org.wso2.carbon.identity.application.authentication.framework.model.UserSession) ArrayList(java.util.ArrayList) UserSessionDAOImpl(org.wso2.carbon.identity.application.authentication.framework.dao.impl.UserSessionDAOImpl) SessionContext(org.wso2.carbon.identity.application.authentication.framework.context.SessionContext) UserSessionDAO(org.wso2.carbon.identity.application.authentication.framework.dao.UserSessionDAO)

Example 29 with SessionContext

use of org.wso2.carbon.identity.application.authentication.framework.context.SessionContext in project carbon-identity-framework by wso2.

the class FrameworkUtilsTest method testGetSessionContextFromCacheExpiredSession.

@Test
public void testGetSessionContextFromCacheExpiredSession() throws FrameworkException {
    cacheEntry.setContext(context);
    setMockedSessionContextCache();
    when(mockedSessionContextCache.getValueFromCache(cacheKey)).thenReturn(cacheEntry);
    when(mockedSessionContextCache.isSessionExpired(any(SessionContextCacheKey.class), any(SessionContextCacheEntry.class))).thenReturn(true);
    IdentityEventService identityEventService = new IdentityEventServiceImpl(Collections.EMPTY_LIST, 1);
    FrameworkServiceDataHolder.getInstance().setIdentityEventService(identityEventService);
    AuthenticationContext authenticationContext = new AuthenticationContext();
    SessionContext sessionContext = FrameworkUtils.getSessionContextFromCache(request, authenticationContext, DUMMY_CACHE_KEY);
    assertNull(sessionContext);
}
Also used : AuthenticationContext(org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext) IdentityEventServiceImpl(org.wso2.carbon.identity.event.services.IdentityEventServiceImpl) SessionContextCacheEntry(org.wso2.carbon.identity.application.authentication.framework.cache.SessionContextCacheEntry) SessionContext(org.wso2.carbon.identity.application.authentication.framework.context.SessionContext) SessionContextCacheKey(org.wso2.carbon.identity.application.authentication.framework.cache.SessionContextCacheKey) IdentityEventService(org.wso2.carbon.identity.event.services.IdentityEventService) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Example 30 with SessionContext

use of org.wso2.carbon.identity.application.authentication.framework.context.SessionContext in project carbon-identity-framework by wso2.

the class FrameworkUtilsTest method testGetSessionContextFromCacheValidCacheEntry.

@Test
public void testGetSessionContextFromCacheValidCacheEntry() {
    cacheEntry.setContext(context);
    setMockedSessionContextCache();
    when(mockedSessionContextCache.getValueFromCache(cacheKey, "abc")).thenReturn(cacheEntry);
    SessionContext sessionContext = FrameworkUtils.getSessionContextFromCache(DUMMY_CACHE_KEY, "abc");
    assertEquals(sessionContext, context);
}
Also used : SessionContext(org.wso2.carbon.identity.application.authentication.framework.context.SessionContext) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Aggregations

SessionContext (org.wso2.carbon.identity.application.authentication.framework.context.SessionContext)25 AuthenticatedUser (org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser)14 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7 Test (org.testng.annotations.Test)7 SessionContextCacheKey (org.wso2.carbon.identity.application.authentication.framework.cache.SessionContextCacheKey)7 HashMap (java.util.HashMap)6 BeforeTest (org.testng.annotations.BeforeTest)6 SessionContextCacheEntry (org.wso2.carbon.identity.application.authentication.framework.cache.SessionContextCacheEntry)6 SequenceConfig (org.wso2.carbon.identity.application.authentication.framework.config.model.SequenceConfig)6 Event (org.wso2.carbon.identity.event.event.Event)6 JSONObject (org.json.JSONObject)5 AuthenticationDataPublisher (org.wso2.carbon.identity.application.authentication.framework.AuthenticationDataPublisher)4 FrameworkException (org.wso2.carbon.identity.application.authentication.framework.exception.FrameworkException)4 PowerMockIdentityBaseTest (org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)4 ArrayList (java.util.ArrayList)3 Cookie (javax.servlet.http.Cookie)3 AuthHistory (org.wso2.carbon.identity.application.authentication.framework.context.AuthHistory)3 AuthenticationContext (org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext)3 Map (java.util.Map)2 AfterTest (org.testng.annotations.AfterTest)2