Skip to content

Commit

Permalink
added login event hook enable check for service register
Browse files Browse the repository at this point in the history
  • Loading branch information
Sachin-Mamoru committed Jul 16, 2024
1 parent 4f1b511 commit a757711
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ 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 LOGIN_EVENT_HOOK_ENABLED = "LoginEventHook.enable";
public static final String ENABLE = "enable";
public static final String SP_TO_CARBON_CLAIM_MAPPING = "SP_TO_CARBON_CLAIM_MAPPING";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import org.wso2.carbon.identity.configuration.mgt.core.ConfigurationManager;
import org.wso2.carbon.identity.event.handler.AbstractEventHandler;
import org.wso2.identity.event.common.publisher.EventPublisherService;
import org.wso2.identity.webhook.common.event.handler.constant.Constants;
import org.wso2.identity.webhook.common.event.handler.util.EventHookHandlerUtils;

/**
* WSO2 Event Handler service component class.
Expand All @@ -50,11 +52,14 @@ protected void activate(ComponentContext context) {

try {
log.debug("Event Handler is activated.");
String isLoginEventHandlerEnabled = EventHookHandlerUtils
.getIdentityEventProperty(Constants.LOGIN_EVENT_HOOK_NAME, Constants.LOGIN_EVENT_HOOK_ENABLED);
BundleContext bundleContext = context.getBundleContext();
LoginEventHookHandler loginEventHookHandler = new LoginEventHookHandler();
if (loginEventHookHandler.isLoginEventHandlerEnabled()) {
if (isLoginEventHandlerEnabled.equalsIgnoreCase(Boolean.TRUE.toString())) {
bundleContext.registerService(AbstractEventHandler.class.getName(),
loginEventHookHandler, null);
new LoginEventHookHandler(), null);
} else {
log.error("Login Event Handler is not enabled.");
}
} catch (Exception e) {
log.error("Error while activating event handler.", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
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;
import org.wso2.carbon.identity.event.IdentityEventException;
import org.wso2.carbon.identity.event.IdentityEventServerException;
import org.wso2.carbon.identity.event.bean.ModuleConfiguration;
import org.wso2.carbon.identity.event.event.Event;
import org.wso2.identity.event.common.publisher.model.EventContext;
import org.wso2.identity.event.common.publisher.model.EventPayload;
Expand All @@ -59,6 +61,30 @@ public class EventHookHandlerUtils {

private static final Log log = LogFactory.getLog(EventHookHandlerUtils.class);

/**
* Get the identity property specified in identity-event.properties
*
* @param moduleName The name of the module which the property belongs to
* @param propertyName The name of the property which should be fetched
* @return The required property
*/
public static String getIdentityEventProperty(String moduleName, String propertyName) {

// Retrieving properties set in identity event properties
String propertyValue = null;
try {
ModuleConfiguration moduleConfiguration = IdentityEventConfigBuilder.getInstance()
.getModuleConfigurations(moduleName);

if (moduleConfiguration != null) {
propertyValue = moduleConfiguration.getModuleProperties().getProperty(propertyName);
}
} catch (IdentityEventException e) {
log.error("An error occurred while retrieving module properties because " + e.getMessage(), e);
}
return propertyValue;
}

/**
* Retrieve event uri.
*
Expand Down Expand Up @@ -124,8 +150,7 @@ public static EventData buildEventDataProvider(Event event) {
* @param eventUri Event URI.
* @return Audience string.
*/
public static SecurityEventTokenPayload buildSecurityEventToken(EventPayload eventPayload,
AuthenticationContext context, String eventUri)
public static SecurityEventTokenPayload buildSecurityEventToken(EventPayload eventPayload, String eventUri)
throws IdentityEventException {

if (eventPayload == null) {
Expand Down

0 comments on commit a757711

Please sign in to comment.