Keycloak custom invoke Admin Events using AdminEventBuilder

Hello!

We have extended the LDAPStorageMapper and specifically the onImportUserFromLDAP() method so that whenever a user is imported in Keycloak a keycloak event is fired. When such events happen, we call CRUD operations with REST apis to maintain users in an external database. This works ok when the events that are invoked are User Events like the one shown below:

EventBuilder eventBuilder=new EventBuilder(realm,_session) .event(type) .realm(realm).client(_session.getContext().getClient());
							  
eventBuilder.user(user) .detail(Details.USERNAME, user.getUsername()).success();

However we dont want to create User Events but rather Admin Events since these are the same type of events when an admin updates users in the admin console UI.

However with Admin events configured as shown below, we get the following error:

AdminAuth auth=new AdminAuth(realm, new AccessToken(), ldapAdmin,_session.getContext().getClient());
AdminEventBuilder adminEventBuilder = new AdminEventBuilder(realm,auth,_session,_session.getContext().getConnection());

An error occurred: jakarta.enterprise.context.ContextNotActiveException: RequestScoped context was not active when trying to obtain a bean instance for a client proxy of CLASS bean [class=io.quarkus.vertx.http.runtime.CurrentVertxRequest, id=0_6n6EmChCiiDdd8HelptG_A0AE]

  • you can activate the request context for a specific method using the @ActivateRequestContext interceptor binding
    at io.quarkus.arc.impl.ClientProxies.notActive(ClientProxies.java:70)
    at io.quarkus.arc.impl.ClientProxies.getSingleContextDelegate(ClientProxies.java:30)
    at io.quarkus.vertx.http.runtime.CurrentVertxRequest_ClientProxy.arc$delegate(Unknown Source)
    at io.quarkus.vertx.http.runtime.CurrentVertxRequest_ClientProxy.getOtherHttpContextObject(Unknown Source)
    at io.quarkus.resteasy.reactive.server.runtime.QuarkusCurrentRequest.get(QuarkusCurrentRequest.java:21)
    at org.jboss.resteasy.reactive.server.core.CurrentRequestManager.get(CurrentRequestManager.java:8)
    at org.keycloak.quarkus.runtime.integration.resteasy.QuarkusKeycloakContext.getResteasyReactiveRequestContext(QuarkusKeycloakContext.java:53)
    at org.keycloak.quarkus.runtime.integration.resteasy.QuarkusKeycloakContext.createClientConnection(QuarkusKeycloakContext.java:47)
    at org.keycloak.services.DefaultKeycloakContext.getConnection(DefaultKeycloakContext.java:152)
    at org.keycloak.Custom.ldap.CustomLDAPMapper.onImportUserFromLDAP(CustomLDAPMapper.java:145)

This is related to the _session.getContext().getConnection() which is not activated.
Could you please let me know if you have any ideas on how to create an activated client connection for the AdminEvent or better how to successfully invoke an Admin Event?

Many thanks,