Search in sources :

Example 6 with IdentityEventException

use of org.wso2.carbon.identity.event.IdentityEventException in project carbon-identity-framework by wso2.

the class AbstractEventHandler method getSubscriptionProperties.

/**
 * Each event has its own subscriptions (configure in identity-event.properties) and it is possible to define
 * multiple properties for each subscription per event under the given module.
 * This method will allow to get the properties for event subscription under the current module.
 *
 * @param eventName Current Event Name
 * @return Property Map for the given event name under the current module.
 * @throws IdentityEventException
 */
public Properties getSubscriptionProperties(String eventName) throws IdentityEventException {
    if (log.isDebugEnabled()) {
        log.debug("Get the subscription properties for event : " + eventName);
    }
    Properties subscriptionProperties = new Properties();
    List<Subscription> subscriptions = configs.getSubscriptions();
    for (Subscription sub : subscriptions) {
        if (sub.getSubscriptionName().equals(eventName)) {
            subscriptionProperties = sub.getSubscriptionProperties();
            break;
        }
    }
    if (log.isDebugEnabled() && subscriptionProperties != null) {
        log.debug("List of subscription properties for event : " + eventName);
        for (Object key : subscriptionProperties.keySet()) {
            log.debug("Key : " + key + " Value : " + subscriptionProperties.getProperty((String) key));
        }
    }
    return subscriptionProperties;
}
Also used : Properties(java.util.Properties) Subscription(org.wso2.carbon.identity.event.bean.Subscription)

Example 7 with IdentityEventException

use of org.wso2.carbon.identity.event.IdentityEventException in project carbon-identity-framework by wso2.

the class IdentityEventServiceComponent method registerEventHandler.

@Reference(name = "event.handler", service = org.wso2.carbon.identity.event.handler.AbstractEventHandler.class, cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC, unbind = "unRegisterEventHandler")
protected void registerEventHandler(AbstractEventHandler eventHandler) {
    String handlerName = eventHandler.getName();
    try {
        eventHandler.init(IdentityEventConfigBuilder.getInstance().getModuleConfigurations(handlerName));
    } catch (IdentityEventException | IdentityRuntimeException e) {
        log.warn("Properties for " + handlerName + " is not configured. This event handler will not be activated");
    }
    eventHandlerList.add(eventHandler);
    MessageHandlerComparator messageHandlerComparator = new MessageHandlerComparator(null);
    Collections.sort(eventHandlerList, messageHandlerComparator);
}
Also used : IdentityEventException(org.wso2.carbon.identity.event.IdentityEventException) MessageHandlerComparator(org.wso2.carbon.identity.core.handler.MessageHandlerComparator) IdentityRuntimeException(org.wso2.carbon.identity.base.IdentityRuntimeException) Reference(org.osgi.service.component.annotations.Reference)

Example 8 with IdentityEventException

use of org.wso2.carbon.identity.event.IdentityEventException in project identity-conditional-auth-functions by wso2-extensions.

the class SendEmailFunctionImpl method sendMail.

@Override
public boolean sendMail(JsAuthenticatedUser user, String templateId, Map<String, String> paramMap) {
    String eventName = IdentityEventConstants.Event.TRIGGER_NOTIFICATION;
    HashMap<String, Object> properties = new HashMap<>(paramMap);
    properties.put(IdentityEventConstants.EventProperty.USER_NAME, user.getWrapped().getUserName());
    properties.put(IdentityEventConstants.EventProperty.TENANT_DOMAIN, user.getWrapped().getTenantDomain());
    properties.put(IdentityEventConstants.EventProperty.USER_STORE_DOMAIN, user.getWrapped().getUserStoreDomain());
    properties.put(TEMPLATE_TYPE, templateId);
    Event identityMgtEvent = new Event(eventName, properties);
    try {
        NotificationFunctionServiceHolder.getInstance().getIdentityEventService().handleEvent(identityMgtEvent);
    } catch (IdentityEventException | NotificationRuntimeException e) {
        LOG.error(String.format("Error when sending notification of template %s to user %s", templateId, user.getWrapped().toFullQualifiedUsername()), e);
        return false;
    }
    return true;
}
Also used : HashMap(java.util.HashMap) IdentityEventException(org.wso2.carbon.identity.event.IdentityEventException) Event(org.wso2.carbon.identity.event.event.Event) NotificationRuntimeException(org.wso2.carbon.identity.event.handler.notification.exception.NotificationRuntimeException)

