-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: first commit importing old project
- Loading branch information
Carlos Jiménez
committed
Oct 17, 2024
1 parent
17bea1d
commit 129fcbe
Showing
8 changed files
with
259 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
name: Build and Release | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v3 | ||
|
||
- name: Set up JDK 11 | ||
uses: actions/setup-java@v3 | ||
with: | ||
distribution: 'temurin' | ||
java-version: '11' | ||
|
||
- name: Build with Maven | ||
run: mvn clean package | ||
|
||
- name: Upload JAR as artifact | ||
uses: actions/upload-artifact@v3 | ||
with: | ||
name: keycloak-webhook-listener | ||
path: target/*.jar | ||
|
||
# Comment the release workflow for now, as we lack the permissions to create a release on GitHub | ||
# release: | ||
# needs: build | ||
# runs-on: ubuntu-latest | ||
# steps: | ||
# - name: Download JAR artifact | ||
# uses: actions/download-artifact@v3 | ||
# with: | ||
# name: keycloak-webhook-listener | ||
|
||
# - name: Create Release | ||
# id: create_release | ||
# uses: actions/create-release@v1 | ||
# env: | ||
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
# with: | ||
# tag_name: v1.0.${{ github.run_number }} | ||
# release_name: Release v1.0.${{ github.run_number }} | ||
# draft: false | ||
# prerelease: false | ||
|
||
# - name: Upload JAR to Release | ||
# uses: actions/upload-release-asset@v1 | ||
# env: | ||
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
# with: | ||
# upload_url: ${{ steps.create_release.outputs.upload_url }} | ||
# asset_path: target/*.jar | ||
# asset_name: keycloak-webhook-listener.jar | ||
# asset_content_type: application/java-archive |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,9 @@ | ||
# keycloak-webhook-listener | ||
|
||
Custom listener for using webhooks in Keycloak | ||
|
||
To build the project, run: | ||
|
||
``` | ||
mvn clean package | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.example.keycloak</groupId> | ||
<artifactId>keycloak-webhook-listener</artifactId> | ||
<packaging>jar</packaging> | ||
<version>1.0.0</version> | ||
<name>keycloak-webhook-listener</name> | ||
<url>http://maven.apache.org</url> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.keycloak</groupId> | ||
<artifactId>keycloak-core</artifactId> | ||
<version>${keycloak.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.keycloak</groupId> | ||
<artifactId>keycloak-server-spi</artifactId> | ||
<version>${keycloak.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.keycloak</groupId> | ||
<artifactId>keycloak-server-spi-private</artifactId> | ||
<version>${keycloak.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.keycloak</groupId> | ||
<artifactId>keycloak-services</artifactId> | ||
<version>${keycloak.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<!-- Dependency to make HTTP requests --> | ||
<dependency> | ||
<groupId>org.apache.httpcomponents</groupId> | ||
<artifactId>httpclient</artifactId> | ||
<version>4.5.13</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.13.2</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<!-- JSON Processing --> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-databind</artifactId> | ||
<version>2.11.3</version> | ||
</dependency> | ||
</dependencies> | ||
<properties> | ||
<keycloak.version>20.0.5</keycloak.version> | ||
<maven.compiler.source>11</maven.compiler.source> | ||
<maven.compiler.target>11</maven.compiler.target> | ||
</properties> | ||
</project> |
71 changes: 71 additions & 0 deletions
71
src/main/java/com/example/keycloak/WebhookEventListenerProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.example.keycloak; | ||
|
||
import org.keycloak.events.Event; | ||
import org.keycloak.events.EventListenerProvider; | ||
import org.keycloak.events.admin.AdminEvent; | ||
import org.keycloak.models.KeycloakSession; | ||
import org.apache.http.client.methods.HttpPost; | ||
import org.apache.http.entity.StringEntity; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.HttpClients; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class WebhookEventListenerProvider implements EventListenerProvider { | ||
private static final Logger logger = LoggerFactory.getLogger(WebhookEventListenerProvider.class); | ||
private final KeycloakSession session; | ||
|
||
public WebhookEventListenerProvider(KeycloakSession session) { | ||
this.session = session; | ||
} | ||
|
||
@Override | ||
public void onEvent(Event event) { | ||
logger.info("Received event: {}", event); | ||
String realmName = session.getContext().getRealm().getName(); | ||
String webhookUrl = getWebhookUrlForRealm(realmName); | ||
sendWebhook("USER_EVENT", event, webhookUrl); | ||
} | ||
|
||
private String getWebhookUrlForRealm(String realmName) { | ||
String envVarName = "WEBHOOK_URL_" + realmName.toUpperCase().replace("-", "_"); | ||
String webhookUrl = System.getenv(envVarName); | ||
if (webhookUrl == null) { | ||
throw new IllegalArgumentException("Webhook URL not specified for realm: " + realmName | ||
+ ". Please set the WEBHOOK_URL_" + realmName.toUpperCase().replace("-", "_) environment variable.")); | ||
} | ||
return webhookUrl; | ||
} | ||
|
||
@Override | ||
public void onEvent(AdminEvent event, boolean includeRepresentation) { | ||
logger.info("Received admin event: {}", event); | ||
String realmName = session.getContext().getRealm().getName(); | ||
String webhookUrl = getWebhookUrlForRealm(realmName); | ||
sendWebhook("ADMIN_EVENT", event, webhookUrl); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
logger.info("Closing WebhookEventListenerProvider"); | ||
} | ||
|
||
private void sendWebhook(String type, Object event, String webhookUrl) { | ||
try (CloseableHttpClient client = HttpClients.createDefault()) { | ||
HttpPost httpPost = new HttpPost(webhookUrl); | ||
httpPost.setHeader("Content-Type", "application/json"); | ||
|
||
ObjectMapper mapper = new ObjectMapper(); | ||
String json = mapper.writeValueAsString(event); | ||
|
||
httpPost.setEntity(new StringEntity(json)); | ||
|
||
logger.info("Sending webhook for event type: {}", type); | ||
client.execute(httpPost); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/com/example/keycloak/WebhookEventListenerProviderFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.example.keycloak; | ||
|
||
import org.keycloak.models.KeycloakSession; | ||
import org.keycloak.events.EventListenerProvider; | ||
import org.keycloak.events.EventListenerProviderFactory; | ||
import org.jboss.logging.Logger; | ||
|
||
public class WebhookEventListenerProviderFactory implements EventListenerProviderFactory { | ||
|
||
private static final Logger logger = Logger.getLogger(WebhookEventListenerProviderFactory.class); | ||
|
||
@Override | ||
public EventListenerProvider create(KeycloakSession session) { | ||
logger.info("Creating WebhookEventListenerProvider"); | ||
return new WebhookEventListenerProvider(session); | ||
} | ||
|
||
@Override | ||
public void init(org.keycloak.Config.Scope config) { | ||
// Initialize configuration if needed | ||
logger.info("Initializing WebhookEventListenerProviderFactory"); | ||
} | ||
|
||
@Override | ||
public void postInit(org.keycloak.models.KeycloakSessionFactory factory) { | ||
// Post-initialization configuration if needed | ||
logger.info("Post-initializing WebhookEventListenerProviderFactory"); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
// Cleanup resources if needed | ||
logger.info("Closing WebhookEventListenerProviderFactory"); | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
// This ID should be unique and used to identify the provider in Keycloak | ||
return "webhook-event-listener"; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
src/main/resources/META-INF/services/org.keycloak.events.EventListenerProviderFactory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
com.example.keycloak.WebhookEventListenerProviderFactory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.example.keycloak; | ||
|
||
import org.junit.Test; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
/** | ||
* Unit test for simple App. | ||
*/ | ||
public class AppTest { | ||
|
||
/** | ||
* Rigorous Test :-) | ||
*/ | ||
@Test | ||
public void testApp() { | ||
assertTrue(true); | ||
} | ||
} |