Skip to content

Commit

Permalink
Fix missing ending brackets, ensure the event listener works with
Browse files Browse the repository at this point in the history
keycloak 15.0.2

Convert events as JSON Objects first, then serialize to strings. This
fixes errors where closing brackets could be missing.

Ensure the SPI works with keycloak 15.0.2.

Disable obsolete deployment test.

Change-Id: Ibd7fe78bffa89dcd203e74c72cd7b818fc2ddeb3
  • Loading branch information
mhuin committed Jun 9, 2022
1 parent 079d47c commit 894a411
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 55 deletions.
5 changes: 1 addition & 4 deletions .zuul.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
check:
jobs:
- maven-build
- keycloak-test-deploy:
dependencies:
- maven-build
gate:
jobs:
- keycloak-test-deploy
- maven-build
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<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">
<groupId>org.softwarefactory.keycloak.providers.events.mqtt</groupId>
<version>9.0.0</version>
<version>15.0.2</version>

<name>Keycloak: Event Publisher to MQTT</name>
<description/>
Expand Down Expand Up @@ -122,6 +122,11 @@
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

import org.json.simple.JSONObject;

import java.util.Map;
import java.util.Set;
import java.lang.Exception;
Expand Down Expand Up @@ -130,64 +132,55 @@ private MqttMessage toPayload(String s) {
}

private String toString(Event event) {
StringBuilder sb = new StringBuilder();

sb.append("{'type': '");
sb.append(event.getType());
sb.append("', 'realmId': '");
sb.append(event.getRealmId());
sb.append("', 'clientId': '");
sb.append(event.getClientId());
sb.append("', 'userId': '");
sb.append(event.getUserId());
sb.append("', 'ipAddress': '");
sb.append(event.getIpAddress());
sb.append("'");

if (event.getError() != null) {
sb.append(", 'error': '");
sb.append(event.getError());
sb.append("'");
}
sb.append(", 'details': {");
JSONObject obj = toJSON(event);
return obj.toString();

}

private JSONObject toJSON(Event event) {
JSONObject ev = new JSONObject();

ev.put("type", event.getType().toString());
ev.put("realmId", event.getRealmId());
ev.put("clientId", event.getClientId());
ev.put("userId", event.getUserId());
ev.put("ipAddress", event.getIpAddress());
ev.put("time", event.getTime());

ev.put("error", event.getError());

JSONObject evDetails = new JSONObject();
if (event.getDetails() != null) {
for (Map.Entry<String, String> e : event.getDetails().entrySet()) {
sb.append("'");
sb.append(e.getKey());
sb.append("': '");
sb.append(e.getValue());
sb.append("', ");
evDetails.put(e.getKey(), e.getValue());
}
}
sb.append("}}");
ev.put("details", evDetails);

return sb.toString();
return ev;
}

private String toString(AdminEvent adminEvent) {
StringBuilder sb = new StringBuilder();

sb.append("{'type': '");
sb.append(adminEvent.getOperationType());
sb.append("', 'realmId': '");
sb.append(adminEvent.getAuthDetails().getRealmId());
sb.append("', 'clientId': '");
sb.append(adminEvent.getAuthDetails().getClientId());
sb.append("', 'userId': '");
sb.append(adminEvent.getAuthDetails().getUserId());
sb.append("', 'ipAddress': '");
sb.append(adminEvent.getAuthDetails().getIpAddress());
sb.append("', 'resourcePath': '");
sb.append(adminEvent.getResourcePath());
sb.append("'");

if (adminEvent.getError() != null) {
sb.append(", 'error': '");
sb.append(adminEvent.getError());
sb.append("'");
}
sb.append("}");
return sb.toString();
JSONObject obj = toJSON(adminEvent);
return obj.toString();

}

private JSONObject toJSON(AdminEvent adminEvent) {
JSONObject ev = new JSONObject();

ev.put("type",adminEvent.getOperationType().toString());
ev.put("realmId", adminEvent.getAuthDetails().getRealmId());
ev.put("clientId", adminEvent.getAuthDetails().getClientId());
ev.put("userId", adminEvent.getAuthDetails().getUserId());
ev.put("ipAddress", adminEvent.getAuthDetails().getIpAddress());
ev.put("time", adminEvent.getTime());
ev.put("resourcePath", adminEvent.getResourcePath());
ev.put("resourceType", adminEvent.getResourceTypeAsString());

ev.put("error", adminEvent.getError());

return ev;
}

@Override
Expand Down

0 comments on commit 894a411

Please sign in to comment.