Example 9 with IdentityEventException

use of org.wso2.carbon.identity.event.IdentityEventException in project identity-conditional-auth-functions by wso2-extensions.

the class PublishToAnalyticsFunctionImpl method publishToAnalytics.

@Override
public void publishToAnalytics(Map<String, String> metadata, Map<String, Object> payloadData, JsAuthenticationContext context) {
    String appName = metadata.get(PARAM_APP_NAME);
    String inputStream = metadata.get(PARAM_INPUT_STREAM);
    String targetPath = metadata.get(PARAM_EP_URL);
    String epUrl = null;
    try {
        if (appName != null && inputStream != null) {
            epUrl = "/" + appName + "/" + inputStream;
        } else if (targetPath != null) {
            epUrl = targetPath;
        } else {
            LOG.error("Target path cannot be found.");
            return;
        }
        String tenantDomain = context.getContext().getTenantDomain();
        String targetHostUrl = CommonUtils.getConnectorConfig(AnalyticsEngineConfigImpl.RECEIVER, tenantDomain);
        if (targetHostUrl == null) {
            LOG.error("Target host cannot be found.");
            return;
        }
        HttpPost request = new HttpPost(epUrl);
        request.setHeader(CONTENT_TYPE, TYPE_APPLICATION_JSON);
        handleAuthentication(request, tenantDomain);
        JSONObject jsonObject = new JSONObject();
        JSONObject event = new JSONObject();
        for (Map.Entry<String, Object> dataElements : payloadData.entrySet()) {
            event.put(dataElements.getKey(), dataElements.getValue());
        }
        jsonObject.put("event", event);
        request.setEntity(new StringEntity(jsonObject.toJSONString()));
        String[] targetHostUrls = targetHostUrl.split(";");
        HttpHost[] targetHosts = new HttpHost[targetHostUrls.length];
        for (int i = 0; i < targetHostUrls.length; i++) {
            URL hostUrl = new URL(targetHostUrls[i]);
            targetHosts[i] = new HttpHost(hostUrl.getHost(), hostUrl.getPort(), hostUrl.getProtocol());
        }
        CloseableHttpAsyncClient client = ClientManager.getInstance().getClient(tenantDomain);
        for (final HttpHost targetHost : targetHosts) {
            client.execute(targetHost, request, new FutureCallback<HttpResponse>() {

                @Override
                public void completed(final HttpResponse response) {
                    int responseCode = response.getStatusLine().getStatusCode();
                    if (responseCode == 200) {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("Successfully published data to the analytics for session data key: " + context.getContext().getContextIdentifier());
                        }
                    } else {
                        LOG.error("Error while publishing data to analytics engine for session data key: " + context.getContext().getContextIdentifier() + ". Request completed successfully. " + "But response code was not 200");
                    }
                }

                @Override
                public void failed(final Exception ex) {
                    LOG.error("Error while publishing data to analytics engine for session data key: " + context.getContext().getContextIdentifier() + ". Request failed with: " + ex);
                }

                @Override
                public void cancelled() {
                    LOG.error("Error while publishing data to analytics engine for session data key: " + context.getContext().getContextIdentifier() + ". Request canceled.");
                }
            });
        }
    } catch (IOException e) {
        LOG.error("Error while calling analytics engine for tenant: " + context.getContext().getTenantDomain(), e);
    } catch (IdentityEventException e) {
        LOG.error("Error while preparing authentication information for tenant: " + context.getContext().getTenantDomain(), e);
    } catch (FrameworkException e) {
        LOG.error("Error while building client to invoke analytics engine for tenant: " + context.getContext().getTenantDomain(), e);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) FrameworkException(org.wso2.carbon.identity.application.authentication.framework.exception.FrameworkException) IdentityEventException(org.wso2.carbon.identity.event.IdentityEventException) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) URL(java.net.URL) IdentityEventException(org.wso2.carbon.identity.event.IdentityEventException) IOException(java.io.IOException) FrameworkException(org.wso2.carbon.identity.application.authentication.framework.exception.FrameworkException) StringEntity(org.apache.http.entity.StringEntity) JSONObject(org.json.simple.JSONObject) HttpHost(org.apache.http.HttpHost) CloseableHttpAsyncClient(org.apache.http.impl.nio.client.CloseableHttpAsyncClient) JSONObject(org.json.simple.JSONObject) Map(java.util.Map)

