Callback (hook) on User creation

I wonder if there is something like a hook or a callback to activate, either programmatically or via admin console, to trigger some action (call a remote service, for ex.) when a new User is created via admin console?

Hi,

take a look at the spi-event-listener example in https://github.com/zonaut/keycloak-extensions and see if this helps you further.

1 Like

O, cool, good to know! Thank you for sharing that. :+1:

@zonaut, it is not clear enought how to proceed after copying the spi-event-listener-0.0.1-SNAPSHOT.jar into ~/keycloak-11.0.2/standalone/deployments foldder (I’m using a standalone instance of Keycloak). Should I create a custom class implementing EventListenerProviderFactory as it is done in this code-source example or there is another way to go? Thank you.

After deployment you need to configure it.
Take a loot at the readme file on how to set it up https://github.com/zonaut/keycloak-extensions/blob/master/spi-event-listener/README.md -> Keycloak admin console configuration
If it isn’t detected try to restart Keycloak so it’s picked up.

I’ve already done all that is described in README of the extension. The question is what is next after

select our pl_event_listener

Imagine that I’d like to trigger an action once a new user is created via Admin Console. How to branch the Kycloak listener to an external service (no matter, Kafka, a Java micro-service, etc.)?

Have you taken a look at the UserVerifiedTransaction example which is executed on the event(s) you listen on and calls an external service through an API call.
The call session.getTransactionManager().enlistPrepare(userVerifiedTransaction); executes this call, you probably will need to do this on the AdminEvent listener

You’ll probably be interested in

  • onEvent(AdminEvent adminEvent, boolean b) -> ResourceType.USER + OperationType.CREATE
1 Like

As far as I understood, I’ll have to override public void onEvent(AdminEvent adminEvent, boolean b) method to be able to listen to admin events. An instance of AdminEvent has some methods, especially the getOperationType() could be interesting which returns an OperationType enum or the getResourceType() method that returns an Enum of ResourceType, where the one of USER could be interested. Right?

Yep, you were faster this time :smiley: So, I have to:

  • create a separate micro-service
  • package it as a jar
  • deploy the jar into the ~/keycloak-11.0.2/standalone/deployments folder.

Right?

Yes, that sounds about right.
Good luck and let us know how it went.

How do we get the user details for the DELETE callback. I mean when a user gets deleted I want the user details as well, currently they are null?