1
1
package com .issuetracker .web .security ;
2
2
3
3
import static com .issuetracker .web .Constants .SERVER_URL ;
4
- import org .apache .http .client .HttpClient ;
5
- import org .apache .http .client .methods .HttpGet ;
6
- import org .keycloak .adapters .HttpClientBuilder ;
7
4
import static com .issuetracker .web .security .KeycloakAuthSession .*;
8
5
import java .io .IOException ;
9
- import java .io .InputStream ;
10
6
import java .util .ArrayList ;
11
- import java .util .Arrays ;
12
7
import java .util .HashSet ;
13
8
import java .util .List ;
14
9
import java .util .Set ;
15
10
import java .util .TreeSet ;
16
- import org .apache .http .HttpEntity ;
17
- import org .apache .http .HttpResponse ;
11
+ import javax .ws .rs .WebApplicationException ;
12
+ import javax .ws .rs .client .ClientRequestContext ;
13
+ import javax .ws .rs .client .ClientRequestFilter ;
14
+ import org .jboss .logging .Logger ;
15
+ import org .jboss .resteasy .client .jaxrs .ResteasyClient ;
16
+ import org .jboss .resteasy .client .jaxrs .ResteasyClientBuilder ;
18
17
import org .keycloak .KeycloakSecurityContext ;
19
18
import org .keycloak .representations .idm .RoleRepresentation ;
20
- import org .keycloak .representations .idm .UserRepresentation ;
21
- import org .keycloak .util .JsonSerialization ;
22
19
23
20
/**
24
21
*
25
22
* @author vramik
26
23
*/
27
24
public class KeycloakService {
25
+ private static final Logger log = Logger .getLogger (KeycloakService .class );
28
26
29
- private static class TypedListOfUser extends ArrayList <UserRepresentation > {
30
- }
31
27
private static class TypedSetOfRoles extends HashSet <RoleRepresentation > {
32
28
}
33
29
34
- public static class Failure extends Exception {
35
- private final int status ;
36
-
37
- public Failure (int status ) {
38
- this .status = status ;
39
- }
30
+ private static class AuthHedersRequestFilter implements ClientRequestFilter {
40
31
41
- public int getStatus () {
42
- return status ;
32
+ private final String tokenString ;
33
+
34
+ public AuthHedersRequestFilter (KeycloakSecurityContext session ) {
35
+ tokenString = session .getTokenString ();
43
36
}
44
- }
45
-
46
- public static List <UserRepresentation > getUsers () throws Failure {
47
- HttpClient client = new HttpClientBuilder ().disableTrustManager ().build ();
48
37
49
- try {
50
- HttpGet get = new HttpGet (SERVER_URL + "/auth/admin/realms/issue-tracker/users" );
51
- System .out .println ("GET: " + get .toString ());
52
- KeycloakSecurityContext session = getKeycloakSecurityContext ();
53
- get .addHeader ("Authorization" , "Bearer " + session .getTokenString ());
54
- System .out .println ("GET HEADER: " + Arrays .toString (get .getHeaders ("Authorization" )));
55
- try {
56
- HttpResponse response = client .execute (get );
57
- if (response .getStatusLine ().getStatusCode () != 200 ) {
58
- System .out .println ("STATUS CODE: " + response .getStatusLine ().getStatusCode ());
59
- throw new Failure (response .getStatusLine ().getStatusCode ());
60
- }
61
- HttpEntity entity = response .getEntity ();
62
- try (InputStream is = entity .getContent ()) {
63
- return JsonSerialization .readValue (is , TypedListOfUser .class );
64
- }
65
- } catch (IOException ex ) {
66
- throw new RuntimeException (ex );
67
- }
68
- } finally {
69
- client .getConnectionManager ().shutdown ();
38
+ @ Override
39
+ public void filter (ClientRequestContext requestContext ) throws IOException {
40
+ requestContext .getHeaders ().add ("Authorization" , "Bearer " + tokenString );
70
41
}
71
42
}
72
43
@@ -76,37 +47,26 @@ public static List<UserRepresentation> getUsers() throws Failure {
76
47
*
77
48
*/
78
49
public static List <String > getRealmRoles () {
79
- HttpClient client = new HttpClientBuilder ().disableTrustManager ().build ();
50
+ Set <String > roles = new TreeSet <>();
51
+ roles .add ("Public" );
52
+
53
+ KeycloakSecurityContext session = getKeycloakSecurityContext ();
54
+ if (session == null ) {
55
+ return new ArrayList <>(roles );
56
+ }
80
57
58
+ ResteasyClient client = new ResteasyClientBuilder ().build ();
59
+ client .register (new AuthHedersRequestFilter (session ));
81
60
try {
82
- HttpGet get = new HttpGet (SERVER_URL + "/auth/admin/realms/issue-tracker/roles" );
83
- KeycloakSecurityContext session = getKeycloakSecurityContext ();
84
- if (session == null ) {
85
- return new ArrayList <>();
86
- }
87
- get .addHeader ("Authorization" , "Bearer " + session .getTokenString ());
88
- try {
89
- HttpResponse response = client .execute (get );
90
- if (response .getStatusLine ().getStatusCode () != 200 ) {
91
- throw new Failure (response .getStatusLine ().getStatusCode ());
92
- }
93
- HttpEntity entity = response .getEntity ();
94
- try (InputStream is = entity .getContent ()) {
95
- Set <String > roles = new TreeSet <>();
96
- for (RoleRepresentation role : JsonSerialization .readValue (is , TypedSetOfRoles .class )) {
97
- roles .add (role .getName ());
98
- }
99
- roles .add ("Public" );
100
- return new ArrayList <>(roles );
101
- }
102
- } catch (IOException ex ) {
103
- throw new RuntimeException (ex );
104
- } catch (Failure f ) {
105
- throw new RuntimeException ("Returned status code: " + f .getStatus (), f );
61
+ TypedSetOfRoles typedSetOfRoles = client .target (SERVER_URL + "/auth/admin/realms/issue-tracker/roles" ).request ().get (TypedSetOfRoles .class );
62
+
63
+ for (RoleRepresentation typedSetOfRole : typedSetOfRoles ) {
64
+ roles .add (typedSetOfRole .getName ());
106
65
}
107
- } finally {
108
- client . getConnectionManager (). shutdown ();
66
+ } catch ( WebApplicationException e ) {
67
+ //in case the response status code of the response returned by the server is not successful
109
68
}
69
+ return new ArrayList <>(roles );
110
70
}
111
71
112
72
}
0 commit comments