Search in sources :

Example 6 with IdentityEventService

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);
    }
}
Also used : UserRealm(org.wso2.carbon.user.api.UserRealm) HashMap(java.util.HashMap) AuthenticationFailedException(org.wso2.carbon.identity.application.authentication.framework.exception.AuthenticationFailedException) IdentityEventException(org.wso2.carbon.identity.event.IdentityEventException) RealmService(org.wso2.carbon.user.core.service.RealmService) UserStoreException(org.wso2.carbon.user.api.UserStoreException) Event(org.wso2.carbon.identity.event.event.Event) IdentityEventService(org.wso2.carbon.identity.event.services.IdentityEventService)

Example 7 with IdentityEventService

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);
        }
    }
}
Also used : UserRealm(org.wso2.carbon.user.api.UserRealm) HashMap(java.util.HashMap) AuthenticationFailedException(org.wso2.carbon.identity.application.authentication.framework.exception.AuthenticationFailedException) IdentityEventException(org.wso2.carbon.identity.event.IdentityEventException) RealmService(org.wso2.carbon.user.core.service.RealmService) UserStoreException(org.wso2.carbon.user.api.UserStoreException) Event(org.wso2.carbon.identity.event.event.Event) IdentityEventService(org.wso2.carbon.identity.event.services.IdentityEventService)

Example 8 with IdentityEventService

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

Example 9 with IdentityEventService

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);
}
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 10 with IdentityEventService

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);
    }
}
Also used : HashMap(java.util.HashMap) IdentityEventException(org.wso2.carbon.identity.event.IdentityEventException) Instant(java.time.Instant) Event(org.wso2.carbon.identity.event.event.Event) AuditLog(org.wso2.carbon.utils.AuditLog) IdentityEventService(org.wso2.carbon.identity.event.services.IdentityEventService)

Aggregations

IdentityEventService (org.wso2.carbon.identity.event.services.IdentityEventService)18 HashMap (java.util.HashMap)13 Test (org.testng.annotations.Test)10 IdentityEventException (org.wso2.carbon.identity.event.IdentityEventException)10 Event (org.wso2.carbon.identity.event.event.Event)10 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8 BeforeTest (org.testng.annotations.BeforeTest)8 RequestObject (org.wso2.carbon.identity.openidconnect.model.RequestObject)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 Matchers.anyString (org.mockito.Matchers.anyString)5 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)4 AfterTest (org.testng.annotations.AfterTest)4 OAuthServerConfiguration (org.wso2.carbon.identity.oauth.config.OAuthServerConfiguration)3 IdentityOAuth2Exception (org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception)3 OAuth2Parameters (org.wso2.carbon.identity.oauth2.model.OAuth2Parameters)3 Method (java.lang.reflect.Method)2 Instant (java.time.Instant)2 Hashtable (java.util.Hashtable)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 HttpMethod (javax.ws.rs.HttpMethod)2