From 6e511b91f52c4b71ccff73c989894c76b285ebf0 Mon Sep 17 00:00:00 2001 From: Sachin-Mamoru Date: Fri, 19 Jul 2024 23:36:29 +0530 Subject: [PATCH] addressed review comments --- .../pom.xml | 10 - .../event/handler/LoginEventHookHandler.java | 35 +--- .../event/handler/constant/Constants.java | 11 +- .../EventHookHandlerServiceComponent.java | 21 +- .../event/handler/model/EventAttribute.java | 86 ++++++++ .../common/event/handler/model/EventData.java | 13 -- .../event/handler/model/ResourceConfig.java | 49 +++++ .../handler/util/EventHookHandlerUtils.java | 190 ++++++++++++++---- .../pom.xml | 10 - .../builder/WSO2LoginEventPayloadBuilder.java | 21 +- .../WSO2AuthenticationFailedEventPayload.java | 72 ++++++- ...WSO2AuthenticationSuccessEventPayload.java | 64 +++++- .../handler/model/WSO2BaseEventPayload.java | 32 +-- pom.xml | 18 -- 14 files changed, 455 insertions(+), 177 deletions(-) create mode 100644 components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/model/EventAttribute.java create mode 100644 components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/model/ResourceConfig.java diff --git a/components/org.wso2.identity.webhook.common.event.handler/pom.xml b/components/org.wso2.identity.webhook.common.event.handler/pom.xml index 9f88bf4..20bd3fd 100644 --- a/components/org.wso2.identity.webhook.common.event.handler/pom.xml +++ b/components/org.wso2.identity.webhook.common.event.handler/pom.xml @@ -81,15 +81,6 @@ org.wso2.orbit.org.apache.httpcomponents httpasyncclient - - com.wso2.identity.asgardeo.extensions - com.wso2.identity.asgardeo.event.configuration.mgt.core.service - - - com.wso2.identity.asgardeo.extensions - com.wso2.identity.asgardeo.config.mapper - - com.googlecode.json-simple.wso2 json-simple @@ -163,7 +154,6 @@ org.wso2.carbon.identity.data.publisher.authentication.analytics.login.*; version="${identity.datapublisher.authentication.version.range}", org.json.simple; version="${com.googlecode.json-simple.wso2.version.range}", org.json.simple.parser; version="${com.googlecode.json-simple.wso2.version.range}", - com.wso2.identity.asgardeo.event.configuration.mgt.core.service.*; version="${asgardeo.event.configuration.mgt.core.version.range}", javax.servlet.http; version="${imp.pkg.version.javax.servlet}", org.wso2.identity.event.common.publisher; version="${org.wso2.identity.event.publishers.version.range}", diff --git a/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/LoginEventHookHandler.java b/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/LoginEventHookHandler.java index 805cb1d..673864f 100644 --- a/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/LoginEventHookHandler.java +++ b/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/LoginEventHookHandler.java @@ -21,11 +21,9 @@ import org.wso2.identity.webhook.common.event.handler.builder.LoginEventPayloadBuilder; import org.wso2.identity.webhook.common.event.handler.constant.Constants; import org.wso2.identity.webhook.common.event.handler.internal.EventHookHandlerDataHolder; +import org.wso2.identity.webhook.common.event.handler.model.EventAttribute; import org.wso2.identity.webhook.common.event.handler.model.EventData; import org.wso2.identity.webhook.common.event.handler.util.EventHookHandlerUtils; -import com.wso2.identity.asgardeo.event.configuration.mgt.core.service.exception.EventConfigurationMgtServerException; -import com.wso2.identity.asgardeo.event.configuration.mgt.core.service.model.EventAttribute; -import com.wso2.identity.asgardeo.event.configuration.mgt.core.service.util.EventConfigurationMgtUtils; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; @@ -74,11 +72,9 @@ public boolean canHandle(MessageContext messageContext) throws IdentityRuntimeEx IdentityEventMessageContext identityContext = (IdentityEventMessageContext) messageContext; String eventName = identityContext.getEvent().getEventName(); - if (isSupportedEvent(eventName)) { - log.debug("canHandle() returning True for the event: " + eventName); - return true; - } - return false; + boolean canHandle = isSupportedEvent(eventName); + log.debug("canHandle() returning " + canHandle + " for the event: " + eventName); + return canHandle; } private boolean isSupportedEvent(String eventName) { @@ -122,18 +118,6 @@ public void handleEvent(Event event) throws IdentityEventException { } } - /** - * Check whether the login event handler is enabled. - * - * @return True if the login event handler is enabled. - */ - public boolean isLoginEventHandlerEnabled() { - - String enablePropertyKey = Constants.LOGIN_EVENT_HOOK_NAME + "." + Constants.ENABLE; - return this.configs != null && this.configs.getModuleProperties() != null && - Boolean.parseBoolean(configs.getModuleProperties().getProperty(enablePropertyKey)); - } - private EventAttribute getLoginEventPublisherConfigForTenant(String tenantDomain, String eventName) { if (StringUtils.isEmpty(tenantDomain) || MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) { @@ -146,15 +130,14 @@ private EventAttribute getLoginEventPublisherConfigForTenant(String tenantDomain .getTenantResources(tenantDomain, condition); return extractEventAttribute(publisherConfigResource, eventName); - } catch (ConfigurationManagementException | EventConfigurationMgtServerException e) { + } catch (ConfigurationManagementException | IdentityEventException e) { log.debug("Error while retrieving event publisher configuration for tenant.", e); } return new EventAttribute(); } - private EventAttribute extractEventAttribute(Resources publisherConfigResource, String eventName) - throws EventConfigurationMgtServerException { + private EventAttribute extractEventAttribute(Resources publisherConfigResource, String eventName) throws IdentityEventException { if (CollectionUtils.isNotEmpty(publisherConfigResource.getResources()) && publisherConfigResource.getResources().get(0) != null && @@ -162,7 +145,7 @@ private EventAttribute extractEventAttribute(Resources publisherConfigResource, for (Attribute attribute : publisherConfigResource.getResources().get(0).getAttributes()) { if (isMatchingEventAttribute(attribute, eventName)) { - return EventConfigurationMgtUtils.buildEventAttributeFromJSONString(attribute.getValue()); + return EventHookHandlerUtils.buildEventAttributeFromJSONString(attribute.getValue()); } } } @@ -180,8 +163,8 @@ private boolean isMatchingEventAttribute(Attribute attribute, String eventName) private ComplexCondition createPublisherConfigFilterCondition() { List conditionList = new ArrayList<>(); - conditionList.add(new PrimitiveCondition(Constants.RESOURCE_TYPE, EQUALS, Constants.WEB_SUB_HUB_CONFIG_RESOURCE_TYPE_NAME)); - conditionList.add(new PrimitiveCondition(Constants.RESOURCE_NAME, EQUALS, Constants.WEB_SUB_HUB_CONFIG_RESOURCE_NAME)); + conditionList.add(new PrimitiveCondition(Constants.RESOURCE_TYPE, EQUALS, Constants.EVENT_PUBLISHER_CONFIG_RESOURCE_TYPE_NAME)); + conditionList.add(new PrimitiveCondition(Constants.RESOURCE_NAME, EQUALS, Constants.EVENT_PUBLISHER_CONFIG_RESOURCE_NAME)); return new ComplexCondition(ConditionType.ComplexOperator.AND, conditionList); } } diff --git a/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/constant/Constants.java b/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/constant/Constants.java index 173041c..a9d9595 100644 --- a/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/constant/Constants.java +++ b/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/constant/Constants.java @@ -25,12 +25,19 @@ public class Constants { public static final String EVENT_SCHEMA_TYPE_WSO2 = "WSO2"; public static final String LOGIN_EVENT_HOOK_NAME = "LoginEventHook"; + public static final String EVENT_SCHEMA_EVENTS_KEY = "events"; public static final String LOGIN_EVENT_HOOK_ENABLED = "LoginEventHook.enable"; public static final String ENABLE = "enable"; + public static final String EVENT_PUBLISHER_CONFIG_ATTRIBUTE_PUBLISH_ENABLED_KEY = "publishEnabled"; + + public static final String EVENT_CONFIG_SCHEMA_NAME_KEY = "eventSchema"; + public static final String EVENT_PUBLISHER_EVENT_SCHEMA_RESOURCE_FILE_PATH = + "repository/resources/identity/websubhub/event-schemas.json"; + public static final String EVENT_PUBLISHER_CONFIG_ATTRIBUTE_PROPERTIES_KEY = "properties"; public static final String SP_TO_CARBON_CLAIM_MAPPING = "SP_TO_CARBON_CLAIM_MAPPING"; - public static final String WEB_SUB_HUB_CONFIG_RESOURCE_TYPE_NAME = "web-sub-hub-event-publisher"; - public static final String WEB_SUB_HUB_CONFIG_RESOURCE_NAME = "web-sub-hub-event-publisher-configs"; + public static final String EVENT_PUBLISHER_CONFIG_RESOURCE_TYPE_NAME = "web-sub-hub-event-publisher"; + public static final String EVENT_PUBLISHER_CONFIG_RESOURCE_NAME = "web-sub-hub-event-publisher-configs"; public static final String RESOURCE_TYPE = "resourceTypeName"; public static final String RESOURCE_NAME = "resourceName"; diff --git a/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/internal/EventHookHandlerServiceComponent.java b/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/internal/EventHookHandlerServiceComponent.java index ab72bd8..3ec4977 100644 --- a/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/internal/EventHookHandlerServiceComponent.java +++ b/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/internal/EventHookHandlerServiceComponent.java @@ -18,9 +18,9 @@ package org.wso2.identity.webhook.common.event.handler.internal; +import org.wso2.carbon.identity.event.IdentityEventServerException; import org.wso2.identity.webhook.common.event.handler.LoginEventHookHandler; import org.wso2.identity.webhook.common.event.handler.builder.LoginEventPayloadBuilder; -import com.wso2.identity.asgardeo.event.configuration.mgt.core.service.EventConfigurationMgtService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.osgi.framework.BundleContext; @@ -61,7 +61,7 @@ protected void activate(ComponentContext context) { } else { log.error("Login Event Handler is not enabled."); } - } catch (Exception e) { + } catch (IdentityEventServerException e) { log.error("Error while activating event handler.", e); } } @@ -110,23 +110,6 @@ protected void unregisterConfigurationManager(ConfigurationManager configuration EventHookHandlerDataHolder.getInstance().setConfigurationManager(null); } - @Reference( - name = "event.configuration.manager.service", - service = EventConfigurationMgtService.class, - cardinality = ReferenceCardinality.MANDATORY, - policy = ReferencePolicy.DYNAMIC, - unbind = "unregisterEventConfigurationManager" - ) - protected void registerEventConfigurationManager(EventConfigurationMgtService eventConfigurationMgtService) { - /* Reference EventConfigurationMgtService to guarantee that this component will wait until - event configuration core is started */ - } - - protected void unregisterEventConfigurationManager(EventConfigurationMgtService eventConfigurationMgtService) { - /* Reference EventConfigurationMgtService to guarantee that this component will wait until - event configuration core is started */ - } - @Reference( name = "org.wso2.identity.event.common.publisher", service = EventPublisherService.class, diff --git a/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/model/EventAttribute.java b/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/model/EventAttribute.java new file mode 100644 index 0000000..8769793 --- /dev/null +++ b/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/model/EventAttribute.java @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.identity.webhook.common.event.handler.model; + +import org.json.simple.JSONObject; +import org.wso2.identity.webhook.common.event.handler.constant.Constants; + +/** + * Model class representing the event attributes. + */ +public class EventAttribute { + + private boolean publishEnabled; + private ResourceConfig properties; + + /** + * Constructs event attribute with default configs. + */ + public EventAttribute() { + + this.publishEnabled = false; + this.properties = new ResourceConfig(new JSONObject()); + } + + /** + * Construct event attribute. + * + * @param publishEnabled Is publishing enabled. + * @param properties Event properties. + */ + public EventAttribute(boolean publishEnabled, ResourceConfig properties) { + + this.publishEnabled = publishEnabled; + this.properties = properties; + } + + public boolean isPublishEnabled() { + + return publishEnabled; + } + + public void setPublishEnabled(boolean publishEnabled) { + + this.publishEnabled = publishEnabled; + } + + public ResourceConfig getProperties() { + + return properties; + } + + public void setProperties(ResourceConfig properties) { + + this.properties = properties; + } + + /** + * Get JSON string for the event attribute. + * + * @return JSON string. + */ + public String toJSONString() { + + JSONObject jsonObject = new JSONObject(); + jsonObject.put(Constants.EVENT_PUBLISHER_CONFIG_ATTRIBUTE_PUBLISH_ENABLED_KEY, publishEnabled); + jsonObject.put(Constants.EVENT_PUBLISHER_CONFIG_ATTRIBUTE_PROPERTIES_KEY, properties.getConfigs()); + + return jsonObject.toJSONString(); + } +} diff --git a/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/model/EventData.java b/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/model/EventData.java index a0c7953..f39499e 100644 --- a/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/model/EventData.java +++ b/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/model/EventData.java @@ -21,7 +21,6 @@ import org.wso2.carbon.identity.application.authentication.framework.AuthenticatorStatus; import org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext; import org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser; -import org.wso2.carbon.identity.application.common.model.User; import javax.servlet.http.HttpServletRequest; import java.util.Map; @@ -37,7 +36,6 @@ public class EventData { private final AuthenticationContext authenticationContext; private final AuthenticatorStatus authenticatorStatus; private final AuthenticatedUser authenticatedUser; - private final User loginIdentifier; private EventData(Builder builder) { this.eventName = builder.eventName; @@ -46,7 +44,6 @@ private EventData(Builder builder) { this.authenticationContext = builder.authenticationContext; this.authenticatorStatus = builder.authenticatorStatus; this.authenticatedUser = builder.authenticatedUser; - this.loginIdentifier = builder.loginIdentifier; } public String getEventName() { @@ -72,10 +69,6 @@ public AuthenticatedUser getAuthenticatedUser() { return authenticatedUser; } - public User getLoginIdentifier() { - return loginIdentifier; - } - public static Builder builder() { return new Builder(); } @@ -87,7 +80,6 @@ public static class Builder { private AuthenticationContext authenticationContext; private AuthenticatorStatus authenticatorStatus; private AuthenticatedUser authenticatedUser; - private User loginIdentifier; public Builder eventName(String eventName) { this.eventName = eventName; return this; @@ -118,11 +110,6 @@ public Builder authenticatedUser(AuthenticatedUser authenticatedUser) { return this; } - public Builder loginIdentifier(User loginIdentifier) { - this.loginIdentifier = loginIdentifier; - return this; - } - public EventData build() { return new EventData(this); } diff --git a/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/model/ResourceConfig.java b/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/model/ResourceConfig.java new file mode 100644 index 0000000..ee96bc8 --- /dev/null +++ b/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/model/ResourceConfig.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.identity.webhook.common.event.handler.model; + +import org.json.simple.JSONObject; + +/** + * A wrapper class for JSONObject to hold resource configuration properties. + */ +public class ResourceConfig { + + private JSONObject configs; + + /** + * Constructor for Resource config . + * + * @param configs JSON object. + */ + public ResourceConfig(JSONObject configs) { + + this.configs = configs; + } + + public JSONObject getConfigs() { + + return configs; + } + + public void setConfigs(JSONObject configs) { + + this.configs = configs; + } +} diff --git a/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/util/EventHookHandlerUtils.java b/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/util/EventHookHandlerUtils.java index dbf9bd2..f8e4bae 100644 --- a/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/util/EventHookHandlerUtils.java +++ b/components/org.wso2.identity.webhook.common.event.handler/src/main/java/org/wso2/identity/webhook/common/event/handler/util/EventHookHandlerUtils.java @@ -18,19 +18,18 @@ package org.wso2.identity.webhook.common.event.handler.util; -import com.wso2.identity.asgardeo.event.configuration.mgt.core.service.exception.EventConfigurationMgtServerException; -import com.wso2.identity.asgardeo.event.configuration.mgt.core.service.model.ResourceConfig; -import com.wso2.identity.asgardeo.event.configuration.mgt.core.service.util.EventConfigurationMgtUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; import org.slf4j.MDC; import org.wso2.carbon.identity.application.authentication.framework.AuthenticatorStatus; import org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext; import org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser; import org.wso2.carbon.identity.application.common.model.Claim; import org.wso2.carbon.identity.application.common.model.ClaimMapping; -import org.wso2.carbon.identity.application.common.model.User; import org.wso2.carbon.identity.core.ServiceURLBuilder; import org.wso2.carbon.identity.core.URLBuilderException; import org.wso2.carbon.identity.event.IdentityEventConfigBuilder; @@ -43,15 +42,21 @@ import org.wso2.identity.event.common.publisher.model.SecurityEventTokenPayload; import org.wso2.identity.webhook.common.event.handler.constant.Constants; import org.wso2.identity.webhook.common.event.handler.internal.EventHookHandlerDataHolder; +import org.wso2.identity.webhook.common.event.handler.model.EventAttribute; import org.wso2.identity.webhook.common.event.handler.model.EventData; +import org.wso2.identity.webhook.common.event.handler.model.ResourceConfig; import javax.servlet.http.HttpServletRequest; +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.HashMap; import java.util.Map; import java.util.UUID; -import static com.wso2.identity.asgardeo.event.configuration.mgt.core.service.util.EventConfigurationMgtConstants.EVENT_CONFIG_SCHEMA_NAME_KEY; -import static com.wso2.identity.asgardeo.event.configuration.mgt.core.service.util.EventConfigurationMgtConstants.ErrorMessages.ERROR_WHILE_CASTING_EVENT_CONFIG_AT_SERVER; import static org.wso2.carbon.identity.application.authentication.framework.util.FrameworkUtils.CORRELATION_ID_MDC; /** @@ -60,6 +65,8 @@ public class EventHookHandlerUtils { private static final Log log = LogFactory.getLog(EventHookHandlerUtils.class); + private static volatile ResourceConfig eventSchema = null; + private static final Object lock = new Object(); /** * Get the identity property specified in identity-event.properties @@ -68,7 +75,7 @@ public class EventHookHandlerUtils { * @param propertyName The name of the property which should be fetched * @return The required property */ - public static String getIdentityEventProperty(String moduleName, String propertyName) { + public static String getIdentityEventProperty(String moduleName, String propertyName) throws IdentityEventServerException { // Retrieving properties set in identity event properties String propertyValue = null; @@ -80,7 +87,8 @@ public static String getIdentityEventProperty(String moduleName, String property propertyValue = moduleConfiguration.getModuleProperties().getProperty(propertyName); } } catch (IdentityEventException e) { - log.error("An error occurred while retrieving module properties because " + e.getMessage(), e); + throw new IdentityEventServerException("An error occurred while retrieving module properties because " + + e.getMessage()); } return propertyValue; } @@ -95,52 +103,151 @@ public static String getIdentityEventProperty(String moduleName, String property public static String getEventUri(String eventKey) throws IdentityEventServerException { try { - ResourceConfig eventConfigObject = EventConfigurationMgtUtils.getEventConfig(eventKey); + ResourceConfig eventConfigObject = getEventConfig(eventKey); if (eventConfigObject.getConfigs() != null && - eventConfigObject.getConfigs().containsKey(EVENT_CONFIG_SCHEMA_NAME_KEY)) { - return (String) eventConfigObject.getConfigs().get(EVENT_CONFIG_SCHEMA_NAME_KEY); + eventConfigObject.getConfigs().containsKey(Constants.EVENT_CONFIG_SCHEMA_NAME_KEY)) { + return (String) eventConfigObject.getConfigs().get(Constants.EVENT_CONFIG_SCHEMA_NAME_KEY); } else { throw new IdentityEventServerException("Event schema not found in the resource event config " + "for the eventKey: " + eventKey); } - } catch (EventConfigurationMgtServerException e) { - throw new IdentityEventServerException(e.getErrorCode(), e.getMessage()); } catch (ClassCastException e) { - throw new IdentityEventServerException(ERROR_WHILE_CASTING_EVENT_CONFIG_AT_SERVER.getCode(), - ERROR_WHILE_CASTING_EVENT_CONFIG_AT_SERVER.getMessage()); + throw new IdentityEventServerException("Error while casting event config at server side", e); } } + /** + * Retrieve the event config. + * + * @param eventName Event name. + * @return Resource config object. + * @throws IdentityEventServerException If an error occurs. + */ + public static ResourceConfig getEventConfig(String eventName) throws IdentityEventServerException { + + JSONObject eventsConfigObject = (JSONObject) getEventsSchemaResourceFile().getConfigs() + .get(Constants.EVENT_SCHEMA_EVENTS_KEY); + if (eventsConfigObject != null && !eventsConfigObject.isEmpty() && + eventsConfigObject.containsKey(eventName)) { + return new ResourceConfig((JSONObject) eventsConfigObject.get(eventName)); + } else { + throw new IdentityEventServerException("Event schema not found in the resource event config " + + "for the eventKey: " + eventName); + } + } + + /** + * This method constructs the EventAttribute object from the json string. + * + * @param jsonString JSON string. + * @return EventAttribute object. + */ + public static EventAttribute buildEventAttributeFromJSONString(String jsonString) throws IdentityEventException { + + JSONObject eventJSON = getJSONObject(jsonString); + EventAttribute eventAttribute = new EventAttribute(); + try { + if (eventJSON.get(Constants.EVENT_PUBLISHER_CONFIG_ATTRIBUTE_PUBLISH_ENABLED_KEY) instanceof Boolean) { + eventAttribute.setPublishEnabled( + (Boolean) eventJSON.get(Constants.EVENT_PUBLISHER_CONFIG_ATTRIBUTE_PUBLISH_ENABLED_KEY)); + } else { + eventAttribute.setPublishEnabled(Boolean.parseBoolean( + (String) eventJSON.get(Constants.EVENT_PUBLISHER_CONFIG_ATTRIBUTE_PUBLISH_ENABLED_KEY))); + } + JSONObject propertiesJSON = + (JSONObject) eventJSON.get(Constants.EVENT_PUBLISHER_CONFIG_ATTRIBUTE_PROPERTIES_KEY); + eventAttribute.setProperties(new ResourceConfig(propertiesJSON)); + + return eventAttribute; + } catch (ClassCastException e) { + throw new IdentityEventException("Error while casting event attribute from JSON string", e); + } + } + + /** + * This method converts the parsed JSON String into a JSONObject. + * + * @param jsonString JSON string. + * @return JSON object. + * @throws IdentityEventServerException If an error occurs while constructing the object. + */ + private static JSONObject getJSONObject(String jsonString) throws IdentityEventServerException { + + JSONParser jsonParser = new JSONParser(); + try { + return (JSONObject) jsonParser.parse(jsonString); + } catch (ParseException | ClassCastException e) { + throw new IdentityEventServerException("Error while parsing JSON string", e); + } + } + + /** + * This method reads the event schema resource file and returns the config object. + * + * @return Config object with content in the resource file. + * @throws IdentityEventServerException If an error occurs while reading the resource file. + */ + private static ResourceConfig getEventsSchemaResourceFile() throws IdentityEventServerException { + + if (eventSchema == null) { + synchronized (lock) { + if (eventSchema == null) { + String resourceFilePath = new File(".").getAbsolutePath() + File.separator + + Constants.EVENT_PUBLISHER_EVENT_SCHEMA_RESOURCE_FILE_PATH; + JSONParser jsonParser = new JSONParser(); + try { + JSONObject eventConfigJSON = (JSONObject) jsonParser.parse(new InputStreamReader( + Files.newInputStream(Paths.get(resourceFilePath)), StandardCharsets.UTF_8) + ); + eventSchema = new ResourceConfig(eventConfigJSON); + } catch (IOException | ParseException | ClassCastException e) { + throw new IdentityEventServerException("Error while reading the event schema file", e); + } + } + } + } + + return eventSchema; + } + /** * Build the event data provider. * * @param event Event object. * @return Event data object. */ - public static EventData buildEventDataProvider(Event event) { + public static EventData buildEventDataProvider(Event event) throws IdentityEventException { Map properties = event.getEventProperties(); - Map params = (Map)properties.get("params"); - AuthenticationContext context = (AuthenticationContext)properties.get("context"); + Map params; + AuthenticationContext context; + AuthenticatorStatus status; + HttpServletRequest request; + + if (properties == null || + (params = (Map) properties.get("params")) == null || + (context = (AuthenticationContext) properties.get("context")) == null || + (status = (AuthenticatorStatus) properties.get("authenticationStatus")) == null || + (request = (HttpServletRequest) params.get("request")) == null) { + // Handle the case where any of the required properties are null + throw new IdentityEventException("One or more required properties are null"); + } + Object user = params.get("user"); AuthenticatedUser authenticatedUser = new AuthenticatedUser(); - // User information in login failed events. - User loginIdentifier = new User(); + if (user instanceof AuthenticatedUser){ authenticatedUser = (AuthenticatedUser)user; setLocalUserClaimsToAuthenticatedUser(authenticatedUser, context); - } else { - loginIdentifier = (User)user; } return EventData.builder() .eventName(event.getEventName()) - .request((HttpServletRequest)properties.get("request")) + .request(request) .eventParams(params) - .authenticationContext((AuthenticationContext)properties.get("context")) - .authenticatorStatus((AuthenticatorStatus)properties.get("authenticationStatus")) + .authenticationContext(context) + .authenticatorStatus(status) .authenticatedUser(authenticatedUser) - .loginIdentifier(loginIdentifier) .build(); } @@ -162,7 +269,7 @@ public static SecurityEventTokenPayload buildSecurityEventToken(EventPayload eve } SecurityEventTokenPayload securityEventTokenPayload = new SecurityEventTokenPayload(); - securityEventTokenPayload.setIss(getReference("", "")); + securityEventTokenPayload.setIss(getURL()); securityEventTokenPayload.setIat(System.currentTimeMillis()); securityEventTokenPayload.setJti(UUID.randomUUID().toString()); securityEventTokenPayload.setRci(getCorrelationID()); @@ -217,26 +324,33 @@ private static void setLocalUserClaimsToAuthenticatedUser(AuthenticatedUser auth } /** - * Retrieve the reference for the given endpoint and id. + * Get the tenant qualified URL. * - * @param endpoint Endpoint. - * @param id Resource id. - * @return Resource location. + * @return Tenant qualified URL. */ - public static String getReference(String endpoint, String id) { + public static String getURL() { - return getURL(endpoint) + "/" + id; + return getURL(null); } - private static String getURL(String endpoint) { + /** + * Get the tenant qualified URL with path. + * + * @param endpoint Endpoint. + * @return Tenant qualified URL. + */ + public static String getURL(String endpoint) { try { - return ServiceURLBuilder.create().addPath(endpoint).build().getAbsolutePublicURL(); + ServiceURLBuilder builder = ServiceURLBuilder.create(); + if (endpoint != null && !endpoint.isEmpty()) { + builder.addPath(endpoint); + } + return builder.build().getAbsolutePublicURL(); } catch (URLBuilderException e) { - log.debug("Error occurred while building the endpoint URL with tenant " + - "qualified URL.", e); + log.debug("Error occurred while building the endpoint URL with tenant qualified URL.", e); } - return endpoint; + return endpoint != null ? endpoint : ""; } /** diff --git a/components/org.wso2.identity.webhook.wso2.event.handler/pom.xml b/components/org.wso2.identity.webhook.wso2.event.handler/pom.xml index ce0203d..37572a6 100644 --- a/components/org.wso2.identity.webhook.wso2.event.handler/pom.xml +++ b/components/org.wso2.identity.webhook.wso2.event.handler/pom.xml @@ -89,15 +89,6 @@ org.wso2.orbit.org.apache.httpcomponents httpasyncclient - - com.wso2.identity.asgardeo.extensions - com.wso2.identity.asgardeo.event.configuration.mgt.core.service - - - com.wso2.identity.asgardeo.extensions - com.wso2.identity.asgardeo.config.mapper - - com.googlecode.json-simple.wso2 json-simple @@ -173,7 +164,6 @@ org.wso2.carbon.identity.data.publisher.authentication.analytics.login.*; version="${identity.datapublisher.authentication.version.range}", org.json.simple; version="${com.googlecode.json-simple.wso2.version.range}", org.json.simple.parser; version="${com.googlecode.json-simple.wso2.version.range}", - com.wso2.identity.asgardeo.event.configuration.mgt.core.service.*; version="${asgardeo.event.configuration.mgt.core.version.range}", org.wso2.identity.webhook.common.event.handler.*; version="${org.wso2.identity.webhook.common.event.handler.version.range}", org.wso2.identity.event.common.publisher; diff --git a/components/org.wso2.identity.webhook.wso2.event.handler/src/main/java/org/wso2/identity/webhook/wso2/event/handler/builder/WSO2LoginEventPayloadBuilder.java b/components/org.wso2.identity.webhook.wso2.event.handler/src/main/java/org/wso2/identity/webhook/wso2/event/handler/builder/WSO2LoginEventPayloadBuilder.java index da8e3db..02929d7 100644 --- a/components/org.wso2.identity.webhook.wso2.event.handler/src/main/java/org/wso2/identity/webhook/wso2/event/handler/builder/WSO2LoginEventPayloadBuilder.java +++ b/components/org.wso2.identity.webhook.wso2.event.handler/src/main/java/org/wso2/identity/webhook/wso2/event/handler/builder/WSO2LoginEventPayloadBuilder.java @@ -24,6 +24,8 @@ import org.wso2.identity.webhook.wso2.event.handler.model.WSO2AuthenticationSuccessEventPayload; import org.wso2.identity.event.common.publisher.model.EventPayload; +import java.util.ArrayList; + import static org.wso2.identity.webhook.common.event.handler.constant.Constants.EVENT_SCHEMA_TYPE_WSO2; /** @@ -35,14 +37,29 @@ public class WSO2LoginEventPayloadBuilder implements LoginEventPayloadBuilder { public EventPayload buildAuthenticationSuccessEvent(EventData eventData) throws IdentityEventException { //TODO: Implement the logic to build the authentication success event payload. - return new WSO2AuthenticationSuccessEventPayload(); + return new WSO2AuthenticationSuccessEventPayload.Builder() + .user(null) + .tenant(null) + .userResidentOrganization(null) + .userStore(null) + .application(null) + .authenticationMethods(new ArrayList<>()) + .build(); } @Override public EventPayload buildAuthenticationFailedEvent(EventData eventData) throws IdentityEventException { //TODO: Implement the logic to build the authentication failed event payload. - return new WSO2AuthenticationFailedEventPayload(); + return new WSO2AuthenticationFailedEventPayload.Builder() + .user(null) + .tenant(null) + .userResidentOrganization(null) + .userStore(null) + .application(null) + .reason(null) + .userLoginIdentifier(null) + .build(); } @Override diff --git a/components/org.wso2.identity.webhook.wso2.event.handler/src/main/java/org/wso2/identity/webhook/wso2/event/handler/model/WSO2AuthenticationFailedEventPayload.java b/components/org.wso2.identity.webhook.wso2.event.handler/src/main/java/org/wso2/identity/webhook/wso2/event/handler/model/WSO2AuthenticationFailedEventPayload.java index 701c2ad..5eedd19 100644 --- a/components/org.wso2.identity.webhook.wso2.event.handler/src/main/java/org/wso2/identity/webhook/wso2/event/handler/model/WSO2AuthenticationFailedEventPayload.java +++ b/components/org.wso2.identity.webhook.wso2.event.handler/src/main/java/org/wso2/identity/webhook/wso2/event/handler/model/WSO2AuthenticationFailedEventPayload.java @@ -18,27 +18,83 @@ package org.wso2.identity.webhook.wso2.event.handler.model; +import org.wso2.identity.webhook.wso2.event.handler.model.common.Application; +import org.wso2.identity.webhook.wso2.event.handler.model.common.Organization; +import org.wso2.identity.webhook.wso2.event.handler.model.common.User; +import org.wso2.identity.webhook.wso2.event.handler.model.common.UserStore; + /** * Model Class for Authentication Failed Event Payload. */ public class WSO2AuthenticationFailedEventPayload extends WSO2BaseEventPayload { - private AuthenticationFailedReason reason; - private String userLoginIdentifier; + private final AuthenticationFailedReason reason; + private final String userLoginIdentifier; - public AuthenticationFailedReason getReason() { - return reason; + private WSO2AuthenticationFailedEventPayload(Builder builder) { + this.user = builder.user; + this.tenant = builder.tenant; + this.userResidentOrganization = builder.userResidentOrganization; + this.userStore = builder.userStore; + this.application = builder.application; + this.reason = builder.reason; + this.userLoginIdentifier = builder.userLoginIdentifier; } - public void setReason(AuthenticationFailedReason reason) { - this.reason = reason; + public AuthenticationFailedReason getReason() { + return reason; } public String getUserLoginIdentifier() { return userLoginIdentifier; } - public void setUserLoginIdentifier(String userLoginIdentifier) { - this.userLoginIdentifier = userLoginIdentifier; + public static class Builder { + private User user; + private Organization tenant; + private Organization userResidentOrganization; + private UserStore userStore; + private Application application; + private AuthenticationFailedReason reason; + private String userLoginIdentifier; + + public Builder user(User user) { + this.user = user; + return this; + } + + public Builder tenant(Organization tenant) { + this.tenant = tenant; + return this; + } + + public Builder userResidentOrganization(Organization userResidentOrganization) { + this.userResidentOrganization = userResidentOrganization; + return this; + } + + public Builder userStore(UserStore userStore) { + this.userStore = userStore; + return this; + } + + public Builder application(Application application) { + this.application = application; + return this; + } + + public Builder reason(AuthenticationFailedReason reason) { + this.reason = reason; + return this; + } + + public Builder userLoginIdentifier(String userLoginIdentifier) { + this.userLoginIdentifier = userLoginIdentifier; + return this; + } + + public WSO2AuthenticationFailedEventPayload build() { + return new WSO2AuthenticationFailedEventPayload(this); + } } } diff --git a/components/org.wso2.identity.webhook.wso2.event.handler/src/main/java/org/wso2/identity/webhook/wso2/event/handler/model/WSO2AuthenticationSuccessEventPayload.java b/components/org.wso2.identity.webhook.wso2.event.handler/src/main/java/org/wso2/identity/webhook/wso2/event/handler/model/WSO2AuthenticationSuccessEventPayload.java index 14eafb3..1006a19 100644 --- a/components/org.wso2.identity.webhook.wso2.event.handler/src/main/java/org/wso2/identity/webhook/wso2/event/handler/model/WSO2AuthenticationSuccessEventPayload.java +++ b/components/org.wso2.identity.webhook.wso2.event.handler/src/main/java/org/wso2/identity/webhook/wso2/event/handler/model/WSO2AuthenticationSuccessEventPayload.java @@ -18,6 +18,11 @@ package org.wso2.identity.webhook.wso2.event.handler.model; +import org.wso2.identity.webhook.wso2.event.handler.model.common.Application; +import org.wso2.identity.webhook.wso2.event.handler.model.common.Organization; +import org.wso2.identity.webhook.wso2.event.handler.model.common.User; +import org.wso2.identity.webhook.wso2.event.handler.model.common.UserStore; + import java.util.ArrayList; import java.util.List; @@ -28,15 +33,64 @@ public class WSO2AuthenticationSuccessEventPayload extends WSO2BaseEventPayload private List authenticationMethods = new ArrayList<>(); + private WSO2AuthenticationSuccessEventPayload(Builder builder) { + this.user = builder.user; + this.tenant = builder.tenant; + this.userResidentOrganization = builder.userResidentOrganization; + this.userStore = builder.userStore; + this.application = builder.application; + this.authenticationMethods = builder.authenticationMethods; + } + public List getAuthenticationMethods() { return authenticationMethods; } - public void setAuthenticationMethods(List authenticationMethods) { - this.authenticationMethods = authenticationMethods; - } + public static class Builder { + private User user; + private Organization tenant; + private Organization userResidentOrganization; + private UserStore userStore; + private Application application; + private List authenticationMethods = new ArrayList<>(); + + public Builder user(User user) { + this.user = user; + return this; + } + + public Builder tenant(Organization tenant) { + this.tenant = tenant; + return this; + } + + public Builder userResidentOrganization(Organization userResidentOrganization) { + this.userResidentOrganization = userResidentOrganization; + return this; + } + + public Builder userStore(UserStore userStore) { + this.userStore = userStore; + return this; + } + + public Builder application(Application application) { + this.application = application; + return this; + } + + public Builder authenticationMethods(List authenticationMethods) { + this.authenticationMethods = authenticationMethods; + return this; + } + + public Builder addAuthenticationMethod(String authenticationMethod) { + this.authenticationMethods.add(authenticationMethod); + return this; + } - public void addAuthenticationMethod(String authenticationMethod) { - this.authenticationMethods.add(authenticationMethod); + public WSO2AuthenticationSuccessEventPayload build() { + return new WSO2AuthenticationSuccessEventPayload(this); + } } } diff --git a/components/org.wso2.identity.webhook.wso2.event.handler/src/main/java/org/wso2/identity/webhook/wso2/event/handler/model/WSO2BaseEventPayload.java b/components/org.wso2.identity.webhook.wso2.event.handler/src/main/java/org/wso2/identity/webhook/wso2/event/handler/model/WSO2BaseEventPayload.java index 334777e..5c52008 100644 --- a/components/org.wso2.identity.webhook.wso2.event.handler/src/main/java/org/wso2/identity/webhook/wso2/event/handler/model/WSO2BaseEventPayload.java +++ b/components/org.wso2.identity.webhook.wso2.event.handler/src/main/java/org/wso2/identity/webhook/wso2/event/handler/model/WSO2BaseEventPayload.java @@ -24,50 +24,30 @@ import org.wso2.identity.webhook.wso2.event.handler.model.common.UserStore; import org.wso2.identity.event.common.publisher.model.EventPayload; -public class WSO2BaseEventPayload extends EventPayload { - private User user; - private Organization tenant; - private Organization userResidentOrganization; - private UserStore userStore; - private Application application; +public abstract class WSO2BaseEventPayload extends EventPayload { + protected User user; + protected Organization tenant; + protected Organization userResidentOrganization; + protected UserStore userStore; + protected Application application; public User getUser() { return user; } - public void setUser(User user) { - this.user = user; - } - public Organization getTenant() { return tenant; } - public void setTenant(Organization tenant) { - this.tenant = tenant; - } - public UserStore getUserStore() { return userStore; } - public void setUserStore(UserStore userStore) { - this.userStore = userStore; - } - public Application getApplication() { return application; } - public void setApplication(Application application) { - this.application = application; - } - public Organization getUserResidentOrganization() { return userResidentOrganization; } - - public void setUserResidentOrganization(Organization userResidentOrganization) { - this.userResidentOrganization = userResidentOrganization; - } } diff --git a/pom.xml b/pom.xml index 3df1842..5f31645 100644 --- a/pom.xml +++ b/pom.xml @@ -152,17 +152,6 @@ servlet-api ${servlet-api.version} - - com.wso2.identity.asgardeo.extensions - com.wso2.identity.asgardeo.event.configuration.mgt.core.service - ${com.wso2.identity.asgardeo.extensions.version} - - - com.wso2.identity.asgardeo.extensions - com.wso2.identity.asgardeo.config.mapper - ${com.wso2.identity.asgardeo.extensions.version} - - com.googlecode.json-simple.wso2 json-simple @@ -370,8 +359,6 @@ 1.8.60 [1.3.0, 3.0.0) - [1.0.13, 1.2.0) - [1.0.0, 2.0.0) @@ -433,14 +420,9 @@ 1.1.5 - - [1.0.0, 2.0.0) - - 3.4.72 - 1.0.219 7.0.114