├── .gitignore ├── Dockerfile ├── LICENSE ├── Makefile ├── README.md ├── pom.xml ├── src └── main │ ├── java │ └── org │ │ └── softwarefactory │ │ └── keycloak │ │ └── providers │ │ └── events │ │ └── http │ │ ├── HTTPEventListenerProvider.java │ │ └── HTTPEventListenerProviderFactory.java │ └── resources │ └── META-INF │ └── services │ └── org.keycloak.events.EventListenerProviderFactory └── target ├── classes ├── META-INF │ └── services │ │ └── org.keycloak.events.EventListenerProviderFactory └── org │ └── softwarefactory │ └── keycloak │ └── providers │ └── events │ └── http │ ├── HTTPEventListenerProvider.class │ └── HTTPEventListenerProviderFactory.class ├── event-listener-http-jar-with-dependencies.jar ├── event-listener-http.jar ├── maven-archiver └── pom.properties └── maven-status └── maven-compiler-plugin └── compile └── default-compile ├── createdFiles.lst └── inputFiles.lst /.gitignore: -------------------------------------------------------------------------------- 1 | # Just so that we don't store the docker image build output into the repository. 2 | mvn-output/ -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # 2 | # Build stage 3 | # 4 | FROM maven:3.6.0-jdk-11-slim 5 | 6 | WORKDIR / 7 | 8 | # Copy dependencies for the build 9 | COPY src /home/app/src 10 | COPY pom.xml /home/app 11 | 12 | # Download dependencies and package the app 13 | RUN mvn -f /home/app/pom.xml clean package 14 | 15 | # Generate destination folder 16 | RUN mkdir /output 17 | RUN chmod +w /output 18 | 19 | # We sleep for a few seconds, just enough time for the `make package-image` command 20 | # to successfully to copy out the files from the running container. 21 | CMD sleep 5 -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | IMAGE=jessylenne/keycloak-event-listener-http 3 | VERSION=latest 4 | CONTAINER_NAME=keycloak-listener-war-builder 5 | JAR_ONE=event-listener-http-jar-with-dependencies.jar 6 | JAR_TWO=event-listener-http.jar 7 | 8 | package-image: 9 | # Build the docker image that we'll use to constrct the maven package 10 | docker build . -t "${IMAGE}:${VERSION}" 11 | # Build the maven package, then save the resulting files into `mvn-output` 12 | docker run -d --rm --name ${CONTAINER_NAME} -v ${PWD}/mvn-output:/output ${IMAGE}:${VERSION} 13 | mkdir -p mvn-output 14 | # Ensure that you have proper write permissions to write on the `mvn-output` folder. 15 | # See: https://stackoverflow.com/a/45276559/1323398 16 | docker cp ${CONTAINER_NAME}:/home/app/target/event-listener-http-jar-with-dependencies.jar ${PWD}/mvn-output/event-listener-http-jar-with-dependencies.jar 17 | docker cp ${CONTAINER_NAME}:/home/app/target/event-listener-http.jar ${PWD}/mvn-output/event-listener-http.jar -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # keycloak-event-listener-http 2 | 3 | A Keycloak SPI that publishes events to an HTTP Webhook. 4 | A (largely) adaptation of @mhui mhuin/keycloak-event-listener-mqtt SPI. 5 | Extended by @darrensapalo to [enable building the JAR files from docker images](https://sapalo.dev/2021/06/16/send-keycloak-webhook-events/). 6 | 7 | # Build 8 | 9 | ## Build on your local machine 10 | 11 | ``` 12 | mvn clean install 13 | ``` 14 | 15 | ## Build using docker 16 | 17 | Alternatively, you can [build the JAR files from a docker image](https://sapalo.dev/2021/06/16/send-keycloak-webhook-events/). You must have `docker` installed. 18 | 19 | 1. Run `make package-image`. 20 | 2. The JAR files should show up on your `mvn-output` folder. 21 | 22 | If you encounter the following issue: 23 | ``` 24 | open {PATH}/mvn-output/event-listener-http-jar-with-dependencies.jar: permission denied 25 | ``` 26 | 27 | Simply add write permissions to the `mvn-output` folder: 28 | 29 | ``` 30 | sudo chown $USER:$USER mvn-output 31 | ``` 32 | 33 | # Deploy 34 | 35 | * Copy target/event-listener-http-jar-with-dependencies.jar to {KEYCLOAK_HOME}/standalone/deployments 36 | * Edit standalone.xml to configure the Webhook settings. Find the following 37 | section in the configuration: 38 | 39 | ``` 40 | 41 | auth 42 | ``` 43 | 44 | And add below: 45 | 46 | ``` 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | ``` 58 | Leave username and password out if the service allows anonymous access. 59 | If unset, the default message topic is "keycloak/events". 60 | 61 | * Restart the keycloak server. 62 | 63 | # Use 64 | Add/Update a user, your webhook should be called, looks at the keycloak syslog for debug 65 | 66 | Request example 67 | ``` 68 | { 69 | "type": "REGISTER", 70 | "realmId": "myrealm", 71 | "clientId": "heva", 72 | "userId": "bcee5034-c65f-4d7c-9036-034042f0a054", 73 | "ipAddress": "172.21.0.1", 74 | "details": { 75 | "auth_method": "openid-connect", 76 | "auth_type": "code", 77 | "register_method": "form", 78 | "redirect_uri": "http://nginx:8000/", 79 | "code_id": "98bfe6b2-b8c2-4b82-bc85-9cd033324ec9", 80 | "email": "fake.email@service.com", 81 | "username": "username" 82 | } 83 | } 84 | ``` -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 17 | 18 | 20 | org.softwarefactory.keycloak.providers.events.http 21 | 5.0.0 22 | 23 | Keycloak: Event Publisher to HTTP 24 | 25 | 4.0.0 26 | 27 | event-listener-http 28 | jar 29 | 30 | 31 | 14.0.1.Final 32 | ${project.version} 33 | 34 | 1.2.2.Final 35 | 1.0.2.Final 36 | 1.0.1.Final 37 | 7.4.Final 38 | 2.6 39 | 1.4.1 40 | 2.19.1 41 | 1.6.0 42 | 1.8 43 | 1.4 44 | 3.0.2 45 | 3.1 46 | 47 | 2.3.2 48 | 1.2.1.Final 49 | 2.0.2.Final 50 | 8.2.1.Final 51 | 4.12 52 | 1.3 53 | 1.6.1 54 | 2.9.5 55 | 56 | true 57 | ./jboss-cli.sh 58 | 10090 59 | 3.11.0 60 | 1.4.0.Final 61 | 2.5.1 62 | 1.1.2 63 | 1.0.1 64 | 65 | 66 | 67 | 68 | org.keycloak 69 | keycloak-core 70 | ${version.keycloak} 71 | 72 | 73 | org.keycloak 74 | keycloak-server-spi 75 | ${version.keycloak} 76 | provided 77 | 78 | 79 | org.keycloak 80 | keycloak-server-spi-private 81 | ${version.keycloak} 82 | provided 83 | 84 | 85 | org.jboss.arquillian.protocol 86 | arquillian-protocol-servlet 87 | 1.4.1.Final 88 | test 89 | 90 | 91 | org.jboss.arquillian.graphene 92 | graphene-webdriver 93 | ${arquillian-graphene.version} 94 | pom 95 | test 96 | 97 | 98 | org.jboss.arquillian.extension 99 | arquillian-phantom-driver 100 | ${arquillian-phantom.version} 101 | test 102 | 103 | 104 | org.wildfly.extras.creaper 105 | creaper-core 106 | ${version.creaper} 107 | test 108 | 109 | 110 | com.google.guava 111 | guava 112 | 113 | 114 | 115 | 116 | org.hamcrest 117 | hamcrest-all 118 | 1.3 119 | 120 | 121 | com.squareup.okhttp3 122 | okhttp 123 | 4.3.1 124 | 125 | 126 | 127 | 128 | event-listener-http 129 | 130 | 131 | 132 | org.apache.maven.plugins 133 | maven-compiler-plugin 134 | ${version.compiler.maven.plugin} 135 | 136 | 1.8 137 | 1.8 138 | 139 | 140 | 141 | org.apache.maven.plugins 142 | maven-surefire-plugin 143 | ${version.surefire.plugin} 144 | 145 | 146 | ${keycloak.management.port} 147 | ${project.build.directory} 148 | 149 | 150 | 151 | 152 | 153 | org.apache.maven.plugins 154 | maven-assembly-plugin 155 | 2.4.1 156 | 157 | 158 | 159 | jar-with-dependencies 160 | 161 | 162 | 163 | 164 | make-assembly 165 | 166 | package 167 | 168 | single 169 | 170 | 171 | 172 | 173 | 174 | org.wildfly.plugins 175 | wildfly-maven-plugin 176 | 1.2.2.Final 177 | 178 | ${wildfly.skip} 179 | ${project.build.finalName}.jar 180 | ${keycloak.management.port} 181 | 182 | 183 | 184 | maven-enforcer-plugin 185 | 186 | 187 | enforce-quickstart-realm-file-exist 188 | validate 189 | 190 | 191 | 192 | 193 | 194 | 195 | -------------------------------------------------------------------------------- /src/main/java/org/softwarefactory/keycloak/providers/events/http/HTTPEventListenerProvider.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Red Hat, Inc. and/or its affiliates 3 | * and other contributors as indicated by the @author tags. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.softwarefactory.keycloak.providers.events.http; 19 | 20 | import org.keycloak.events.Event; 21 | import org.keycloak.events.EventListenerProvider; 22 | import org.keycloak.events.EventType; 23 | import org.keycloak.events.admin.AdminEvent; 24 | import org.keycloak.events.admin.OperationType; 25 | 26 | import java.util.Map; 27 | import java.util.Set; 28 | import java.lang.Exception; 29 | 30 | import okhttp3.*; 31 | import okhttp3.OkHttpClient.Builder; 32 | 33 | import java.io.IOException; 34 | 35 | /** 36 | * @author Jessy Lenne 37 | */ 38 | public class HTTPEventListenerProvider implements EventListenerProvider { 39 | private final OkHttpClient httpClient = new OkHttpClient(); 40 | private Set excludedEvents; 41 | private Set excludedAdminOperations; 42 | private String serverUri; 43 | private String username; 44 | private String password; 45 | public static final String publisherId = "keycloak"; 46 | public String TOPIC; 47 | 48 | public HTTPEventListenerProvider(Set excludedEvents, Set excludedAdminOperations, String serverUri, String username, String password, String topic) { 49 | this.excludedEvents = excludedEvents; 50 | this.excludedAdminOperations = excludedAdminOperations; 51 | this.serverUri = serverUri; 52 | this.username = username; 53 | this.password = password; 54 | this.TOPIC = topic; 55 | } 56 | 57 | @Override 58 | public void onEvent(Event event) { 59 | // Ignore excluded events 60 | if (excludedEvents != null && excludedEvents.contains(event.getType())) { 61 | return; 62 | } else { 63 | String stringEvent = toString(event); 64 | try { 65 | RequestBody formBody = new FormBody.Builder() 66 | .add("json", stringEvent) 67 | .build(); 68 | 69 | okhttp3.Request.Builder builder = new Request.Builder() 70 | .url(this.serverUri) 71 | .addHeader("User-Agent", "KeycloakHttp Bot"); 72 | 73 | 74 | if (this.username != null && this.password != null) { 75 | builder.addHeader("Authorization", "Basic " + this.username + ":" + this.password.toCharArray()); 76 | } 77 | 78 | Request request = builder.post(formBody) 79 | .build(); 80 | 81 | Response response = httpClient.newCall(request).execute(); 82 | 83 | if (!response.isSuccessful()) { 84 | throw new IOException("Unexpected code " + response); 85 | } 86 | 87 | // Get response body 88 | System.out.println(response.body().string()); 89 | } catch(Exception e) { 90 | // ? 91 | System.out.println("UH OH!! " + e.toString()); 92 | e.printStackTrace(); 93 | return; 94 | } 95 | } 96 | } 97 | 98 | @Override 99 | public void onEvent(AdminEvent event, boolean includeRepresentation) { 100 | // Ignore excluded operations 101 | if (excludedAdminOperations != null && excludedAdminOperations.contains(event.getOperationType())) { 102 | return; 103 | } else { 104 | String stringEvent = toString(event); 105 | try { 106 | RequestBody formBody = new FormBody.Builder() 107 | .add("json", stringEvent) 108 | .build(); 109 | 110 | okhttp3.Request.Builder builder = new Request.Builder() 111 | .url(this.serverUri) 112 | .addHeader("User-Agent", "KeycloakHttp Bot"); 113 | 114 | 115 | if (this.username != null && this.password != null) { 116 | builder.addHeader("Authorization", "Basic " + this.username + ":" + this.password.toCharArray()); 117 | } 118 | 119 | Request request = builder.post(formBody) 120 | .build(); 121 | 122 | Response response = httpClient.newCall(request).execute(); 123 | 124 | if (!response.isSuccessful()) { 125 | throw new IOException("Unexpected code " + response); 126 | } 127 | 128 | // Get response body 129 | System.out.println(response.body().string()); 130 | } catch(Exception e) { 131 | // ? 132 | System.out.println("UH OH!! " + e.toString()); 133 | e.printStackTrace(); 134 | return; 135 | } 136 | } 137 | } 138 | 139 | 140 | private String toString(Event event) { 141 | StringBuilder sb = new StringBuilder(); 142 | 143 | sb.append("{'type': '"); 144 | sb.append(event.getType()); 145 | sb.append("', 'realmId': '"); 146 | sb.append(event.getRealmId()); 147 | sb.append("', 'clientId': '"); 148 | sb.append(event.getClientId()); 149 | sb.append("', 'userId': '"); 150 | sb.append(event.getUserId()); 151 | sb.append("', 'ipAddress': '"); 152 | sb.append(event.getIpAddress()); 153 | sb.append("'"); 154 | 155 | if (event.getError() != null) { 156 | sb.append(", 'error': '"); 157 | sb.append(event.getError()); 158 | sb.append("'"); 159 | } 160 | sb.append(", 'details': {"); 161 | if (event.getDetails() != null) { 162 | for (Map.Entry e : event.getDetails().entrySet()) { 163 | sb.append("'"); 164 | sb.append(e.getKey()); 165 | sb.append("': '"); 166 | sb.append(e.getValue()); 167 | sb.append("', "); 168 | } 169 | } 170 | sb.append("}}"); 171 | 172 | return sb.toString(); 173 | } 174 | 175 | 176 | private String toString(AdminEvent adminEvent) { 177 | StringBuilder sb = new StringBuilder(); 178 | 179 | sb.append("{'type': '"); 180 | sb.append(adminEvent.getOperationType()); 181 | sb.append("', 'realmId': '"); 182 | sb.append(adminEvent.getAuthDetails().getRealmId()); 183 | sb.append("', 'clientId': '"); 184 | sb.append(adminEvent.getAuthDetails().getClientId()); 185 | sb.append("', 'userId': '"); 186 | sb.append(adminEvent.getAuthDetails().getUserId()); 187 | sb.append("', 'ipAddress': '"); 188 | sb.append(adminEvent.getAuthDetails().getIpAddress()); 189 | sb.append("', 'resourcePath': '"); 190 | sb.append(adminEvent.getResourcePath()); 191 | sb.append("'"); 192 | 193 | if (adminEvent.getError() != null) { 194 | sb.append(", 'error': '"); 195 | sb.append(adminEvent.getError()); 196 | sb.append("'"); 197 | } 198 | sb.append("}"); 199 | return sb.toString(); 200 | } 201 | 202 | @Override 203 | public void close() { 204 | } 205 | 206 | } 207 | -------------------------------------------------------------------------------- /src/main/java/org/softwarefactory/keycloak/providers/events/http/HTTPEventListenerProviderFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2019 Red Hat, Inc. and/or its affiliates 3 | * and other contributors as indicated by the @author tags. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | package org.softwarefactory.keycloak.providers.events.http; 19 | 20 | import org.keycloak.Config; 21 | import org.keycloak.events.EventListenerProvider; 22 | import org.keycloak.events.EventListenerProviderFactory; 23 | import org.keycloak.events.EventType; 24 | import org.keycloak.events.admin.OperationType; 25 | import org.keycloak.models.KeycloakSession; 26 | import org.keycloak.models.KeycloakSessionFactory; 27 | 28 | import java.util.HashSet; 29 | import java.util.Set; 30 | import java.lang.Exception; 31 | 32 | /** 33 | * @author Jessy Lennee 34 | */ 35 | public class HTTPEventListenerProviderFactory implements EventListenerProviderFactory { 36 | 37 | private Set excludedEvents; 38 | private Set excludedAdminOperations; 39 | private String serverUri; 40 | private String username; 41 | private String password; 42 | private String topic; 43 | 44 | @Override 45 | public EventListenerProvider create(KeycloakSession session) { 46 | return new HTTPEventListenerProvider(excludedEvents, excludedAdminOperations, serverUri, username, password, topic); 47 | } 48 | 49 | @Override 50 | public void init(Config.Scope config) { 51 | String[] excludes = config.getArray("exclude-events"); 52 | if (excludes != null) { 53 | excludedEvents = new HashSet<>(); 54 | for (String e : excludes) { 55 | excludedEvents.add(EventType.valueOf(e)); 56 | } 57 | } 58 | 59 | String[] excludesOperations = config.getArray("excludesOperations"); 60 | if (excludesOperations != null) { 61 | excludedAdminOperations = new HashSet<>(); 62 | for (String e : excludesOperations) { 63 | excludedAdminOperations.add(OperationType.valueOf(e)); 64 | } 65 | } 66 | 67 | serverUri = config.get("serverUri", "http://nginx/frontend_dev.php/webhook/keycloak"); 68 | username = config.get("username", null); 69 | password = config.get("password", null); 70 | topic = config.get("topic", "keycloak/events"); 71 | } 72 | 73 | @Override 74 | public void postInit(KeycloakSessionFactory factory) { 75 | 76 | } 77 | @Override 78 | public void close() { 79 | } 80 | 81 | @Override 82 | public String getId() { 83 | return "http"; 84 | } 85 | 86 | } 87 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/services/org.keycloak.events.EventListenerProviderFactory: -------------------------------------------------------------------------------- 1 | 2 | # 3 | # Copyright 2019 Red Hat, Inc. and/or its affiliates 4 | # and other contributors as indicated by the @author tags. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | org.softwarefactory.keycloak.providers.events.http.HTTPEventListenerProviderFactory 20 | -------------------------------------------------------------------------------- /target/classes/META-INF/services/org.keycloak.events.EventListenerProviderFactory: -------------------------------------------------------------------------------- 1 | 2 | # 3 | # Copyright 2019 Red Hat, Inc. and/or its affiliates 4 | # and other contributors as indicated by the @author tags. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | org.softwarefactory.keycloak.providers.events.http.HTTPEventListenerProviderFactory 20 | -------------------------------------------------------------------------------- /target/classes/org/softwarefactory/keycloak/providers/events/http/HTTPEventListenerProvider.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jessylenne/keycloak-event-listener-http/e7cea26245c06a44158ba65167ef61413ed88c0b/target/classes/org/softwarefactory/keycloak/providers/events/http/HTTPEventListenerProvider.class -------------------------------------------------------------------------------- /target/classes/org/softwarefactory/keycloak/providers/events/http/HTTPEventListenerProviderFactory.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jessylenne/keycloak-event-listener-http/e7cea26245c06a44158ba65167ef61413ed88c0b/target/classes/org/softwarefactory/keycloak/providers/events/http/HTTPEventListenerProviderFactory.class -------------------------------------------------------------------------------- /target/event-listener-http-jar-with-dependencies.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jessylenne/keycloak-event-listener-http/e7cea26245c06a44158ba65167ef61413ed88c0b/target/event-listener-http-jar-with-dependencies.jar -------------------------------------------------------------------------------- /target/event-listener-http.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jessylenne/keycloak-event-listener-http/e7cea26245c06a44158ba65167ef61413ed88c0b/target/event-listener-http.jar -------------------------------------------------------------------------------- /target/maven-archiver/pom.properties: -------------------------------------------------------------------------------- 1 | #Generated by Maven 2 | #Tue Jan 28 10:33:34 CET 2020 3 | groupId=org.softwarefactory.keycloak.providers.events.http 4 | artifactId=event-listener-http 5 | version=5.0.0 6 | -------------------------------------------------------------------------------- /target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst: -------------------------------------------------------------------------------- 1 | org/softwarefactory/keycloak/providers/events/http/HTTPEventListenerProviderFactory.class 2 | org/softwarefactory/keycloak/providers/events/http/HTTPEventListenerProvider.class 3 | -------------------------------------------------------------------------------- /target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst: -------------------------------------------------------------------------------- 1 | /Users/jessylenne/Documents/www/keycloak-event-listener-http/src/main/java/org/softwarefactory/keycloak/providers/events/http/HTTPEventListenerProvider.java 2 | /Users/jessylenne/Documents/www/keycloak-event-listener-http/src/main/java/org/softwarefactory/keycloak/providers/events/http/HTTPEventListenerProviderFactory.java 3 | --------------------------------------------------------------------------------