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;
}
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);
}
}
}
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;
}
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;
}
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);
}
}
Aggregations