Repository navigation
Replies: 5 comments 5 replies
|
Every bigger Keycloak project i have seen has sooner or later started to introduce some kind of custom event system which means forking |
|
This is something that has come up in our numerous SPIs, custom authenticators, flows, etc frequently. I also second this proposal. Something I don't see in this proposal, but would be interesting on our end: Keycloak supports turning events into metrics. Currently, the supported tags are |
|
Interesting idea about metric tags @sonOfRa 👍 Would like to hear Grafana users' opinion on whether we should automatically namespace vendor type with vendor ID. Without namespacing: With namespacing: Without namespacing, the event payloads will be lighter, but there could be clashes if several vendors use the same type. Could that be an issue for Prometheus / Grafana? |
|
Thanks for the proposal, custom events are something I see requested quite often as well. What I don't like about a single I wonder if a better alternative would be to turn ...
public interface EventType {
// deprecation notice, suggestion forRemoval=true, recommend direct usage of BuiltinEventType.LOGIN instead.
@Deprecated
EventType LOGIN = BuiltinEventType.LOGIN;
@Deprecated
EventType LOGIN_ERROR = BuiltinEventType.LOGIN_ERROR;
@Deprecated
EventType REGISTER = BuiltinEventType.REGISTER;
@Deprecated
EventType REGISTER_ERROR = BuiltinEventType.REGISTER_ERROR;
...
String name();
boolean isSaveByDefault();
...
}The existing enum values would move to something like ...
public enum BuiltinEventType implements EventType, EnumWithStableIndex {
LOGIN(0, true),
LOGIN_ERROR(0x10000 + LOGIN.getStableIndex(), true),
...
}Extensions could then define their own event types with a plain enum and provide them via a small SPI: package com.acme.keycloak.events;
public enum AcmeEventType implements EventType {
ACME_GET_RISK_SCORE(true),
MY_EVENT(true),
MY_EVENT_ERROR(true);
private final boolean saveByDefault;
AcmeEventType(boolean saveByDefault) {
this.saveByDefault = saveByDefault;
}
@Override
public boolean isSaveByDefault() {
return saveByDefault;
}
public static class Factory implements EventTypeProviderFactory {
@Override
public String getId() {
return "acme-events";
}
@Override
public Set<? extends EventType> getEventTypes() {
return EnumSet.allOf(AcmeEventType.class);
}
}
}with public interface EventTypeProviderFactory extends ProviderFactory<EventTypeProvider>, EventTypeProvider {
@Override
default EventTypeProvider create(KeycloakSession session) {
return this;
}
...META-INF/services/org.keycloak.events.EventTypeProviderFactory: event.event(AcmeEventType.ACME_GET_RISK_SCORE).detail("risk_score", "0.15").success();
// in an event listener
if (event.getType() == AcmeEventType.ACME_GET_RISK_SCORE) { ... }The custom types are provided through a regular SPI ( Btw. for custom events it might be good to define a convention of prefixing the custom events with something, e.g. I just tried this out in a small PoC (builds and starts clean locally): https://github.com/thomasdarimont/keycloak/tree/poc/custom-events
...
@Override
public void authenticate(AuthenticationFlowContext context) {
// clone, so that the login event of the flow stays untouched
context.getEvent().clone()
.event(MyEventType.MY_EVENT)
.detail("my_detail", "fired by " + PROVIDER_ID)
.success();
context.success();
}
...The downside is that this is a breaking change for extensions: existing event listeners (might) need to be recompiled, and a Open points:
WDYT? |
|
Related Custom EventType
|
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Abstract
This document proposes a solution to the long-standing problem of custom event types in Keycloak.
Problem
In Keycloak, event types are modeled as an enum, org.keycloak.events.EventType. As Java enums are not extensible, the only way to support custom event types is to add the event type definitions directly to the EventType enum, which means the need to maintain a fork of Keycloak, with all the downsides that it brings.
On the other hand, having multiple custom event types is a real-world requirement, with some of the vendors having 50+ types on top of Keycloak's 70.
Examples include:
Important
Update 24 Nov 2026: Thomas Darimont suggested a more versatile solution. Please skip to his message; the original suggestion will be kept for historic purposes.
Solution
EventType.VENDORandEventType.VENDOR_ERRORevent types;vendor(String, Enum)in the EventBuilder classVENDORandVENDOR_TYPE, in the Details interface.org.keycloak.events.EventType
org.keycloak.events.EventBuilder
(it is expected that vendor-specific types be modeled as Java enums)
org.keycloak.events.Details
com.acme.events.AcmeEventType
com.acme.events.AcmeDetails
Example Use
Proof of Concept
Migration & Impact
(TODO)
Future
With the proposed changes, the custom events will be displayed as VENDOR and VENDOR_ERROR in the Admin UI. The details will be hidden under the expander toggle.
In the future, the Admin UI could be extended to support searching by vendor and vendor type, as well as displaying the vendor-specific details in the top-level view. This might require a new SPI, so that Keycloak always has a list of recognized vendors.
All reactions