2
2
3
3
import org .jboss .logging .Logger ;
4
4
5
+ import com .google .gson .JsonObject ;
6
+ import com .google .gson .JsonParser ;
7
+
5
8
import java .io .BufferedReader ;
6
9
import java .io .IOException ;
10
+ import java .io .InputStream ;
7
11
import java .io .InputStreamReader ;
8
12
import java .io .OutputStream ;
9
13
import java .net .HttpURLConnection ;
12
16
13
17
public class Client {
14
18
private static final Logger log = Logger .getLogger (Client .class );
15
- private static final String WEBHOOK_URL = "WEBHOOK_URL" ;
19
+ private static final String WEBHOOK_URL = "KC_SPI_EVENT_LISTENER_KEYCLOAK_EVENTS_WEBHOOK_URL" ;
20
+
21
+ private static final String WEBHOOK_KEYCLOAK_ISSUER = "KC_SPI_EVENT_LISTENER_KEYCLOAK_EVENTS_WEBHOOK_KEYCLOAK_ISSUER" ;
22
+ private static final String WEBHOOK_KEYCLOAK_CLIENT_ID = "KC_SPI_EVENT_LISTENER_KEYCLOAK_EVENTS_WEBHOOK_KEYCLOAK_CLIENT_ID" ;
23
+ private static final String WEBHOOK_KEYCLOAK_CLIENT_SECRET = "KC_SPI_EVENT_LISTENER_KEYCLOAK_EVENTS_WEBHOOK_KEYCLOAK_CLIENT_SECRET" ;
24
+ private static final String WEBHOOK_KEYCLOAK_REALM = "KC_SPI_EVENT_LISTENER_KEYCLOAK_EVENTS_WEBHOOK_KEYCLOAK_REALM" ;
25
+ // KEYCLOAK_ISSUER=https://infrapal.serverlesssalad.com/auth
26
+ // KEYCLOAK_CLIENT_ID=ss-backend
27
+ // KEYCLOAK_CLIENT_SECRET=lx2kfVGrqRJgwxqpFEc4ot5MTSYyh8A4
28
+
29
+
16
30
17
31
public static void postService (String data ) throws IOException {
18
32
try {
@@ -23,8 +37,11 @@ public static void postService(String data) throws IOException {
23
37
throw new IllegalArgumentException ("Environment variable WEBHOOK_URL is not set or is empty." );
24
38
}
25
39
40
+ String token = getAuthToken ();
41
+
26
42
URL url = URI .create (urlString ).toURL ();
27
43
HttpURLConnection conn = (HttpURLConnection ) url .openConnection ();
44
+ conn .setRequestProperty ("Authorization" , "Bearer " + token ); // Add token to header
28
45
conn .setDoOutput (true );
29
46
conn .setRequestMethod ("POST" );
30
47
conn .setRequestProperty ("Content-Type" , "application/json; utf-8" );
@@ -50,4 +67,50 @@ public static void postService(String data) throws IOException {
50
67
throw new IOException ("Failed to post service: " + e .getMessage (), e );
51
68
}
52
69
}
70
+
71
+ private static String getAuthToken () {
72
+ // Example: Use Keycloak's Admin Client API to get a token
73
+ String issuer = System .getenv (WEBHOOK_KEYCLOAK_ISSUER );
74
+ String clientId = System .getenv (WEBHOOK_KEYCLOAK_CLIENT_ID );
75
+ String clientSecret = System .getenv (WEBHOOK_KEYCLOAK_CLIENT_SECRET );
76
+ String realm = System .getenv (WEBHOOK_KEYCLOAK_REALM );
77
+
78
+ if (issuer == null || issuer .isEmpty () || clientId == null || clientId .isEmpty () || clientSecret == null || clientSecret .isEmpty () || realm == null || realm .isEmpty ()) {
79
+ throw new IllegalArgumentException ("Environment variable WEBHOOK_KEYCLOAK_ISSUER or WEBHOOK_KEYCLOAK_CLIENT_ID or WEBHOOK_KEYCLOAK_CLIENT_SECRET or WEBHOOK_KEYCLOAK_REALM is not set or is empty." );
80
+ }
81
+
82
+ String authUrl = issuer + "/realms/" + realm + "/protocol/openid-connect/token" ;
83
+ try {
84
+ URL url = new URL (authUrl );
85
+ HttpURLConnection connection = (HttpURLConnection ) url .openConnection ();
86
+ connection .setRequestMethod ("POST" );
87
+ connection .setDoOutput (true );
88
+ connection .setRequestProperty ("Content-Type" , "application/x-www-form-urlencoded" );
89
+
90
+ String body = "grant_type=client_credentials&client_id=" + clientId + "&client_secret=" + clientSecret ;
91
+ try (OutputStream os = connection .getOutputStream ()) {
92
+ os .write (body .getBytes ("utf-8" ));
93
+ }
94
+
95
+ if (connection .getResponseCode () == 200 ) {
96
+ try (InputStream is = connection .getInputStream ();
97
+ BufferedReader reader = new BufferedReader (new InputStreamReader (is ))) {
98
+ StringBuilder response = new StringBuilder ();
99
+ String line ;
100
+ while ((line = reader .readLine ()) != null ) {
101
+ response .append (line );
102
+ }
103
+
104
+ // Parse the token from the response
105
+ String responseBody = response .toString ();
106
+ JsonObject jsonResponse = JsonParser .parseString (responseBody ).getAsJsonObject ();
107
+ return jsonResponse .get ("access_token" ).getAsString ();
108
+ }
109
+ } else {
110
+ throw new RuntimeException ("Failed to get token: " + connection .getResponseCode ());
111
+ }
112
+ } catch (Exception e ) {
113
+ throw new RuntimeException ("Error while obtaining auth token" , e );
114
+ }
115
+ }
53
116
}
0 commit comments