Skip to content

Commit

Permalink
addressed review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Sachin-Mamoru committed Jul 19, 2024
1 parent 4ab4c29 commit 6e511b9
Show file tree
Hide file tree
Showing 14 changed files with 455 additions and 177 deletions.
10 changes: 0 additions & 10 deletions components/org.wso2.identity.webhook.common.event.handler/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,6 @@
<groupId>org.wso2.orbit.org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
</dependency>
<dependency>
<groupId>com.wso2.identity.asgardeo.extensions</groupId>
<artifactId>com.wso2.identity.asgardeo.event.configuration.mgt.core.service</artifactId>
</dependency>
<dependency>
<groupId>com.wso2.identity.asgardeo.extensions</groupId>
<artifactId>com.wso2.identity.asgardeo.config.mapper</artifactId>
</dependency>

<dependency>
<groupId>com.googlecode.json-simple.wso2</groupId>
<artifactId>json-simple</artifactId>
Expand Down Expand Up @@ -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}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)) {
Expand All @@ -146,23 +130,22 @@ 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 &&
CollectionUtils.isNotEmpty(publisherConfigResource.getResources().get(0).getAttributes())) {

for (Attribute attribute : publisherConfigResource.getResources().get(0).getAttributes()) {
if (isMatchingEventAttribute(attribute, eventName)) {
return EventConfigurationMgtUtils.buildEventAttributeFromJSONString(attribute.getValue());
return EventHookHandlerUtils.buildEventAttributeFromJSONString(attribute.getValue());
}
}
}
Expand All @@ -180,8 +163,8 @@ private boolean isMatchingEventAttribute(Attribute attribute, String eventName)
private ComplexCondition createPublisherConfigFilterCondition() {

List<Condition> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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() {
Expand All @@ -72,10 +69,6 @@ public AuthenticatedUser getAuthenticatedUser() {
return authenticatedUser;
}

public User getLoginIdentifier() {
return loginIdentifier;
}

public static Builder builder() {
return new Builder();
}
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading

0 comments on commit 6e511b9

Please sign in to comment.