Search in sources :

Example 21 with SessionContext

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

the class FrameworkUtils method getSessionContextFromCache.

/**
 * Retrieve session context from the session cache.
 *
 * @param request           HttpServletRequest.
 * @param context           Authentication context.
 * @param sessionContextKey Session context key.
 * @return Session context key.
 * @throws FrameworkException Error in triggering session expire event.
 */
public static SessionContext getSessionContextFromCache(HttpServletRequest request, AuthenticationContext context, String sessionContextKey) throws FrameworkException {
    SessionContext sessionContext = null;
    if (StringUtils.isNotBlank(sessionContextKey)) {
        SessionContextCacheKey cacheKey = new SessionContextCacheKey(sessionContextKey);
        SessionContextCache sessionContextCache = SessionContextCache.getInstance();
        SessionContextCacheEntry cacheEntry = sessionContextCache.getSessionContextCacheEntry(cacheKey, context.getLoginTenantDomain());
        if (cacheEntry != null) {
            sessionContext = cacheEntry.getContext();
            boolean isSessionExpired = sessionContextCache.isSessionExpired(cacheKey, cacheEntry);
            if (isSessionExpired) {
                triggerSessionExpireEvent(request, context, sessionContext);
                if (log.isDebugEnabled()) {
                    log.debug("A SESSION_EXPIRE event was fired for the expired session found corresponding " + "to the key: " + cacheKey.getContextId());
                }
                return null;
            }
        }
    }
    return sessionContext;
}
Also used : 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) SessionContextCache(org.wso2.carbon.identity.application.authentication.framework.cache.SessionContextCache)

Example 22 with SessionContext

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

the class FrameworkUtils method publishSessionEvent.

public static void publishSessionEvent(String sessionId, HttpServletRequest request, AuthenticationContext context, SessionContext sessionContext, AuthenticatedUser user, String status) {
    AuthenticationDataPublisher authnDataPublisherProxy = FrameworkServiceDataHolder.getInstance().getAuthnDataPublisherProxy();
    if (authnDataPublisherProxy != null && authnDataPublisherProxy.isEnabled(context)) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put(FrameworkConstants.AnalyticsAttributes.USER, user);
        paramMap.put(FrameworkConstants.AnalyticsAttributes.SESSION_ID, sessionId);
        String isPublishingSessionCountEnabledValue = IdentityUtil.getProperty(FrameworkConstants.Config.PUBLISH_ACTIVE_SESSION_COUNT);
        boolean isPublishingSessionCountEnabled = Boolean.parseBoolean(isPublishingSessionCountEnabledValue);
        if (isPublishingSessionCountEnabled) {
            paramMap.put(FrameworkConstants.AnalyticsAttributes.ACTIVE_SESSION_COUNT, getActiveSessionCount(user.getTenantDomain()));
        }
        Map<String, Object> unmodifiableParamMap = Collections.unmodifiableMap(paramMap);
        if (FrameworkConstants.AnalyticsAttributes.SESSION_CREATE.equalsIgnoreCase(status)) {
            authnDataPublisherProxy.publishSessionCreation(request, context, sessionContext, unmodifiableParamMap);
        } else if (FrameworkConstants.AnalyticsAttributes.SESSION_UPDATE.equalsIgnoreCase(status)) {
            authnDataPublisherProxy.publishSessionUpdate(request, context, sessionContext, unmodifiableParamMap);
        } else if (FrameworkConstants.AnalyticsAttributes.SESSION_TERMINATE.equalsIgnoreCase(status)) {
            authnDataPublisherProxy.publishSessionTermination(request, context, sessionContext, unmodifiableParamMap);
        }
    }
}
Also used : HashMap(java.util.HashMap) JSONObject(org.json.JSONObject) AuthenticationDataPublisher(org.wso2.carbon.identity.application.authentication.framework.AuthenticationDataPublisher)

Example 23 with SessionContext

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

the class FrameworkUtils method getSessionContextFromCache.

/**
 * @param key
 * @param loginTenantDomain
 * @return
 */
