forked from wildfly/quickstart
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJwtAuthIT.java
More file actions
221 lines (191 loc) · 8.05 KB
/
JwtAuthIT.java
File metadata and controls
221 lines (191 loc) · 8.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* JBoss, Home of Professional Open Source.
*
* Copyright 2023 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.quickstarts.jaxrsjwt;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Supplier;
import jakarta.json.JsonObject;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.ClientRequestContext;
import jakarta.ws.rs.client.ClientRequestFilter;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.core.Form;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriBuilder;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
/**
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
*/
public class JwtAuthIT {
private static final String DEFAULT_SERVER_HOST = "http://localhost:8080";
private static Client adminClient;
private static Client customerClient;
private static Client noAuthClient;
@BeforeAll
public static void createClients() {
adminClient = createAdminClient();
customerClient = createCustomerClient();
noAuthClient = ClientBuilder.newClient();
}
@AfterAll
public static void closeClients() {
if (adminClient != null) adminClient.close();
if (customerClient != null) customerClient.close();
if (noAuthClient != null) noAuthClient.close();
}
@Test
public void adminProtected() {
final JsonObject json = get(adminClient, Response.Status.OK, resolveBaseUrl().path("protected"));
Assertions.assertEquals("Hello admin!", json.getString("result"));
}
@Test
public void adminPublic() {
final JsonObject json = get(adminClient, Response.Status.OK, resolveBaseUrl().path("public"));
Assertions.assertEquals("Hello admin!", json.getString("result"));
}
@Test
public void adminCustomer() {
get(adminClient, Response.Status.FORBIDDEN, resolveBaseUrl().path("customer"));
}
@Test
public void adminClaims() {
final JsonObject json = get(adminClient, Response.Status.OK, resolveBaseUrl().path("claims"));
Assertions.assertEquals("admin", json.getJsonArray("groups").getString(0));
}
@Test
public void customerProtected() {
get(customerClient, Response.Status.FORBIDDEN, resolveBaseUrl().path("protected"));
}
@Test
public void customerPublic() {
final JsonObject json = get(customerClient, Response.Status.OK, resolveBaseUrl().path("public"));
Assertions.assertEquals("Hello customer!", json.getString("result"));
}
@Test
public void customerCustomer() {
final JsonObject json = get(customerClient, Response.Status.OK, resolveBaseUrl().path("customer"));
Assertions.assertEquals("Hello customer!", json.getString("result"));
}
@Test
public void customerClaims() {
final JsonObject json = get(customerClient, Response.Status.OK, resolveBaseUrl().path("claims"));
Assertions.assertEquals("customer", json.getJsonArray("groups").getString(0));
}
@Test
public void noAuthProtected() {
get(noAuthClient, Response.Status.UNAUTHORIZED, resolveBaseUrl().path("protected"));
}
@Test
public void noAuthPublic() {
final JsonObject json = get(noAuthClient, Response.Status.OK, resolveBaseUrl().path("public"));
Assertions.assertEquals("Hello anonymous!", json.getString("result"));
}
@Test
public void noAuthCustomer() {
get(noAuthClient, Response.Status.UNAUTHORIZED, resolveBaseUrl().path("customer"));
}
@Test
public void noAuthClaims() {
get(noAuthClient, Response.Status.UNAUTHORIZED, resolveBaseUrl().path("claims"));
}
private static Client createAdminClient() {
return createClient("admin", "adminpw");
}
private static Client createCustomerClient() {
return createClient("customer", "customerpw");
}
private static Client createClient(final String username, final String password) {
if (username == null || password == null) {
return ClientBuilder.newClient();
}
return ClientBuilder.newBuilder()
.register(new BearerTokenAuthorizationFilter(username, password))
.build();
}
private static JsonObject get(final Client client, final Response.Status expectedStatus, final UriBuilder uriBuilder) {
try (
Response response = client.target(uriBuilder).request().get()
) {
Assertions.assertEquals(expectedStatus, response.getStatusInfo(), () ->
String.format("Expected status %s for resource %s got \"%d: %s\": %s", expectedStatus, uriBuilder.build(),
response.getStatus(), response.getStatusInfo(), response.readEntity(String.class)));
if (expectedStatus == Response.Status.OK) {
return response.readEntity(JsonObject.class);
}
}
return JsonObject.EMPTY_JSON_OBJECT;
}
private static UriBuilder resolveBaseUrl() {
final String baseUrl = resolveServerHost();
return UriBuilder.fromUri(baseUrl).path("/jaxrs-jwt/rest");
}
private static String resolveServerHost() {
final String result = System.getenv("SERVER_HOST");
return result != null ? result : System.getProperty("server.host", DEFAULT_SERVER_HOST);
}
/**
* This is an example filter and should not be used in production
*/
private static class BearerTokenAuthorizationFilter implements ClientRequestFilter {
private final Supplier<String> tokenSupplier;
private final Lock lock;
private volatile String cachedToken;
private BearerTokenAuthorizationFilter(final String username, final String password) {
lock = new ReentrantLock();
this.tokenSupplier = () -> {
final UriBuilder uriBuilder = resolveBaseUrl().path("token");
final Form form = new Form();
form.param("username", username);
form.param("password", password);
try (
Client client = ClientBuilder.newClient();
Response response = client
.target(uriBuilder)
.request()
.post(Entity.form(form))
) {
Assertions.assertEquals(Response.Status.OK, response.getStatusInfo(), () ->
String.format("Failed to get token: %d: %s", response.getStatus(), response.readEntity(String.class)));
final JsonObject token = response.readEntity(JsonObject.class);
Assertions.assertNotNull(token);
return token.getString("token");
}
};
}
@Override
public void filter(final ClientRequestContext requestContext) {
if (cachedToken == null) {
try {
lock.lock();
if (cachedToken == null) {
cachedToken = tokenSupplier.get();
}
} finally {
lock.unlock();
}
}
requestContext.getHeaders().putSingle(HttpHeaders.AUTHORIZATION, "Bearer " + cachedToken);
}
}
}