This is a Keycloak Service Provider Interface (SPI) that sends email notifications to users when their account is impersonated by an administrator or another user. This helps improve security awareness and transparency in your Keycloak instance.
- Automatically detects impersonation events in Keycloak
- Sends email notifications to impersonated users
- Includes details about who performed the impersonation and when
- Supports both HTML and plain text email formats
- Java 21 or higher
- Maven 3.8.1 or higher
- Keycloak 26.2.4 or compatible version
To build the project, run the following Maven command from the project root directory:
mvn clean installThis will compile the code and create a JAR file in the target directory named impersonation-notification-1.0.1.jar.
-
Copy the generated JAR file from the
targetdirectory to the Keycloak providers directory:cp target/impersonation-notification-1.0.1.jar /path/to/keycloak/providers/
Note: The exact path depends on your Keycloak installation. For a standard installation, this might be:
/opt/keycloak/providers/(standalone server)/opt/jboss/keycloak/standalone/deployments/(older versions or JBoss-based installations)
-
Restart Keycloak to load the new provider.
-
Copy the theme directory to the Keycloak themes directory:
cp -r theme_impersonation_notification /path/to/keycloak/themes/
Note: The exact path depends on your Keycloak installation. For a standard installation, this might be:
/opt/keycloak/themes/(standalone server)/opt/jboss/keycloak/themes/(older versions or JBoss-based installations)
-
Restart Keycloak to load the new theme.
-
Log in to the Keycloak Admin Console.
-
Select the realm where you want to enable the impersonation notification.
-
Go to Realm Settings > Events.
-
Click on the Config tab.
-
In the Event Listeners field, add
impersonation-notificationto the list of event listeners. -
Click Save.
-
Make sure email settings are properly configured for your realm:
- Go to Realm Settings > Email
- Configure the SMTP server settings
- Save the configuration
-
(Optional) To use the custom email theme:
- Go to Realm Settings > Themes
- Select
theme_impersonation_notificationfrom the Email Theme dropdown - Click Save
When an administrator or authorized user impersonates another user in Keycloak:
- The SPI detects the impersonation event
- It retrieves information about both the impersonator and the impersonated user
- It sends an email to the impersonated user with details about the impersonation
- The email includes:
- The name and username of the impersonated user
- The date and time of the impersonation
- The name and username of the impersonator
You can customize the email templates and messages by modifying the files in the theme_impersonation_notification directory:
email/html/impersonation-notification.ftl: HTML email templateemail/text/impersonation-notification.ftl: Plain text email templateemail/messages/messages_en.properties: Email message texts
After modifying these files, you'll need to rebuild the theme and reinstall it.
- If emails are not being sent, check the Keycloak server logs for any error messages.
- Verify that the email configuration in your realm is correct.
- Make sure the impersonated user has a verified email address in their account.
- Check that the event listener is properly registered in the realm configuration.
This project is open source and available under The Unlicense.