Example 10 with IdentityEventException

use of org.wso2.carbon.identity.event.IdentityEventException in project identity-conditional-auth-functions by wso2-extensions.

the class AbstractAnalyticsFunction method handleAuthentication.

/**
 * Handle the authentication of the external analytics calls.
 *
 * @param request      Request sent to the analytics engine.
 * @param tenantDomain tenant domain of the service provider.
 * @throws IdentityEventException
 */
protected void handleAuthentication(HttpPost request, String tenantDomain) throws IdentityEventException {
    if (Boolean.parseBoolean(isBasicAuthEnabled(tenantDomain))) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Basic Authentication enabled for outbound analytics calls in the tenant:" + tenantDomain);
        }
        String username = getUsername(tenantDomain);
        String password = getPassword(tenantDomain);
        AuthenticationManager authenticationManager = authenticationFactory.getAuthenticationManager("Basic");
        request.setHeader(authenticationManager.authenticate(new UsernamePasswordCredentials(username, password), request));
    } else {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Basic Authentication is not enabled for outbound analytics calls in for the tenant:" + tenantDomain);
        }
    }
}
Also used : AuthenticationManager(org.wso2.carbon.identity.conditional.auth.functions.common.auth.AuthenticationManager) UsernamePasswordCredentials(org.wso2.carbon.identity.conditional.auth.functions.common.auth.UsernamePasswordCredentials)

Aggregations

IdentityEventException (org.wso2.carbon.identity.event.IdentityEventException)75 HashMap (java.util.HashMap)42 Event (org.wso2.carbon.identity.event.event.Event)39 UserStoreManager (org.wso2.carbon.user.core.UserStoreManager)17 User (org.wso2.carbon.identity.application.common.model.User)14 IdentityRecoveryException (org.wso2.carbon.identity.recovery.IdentityRecoveryException)14 UserRecoveryData (org.wso2.carbon.identity.recovery.model.UserRecoveryData)13 UserRecoveryDataStore (org.wso2.carbon.identity.recovery.store.UserRecoveryDataStore)13 IdentityEventService (org.wso2.carbon.identity.event.services.IdentityEventService)12 Map (java.util.Map)11 UserStoreException (org.wso2.carbon.user.core.UserStoreException)10 JSONObject (org.json.JSONObject)9 UserStoreException (org.wso2.carbon.user.api.UserStoreException)9 Property (org.wso2.carbon.identity.recovery.model.Property)7 Property (org.wso2.carbon.identity.application.common.model.Property)6 RealmService (org.wso2.carbon.user.core.service.RealmService)6 Test (org.testng.annotations.Test)5 Properties (java.util.Properties)4 IdentityGovernanceException (org.wso2.carbon.identity.governance.IdentityGovernanceException)4 NotificationChannelManagerException (org.wso2.carbon.identity.governance.exceptions.notiification.NotificationChannelManagerException)4