public static SessionContext getSessionContextFromCache(String key, String loginTenantDomain) {
    SessionContext sessionContext = null;
    if (StringUtils.isNotBlank(key)) {
        SessionContextCacheKey cacheKey = new SessionContextCacheKey(key);
        Object cacheEntryObj = SessionContextCache.getInstance().getValueFromCache(cacheKey, loginTenantDomain);
        if (cacheEntryObj != null) {
            sessionContext = ((SessionContextCacheEntry) cacheEntryObj).getContext();
        }
    }
    return sessionContext;
}
Also used : SessionContext(org.wso2.carbon.identity.application.authentication.framework.context.SessionContext) SessionContextCacheKey(org.wso2.carbon.identity.application.authentication.framework.cache.SessionContextCacheKey) JSONObject(org.json.JSONObject)

Example 24 with SessionContext

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

the class SessionExtenderProcessor method process.

@Override
public IdentityResponse.IdentityResponseBuilder process(IdentityRequest identityRequest) throws SessionExtenderClientException {
    if (log.isDebugEnabled()) {
        log.debug("Request processing started by SessionExtenderProcessor.");
    }
    SessionExtenderRequest sessionExtenderRequest = (SessionExtenderRequest) identityRequest;
    String tenantDomain = sessionExtenderRequest.getTenantDomain();
    String sessionKey = getSessionKey(sessionExtenderRequest);
    SessionContextCacheKey sessionContextCacheKey = new SessionContextCacheKey(sessionKey);
    SessionContextCacheEntry sessionContextCacheEntry = SessionContextCache.getInstance().getSessionContextCacheEntry(sessionContextCacheKey, tenantDomain);
    if (sessionContextCacheEntry == null) {
        if (log.isDebugEnabled()) {
            log.debug("No session available for requested session identifier: " + sessionKey);
        }
        throw new SessionExtenderClientException(SessionExtenderConstants.Error.SESSION_NOT_AVAILABLE.getCode(), SessionExtenderConstants.Error.SESSION_NOT_AVAILABLE.getMessage(), "No session available for requested session identifier.");
    }
    SessionContext sessionContext = sessionContextCacheEntry.getContext();
    long currentTime = System.currentTimeMillis();
    FrameworkUtils.updateSessionLastAccessTimeMetadata(sessionKey, currentTime);
    FrameworkUtils.addSessionContextToCache(sessionKey, sessionContext, tenantDomain, tenantDomain);
    String traceId = FrameworkUtils.getCorrelation();
    fireEvent(sessionKey, sessionContext, tenantDomain, traceId);
    addAuditLogs(sessionKey, tenantDomain, traceId);
    SessionExtenderResponse.SessionExtenderResponseBuilder responseBuilder = new SessionExtenderResponse.SessionExtenderResponseBuilder();
    responseBuilder.setTraceId(traceId);
    return responseBuilder;
}
Also used : SessionExtenderResponse(org.wso2.carbon.identity.application.authentication.framework.session.extender.response.SessionExtenderResponse) SessionExtenderClientException(org.wso2.carbon.identity.application.authentication.framework.session.extender.exception.SessionExtenderClientException) 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) SessionExtenderRequest(org.wso2.carbon.identity.application.authentication.framework.session.extender.request.SessionExtenderRequest)

Example 25 with SessionContext

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

the class SessionExtenderProcessor method fireEvent.

private void fireEvent(String sessionId, SessionContext sessionContext, String tenantDomain, String traceId) {
    IdentityEventService eventService = FrameworkServiceDataHolder.getInstance().getIdentityEventService();
    try {
        Map<String, Object> eventProperties = new HashMap<>();
        eventProperties.put(IdentityEventConstants.EventProperty.SESSION_CONTEXT_ID, sessionId);
        eventProperties.put(IdentityEventConstants.EventProperty.SESSION_CONTEXT, sessionContext);
        eventProperties.put(IdentityEventConstants.EventProperty.TENANT_DOMAIN, tenantDomain);
        eventProperties.put(IdentityEventConstants.EventProperty.TRACE_ID, traceId);
        Event event = new Event(IdentityEventConstants.Event.SESSION_EXTENSION, eventProperties);
        eventService.handleEvent(event);
    } catch (IdentityEventException e) {
        String errorLog = "Could not fire event " + IdentityEventConstants.Event.SESSION_EXTENSION + " when extending the session with session ID " + sessionId + " in tenant domain " + tenantDomain;
        log.error(errorLog, e);
    }
}
Also used : HashMap(java.util.HashMap) IdentityEventException(org.wso2.carbon.identity.event.IdentityEventException) Event(org.wso2.carbon.identity.event.event.Event) JSONObject(org.json.JSONObject) IdentityEventService(org.wso2.carbon.identity.event.services.IdentityEventService)

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