Writing a Custom event handler using the WSO2 Identity Server Eventing Framework

Gangani Chamika
3 min readApr 27, 2020

Let me share my experience in writing a custom event handler using the WSO2 Identity Server Eventing Framework. Recently I wrote an event handler to trigger a specific operation based on a published event. I’ll explain in a more generic way to implement an event handler where you can use this knowledge to write any event handler using the WSO2 Identity Server Eventing Framework.

The WSO2 Identity Server Eventing Framework blog by Isura Karunarathne was really helpful for me to implement the custom event handler for the WSO2 Identity Server. You can read that blog as well. Here I’ll add more details to that by mainly focusing on the event handler implementation.

Let’s start implementing a sample event handler

public static final String PRE_SET_EVENT = "PRE_SET_EVENT";
public static final String POST_SET_EVENT = "POST_SET_EVENT";
  • You have to extend the AbstractEventHandler class [org.wso2.carbon.identity.event.handler.AbstractEventHandler] to create the custom event handler.
  • Inside the custom event handler override getName() method to set the name for the event handler and getPriority() to set the priory of the event handler.
public String getName() {
return "customEvent";
}
@Override
public int getPriority(MessageContext messageContext) {
return 50;
}
  • To execute the expected operation, override the handleEvent() method. The event.getEventProperties() method can be used to get the parameters related to the user operations.
@Override
public void handleEvent(Event event) throws IdentityEventException {
Map<String, Object> eventProperties =event.getEventProperties();
String userName = (String) eventProperties.get(IdentityEventConstants.EventProperty.USER_NAME);
UserStoreManager userStoreManager = (UserStoreManager) eventProperties.get(IdentityEventConstants.EventProperty.USER_STORE_MANAGER);
String tenantDomain = (String) eventProperties.get(IdentityEventConstants.EventProperty.TENANT_DOMAIN);
String domainName = userStoreManager.getRealmConfiguration().getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_DOMAIN_NAME);
String eventName = event.getEventName();
if (IdentityEventConstants.Event.PRE_SET_EVENT.equals(eventName)) {//Do the operation on PRE_SET_EVENT.}
if(IdentityEventConstants.Event.POST_SET_EVENT.equals(eventName)) {//Do the operation on POST_SET_EVENT.}
}

How to use the event handler to do an operation

  • You can write a method to trigger the event. There, you can publish an event along with the event properties such as USER_NAME, TENANT_DOMAIN, USER_STORE_DOMAIN, and call the handleEvent method as shown in the following code.
  • As shown in the above code, the handleEvent method in the handler class, will check for the published event and do the operation accordingly.
private void triggerSampleEvent (User user, ... ,String eventName)  
throws IdentityRecoveryException {

HashMap<String, Object> properties = new HashMap<>();

properties.put(IdentityEventConstants.EventProperty.USER_NAME, user.getUserName());
properties.put(IdentityEventConstants.EventProperty.
TENANT_DOMAIN, user.getTenantDomain());
properties.put(IdentityEventConstants.EventProperty.
USER_STORE_DOMAIN, user.getUserStoreDomain());

Event sampleEvent = new Event(eventName, properties);
try {
IdentityRecoveryServiceDataHolder.getInstance(). getIdentityEventService().handleEvent(sampleEvent);
} catch (IdentityEventException e) {
throw Utils.handleServerException(ERROR_CODE_TRIGGER_EVENT, e);
}

Register Event Handler

Then you can register the event handler in the service component as follows.

protected void activate(ComponentContext context) {try {
BundleContext bundleContext = context.getBundleContext();
bundleContext.registerService(AbstractEventHandler.
class.getName(),new CustomEventHandler(), null);
} catch (Exception e) {
...
}
  • Now you can publish an event, where you want to trigger the

Let’s configure the event handler

  • The custom event configuration can be added as follows to {wso2is-home}/repository/conf/deployment.toml file. The events which need to subscribe to the handler can be listed in subscriptions.
[[event_handler]]
name= "customEvent"
subscriptions =["PRE_SET_EVENT","POST_SET_EVENT"]

if you don’t use the deployment.toml for configuration, add the following configs to {wso2is-home}/repository/conf/identity/identity-event.properties file.

module.name.22=customEvent
customEvent.subscription.1=PRE_SET_EVENT
customEvent.subscription.2=POST_SET_EVENT
  • When you want to execute an operation related to an event, publish the event. Then the handler that is subscribed for the particular events will be executed those events.
  • According to the above configuration, customEvent handler is subscribed for PRE_SET_EVENT and POST_SET_EVENT.

Congratulations!!! You have successfully implemented a sample event handler using the WSO2 Identity Server Eventing Framework. Hope this was useful.

--

--