use of org.wso2.carbon.identity.event.services.IdentityEventService in project carbon-identity-framework by wso2.
the class AbstractApplicationAuthenticator method handlePostAuthentication.
private void handlePostAuthentication(AuthenticationContext context) throws AuthenticationFailedException {
Map<String, Object> eventProperties = new HashMap<>();
String username = MultitenantUtils.getTenantAwareUsername(context.getSubject().toFullQualifiedUsername());
if (context.getSubject().isFederatedUser()) {
username = UserCoreUtil.removeDomainFromName(username);
}
String tenantDomain = context.getTenantDomain();
IdentityEventService identityEventService = FrameworkServiceDataHolder.getInstance().getIdentityEventService();
RealmService realmService = FrameworkServiceDataHolder.getInstance().getRealmService();
try {
UserRealm userRealm = realmService.getTenantUserRealm(IdentityTenantUtil.getTenantId(tenantDomain));
eventProperties.put(IdentityEventConstants.EventProperty.USER_NAME, username);
eventProperties.put(IdentityEventConstants.EventProperty.USER_STORE_MANAGER, userRealm.getUserStoreManager());
eventProperties.put(IdentityEventConstants.EventProperty.TENANT_DOMAIN, tenantDomain);
if (context.isRequestAuthenticated()) {
eventProperties.put(IdentityEventConstants.EventProperty.OPERATION_STATUS, true);
} else {
eventProperties.put(IdentityEventConstants.EventProperty.OPERATION_STATUS, false);
}
Event event = new Event(IdentityEventConstants.Event.POST_AUTHENTICATION, eventProperties);
identityEventService.handleEvent(event);
} catch (UserStoreException e) {
throw new AuthenticationFailedException(ErrorMessages.SYSTEM_ERROR_WHILE_AUTHENTICATING.getCode(), " Error in accessing user store in tenant: " + tenantDomain, e);
} catch (IdentityEventException e) {
throw new AuthenticationFailedException(ErrorMessages.SYSTEM_ERROR_WHILE_AUTHENTICATING.getCode(), " Error while handling post authentication event for user: " + username + " in tenant: " + tenantDomain, e);
}
}
use of org.wso2.carbon.identity.event.services.IdentityEventService in project carbon-identity-framework by wso2.
the class AbstractLocalApplicationAuthenticator method fireEvent.
/**
* To fire the events for account locking.
*
* @param context the authentication context
* @param eventName the event name
* @param operationStatus the success or failure status
* @throws AuthenticationFailedException the exception in the authentication flow
*/
private void fireEvent(AuthenticationContext context, String eventName, boolean operationStatus) throws AuthenticationFailedException {
if (eventFiringEnabledForAccountLocking()) {
IdentityEventService eventService = FrameworkServiceDataHolder.getInstance().getIdentityEventService();
try {
Map<String, Object> eventProperties = new HashMap<>();
String userName = (String) context.getProperty(FrameworkConstants.USERNAME);
String tenantAwareUsername = MultitenantUtils.getTenantAwareUsername(userName);
String tenantDomain = context.getTenantDomain();
int tenantID = IdentityTenantUtil.getTenantId(tenantDomain);
RealmService realmService = FrameworkServiceDataHolder.getInstance().getRealmService();
UserRealm userRealm = realmService.getTenantUserRealm(tenantID);
eventProperties.put(IdentityEventConstants.EventProperty.USER_NAME, tenantAwareUsername);
eventProperties.put(IdentityEventConstants.EventProperty.USER_STORE_MANAGER, userRealm.getUserStoreManager());
eventProperties.put(IdentityEventConstants.EventProperty.TENANT_DOMAIN, tenantDomain);
eventProperties.put(IdentityEventConstants.EventProperty.OPERATION_STATUS, operationStatus);
Event event = new Event(eventName, eventProperties);
eventService.handleEvent(event);
} catch (UserStoreException e) {
throw new AuthenticationFailedException(ErrorMessages.SYSTEM_ERROR_WHILE_AUTHENTICATING.getCode(), " Error in accessing user store ", e);
} catch (IdentityEventException e) {
throw new AuthenticationFailedException(ErrorMessages.SYSTEM_ERROR_WHILE_AUTHENTICATING.getCode(), " Error while firing the events ", e);
}
}
}
use of org.wso2.carbon.identity.event.services.IdentityEventService 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);
}
}
use of org.wso2.carbon.identity.event.services.IdentityEventService 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);
}
use of org.wso2.carbon.identity.event.services.IdentityEventService in project carbon-identity-framework by wso2.
the class LoggerUtils method triggerAuditLogEvent.
/**
* @param initiatorId Request initiator's id.
* @param initiatorName Request initiator's name.
* @param initiatorType Request initiator's type.
* @param evenType State changing event name.
* @param targetId Target resource's id.
* @param targetName Target resource's name.
* @param targetType Target resource type.
* @param dataChange Changing data.
*/
public static void triggerAuditLogEvent(String initiatorId, String initiatorName, String initiatorType, String evenType, String targetId, String targetName, String targetType, String dataChange) {
try {
// Publish new audit logs only if the old audit log publishing is disabled.
if (isLegacyAuditLogsDisabled()) {
Map<String, Object> addAuditLogProperties = new HashMap<>();
String id = UUID.randomUUID().toString();
Instant recordedAt = parseDateTime(Instant.now().toString());
String clientComponent = MDC.get(CLIENT_COMPONENT);
String correlationId = MDC.get(CORRELATION_ID_MDC);
AuditLog auditLog = new AuditLog(id, recordedAt, clientComponent, correlationId, initiatorId, initiatorName, initiatorType, evenType, targetId, targetName, targetType, dataChange);
addAuditLogProperties.put(CarbonConstants.LogEventConstants.AUDIT_LOG, auditLog);
IdentityEventService eventMgtService = CentralLogMgtServiceComponentHolder.getInstance().getIdentityEventService();
Event auditEvent = new Event(PUBLISH_AUDIT_LOG, addAuditLogProperties);
eventMgtService.handleEvent(auditEvent);
}
} catch (IdentityEventException e) {
String errorLog = "Error occurred when firing the event. Unable to audit the request.";
log.error(errorLog, e);
}
}
Aggregations