-
Notifications
You must be signed in to change notification settings - Fork 888
/
BasicAuthTest.java
322 lines (288 loc) · 14.8 KB
/
BasicAuthTest.java
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package org.jboss.resteasy.test.security;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClients;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.as.arquillian.api.ServerSetup;
import org.jboss.resteasy.category.NotForForwardCompatibility;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine;
import org.jboss.resteasy.setup.AbstractUsersRolesSecurityDomainSetup;
import org.jboss.resteasy.test.security.resource.BasicAuthRequestFilter;
import org.jboss.resteasy.test.security.resource.BasicAuthBaseProxy;
import org.jboss.resteasy.test.security.resource.BasicAuthBaseResource;
import org.jboss.resteasy.test.security.resource.BasicAuthBaseResourceAnybody;
import org.jboss.resteasy.test.security.resource.BasicAuthBaseResourceMoreSecured;
import org.jboss.resteasy.util.HttpResponseCodes;
import org.jboss.resteasy.utils.PortProviderUtil;
import org.jboss.resteasy.utils.TestUtil;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import javax.ws.rs.NotAuthorizedException;
import javax.ws.rs.core.Response;
import java.io.File;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Hashtable;
/**
* @tpSubChapter Security
* @tpChapter Integration tests
* @tpTestCaseDetails Basic test for RESTEasy authentication.
* @tpSince RESTEasy 3.0.16
*/
@ServerSetup({BasicAuthTest.SecurityDomainSetup.class})
@RunWith(Arquillian.class)
@RunAsClient
public class BasicAuthTest {
private static final String WRONG_RESPONSE = "Wrong response content.";
private static final String ACCESS_FORBIDDEN_MESSAGE = "Access forbidden: role not allowed";
private static ResteasyClient authorizedClient;
private static ResteasyClient unauthorizedClient;
private static ResteasyClient noAutorizationClient;
// Following clients are used in tests for ClientRequestFilter
private static ResteasyClient authorizedClientUsingRequestFilter;
private static ResteasyClient unauthorizedClientUsingRequestFilter;
private static ResteasyClient unauthorizedClientUsingRequestFilterWithWrongPassword;
@BeforeClass
public static void init() {
// authorizedClient
{
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("bill", "password1");
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(AuthScope.ANY), credentials);
CloseableHttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build();
ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(client);
authorizedClient = new ResteasyClientBuilder().httpEngine(engine).build();
}
// unauthorizedClient
{
UsernamePasswordCredentials credentials_other = new UsernamePasswordCredentials("ordinaryUser", "password2");
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(AuthScope.ANY), credentials_other);
CloseableHttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build();
ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(client);
unauthorizedClient = new ResteasyClientBuilder().httpEngine(engine).build();
}
// noAuthorizationClient
noAutorizationClient = new ResteasyClientBuilder().build();
// authorizedClient with ClientRequestFilter
{
authorizedClientUsingRequestFilter = new ResteasyClientBuilder().register(new BasicAuthRequestFilter("bill", "password1")).build();
}
// unauthorizedClient with ClientRequestFilter - unauthorized user
{
unauthorizedClientUsingRequestFilter = new ResteasyClientBuilder().register(new BasicAuthRequestFilter("ordinaryUser", "password2")).build();
}
// unauthorizedClient with ClientRequestFilter - wrong password
{
unauthorizedClientUsingRequestFilterWithWrongPassword = new ResteasyClientBuilder().register(new BasicAuthRequestFilter("bill", "password2")).build();
}
}
@AfterClass
public static void after() throws Exception {
authorizedClient.close();
unauthorizedClient.close();
noAutorizationClient.close();
authorizedClientUsingRequestFilter.close();
unauthorizedClientUsingRequestFilter.close();
unauthorizedClientUsingRequestFilterWithWrongPassword.close();
}
@Deployment
public static Archive<?> deployLocatingResource() {
WebArchive war = TestUtil.prepareArchive(BasicAuthTest.class.getSimpleName());
Hashtable<String, String> contextParams = new Hashtable<String, String>();
contextParams.put("resteasy.role.based.security", "true");
war.addClass(BasicAuthBaseProxy.class)
.addAsWebInfResource(BasicAuthTest.class.getPackage(), "jboss-web.xml", "/jboss-web.xml")
.addAsWebInfResource(BasicAuthTest.class.getPackage(), "web.xml", "/web.xml");
return TestUtil.finishContainerPrepare(war, contextParams, BasicAuthBaseResource.class,
BasicAuthBaseResourceMoreSecured.class, BasicAuthBaseResourceAnybody.class);
}
private String generateURL(String path) {
return PortProviderUtil.generateURL(path, BasicAuthTest.class.getSimpleName());
}
/**
* @tpTestDetails Basic ProxyFactory test. Correct credentials are used.
* @tpSince RESTEasy 3.0.16
*/
@Test
public void testProxy() throws Exception {
BasicAuthBaseProxy proxy = authorizedClient.target(generateURL("/")).proxyBuilder(BasicAuthBaseProxy.class).build();
Assert.assertEquals(WRONG_RESPONSE, proxy.get(), "hello");
Assert.assertEquals(WRONG_RESPONSE, proxy.getAuthorized(), "authorized");
}
/**
* @tpTestDetails Basic ProxyFactory test. No credentials are used.
* @tpSince RESTEasy 3.0.16
*/
@Test
public void testProxyFailure() throws Exception {
BasicAuthBaseProxy proxy = noAutorizationClient.target(generateURL("/")).proxyBuilder(BasicAuthBaseProxy.class).build();
try {
proxy.getFailure();
Assert.fail();
} catch (NotAuthorizedException e) {
Assert.assertEquals(HttpResponseCodes.SC_UNAUTHORIZED, e.getResponse().getStatus());
}
}
/**
* @tpTestDetails Test secured resource with correct and incorrect credentials.
* @tpSince RESTEasy 3.0.16
*/
@Test
public void testSecurity() throws Exception {
// authorized client
{
Response response = authorizedClient.target(generateURL("/secured")).request().get();
Assert.assertEquals(HttpResponseCodes.SC_OK, response.getStatus());
Assert.assertEquals(WRONG_RESPONSE, "hello", response.readEntity(String.class));
}
{
Response response = authorizedClient.target(generateURL("/secured/authorized")).request().get();
Assert.assertEquals(HttpResponseCodes.SC_OK, response.getStatus());
Assert.assertEquals(WRONG_RESPONSE, "authorized", response.readEntity(String.class));
}
{
Response response = authorizedClient.target(generateURL("/secured/deny")).request().get();
Assert.assertEquals(HttpResponseCodes.SC_FORBIDDEN, response.getStatus());
Assert.assertEquals(WRONG_RESPONSE, ACCESS_FORBIDDEN_MESSAGE, response.readEntity(String.class));
}
{
Response response = authorizedClient.target(generateURL("/secured3/authorized")).request().get();
Assert.assertEquals(HttpResponseCodes.SC_OK, response.getStatus());
Assert.assertEquals(WRONG_RESPONSE, "authorized", response.readEntity(String.class));
}
// unauthorized client
{
Response response = unauthorizedClient.target(generateURL("/secured3/authorized")).request().get();
Assert.assertEquals(HttpResponseCodes.SC_FORBIDDEN, response.getStatus());
Assert.assertEquals(WRONG_RESPONSE, ACCESS_FORBIDDEN_MESSAGE, response.readEntity(String.class));
}
{
Response response = unauthorizedClient.target(generateURL("/secured3/anybody")).request().get();
Assert.assertEquals(HttpResponseCodes.SC_OK, response.getStatus());
response.close();
}
}
/**
* @tpTestDetails Regression test for RESTEASY-579
* @tpSince RESTEasy 3.0.16
*/
@Test
public void test579() throws Exception {
Response response = authorizedClient.target(generateURL("/secured2")).request().get();
Assert.assertEquals(HttpResponseCodes.SC_NOT_FOUND, response.getStatus());
response.close();
}
/**
* @tpTestDetails Check failures for secured resource.
* @tpSince RESTEasy 3.0.16
*/
@Test
public void testSecurityFailure() throws Exception {
{
Response response = noAutorizationClient.target(generateURL("/secured")).request().get();
Assert.assertEquals(HttpResponseCodes.SC_UNAUTHORIZED, response.getStatus());
response.close();
}
{
Response response = authorizedClient.target(generateURL("/secured/authorized")).request().get();
Assert.assertEquals(HttpResponseCodes.SC_OK, response.getStatus());
Assert.assertEquals(WRONG_RESPONSE, "authorized", response.readEntity(String.class));
}
{
Response response = unauthorizedClient.target(generateURL("/secured/authorized")).request().get();
Assert.assertEquals(HttpResponseCodes.SC_FORBIDDEN, response.getStatus());
Assert.assertEquals(ACCESS_FORBIDDEN_MESSAGE, response.readEntity(String.class));
}
}
/**
* @tpTestDetails Regression test for JBEAP-1589, RESTEASY-1249
* @tpSince RESTEasy 3.0.16
*/
@Test
public void testAccesForbiddenMessage() throws Exception {
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("bill", "password1");
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(AuthScope.ANY), credentials);
CloseableHttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build();
ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(client);
ResteasyClient authorizedClient = new ResteasyClientBuilder().httpEngine(engine).build();
Response response = authorizedClient.target(generateURL("/secured/deny")).request().get();
Assert.assertEquals(HttpResponseCodes.SC_FORBIDDEN, response.getStatus());
Assert.assertEquals(ACCESS_FORBIDDEN_MESSAGE, response.readEntity(String.class));
authorizedClient.close();
}
/**
* @tpTestDetails Test Content-type when forbidden exception is raised, RESTEASY-1563
* @tpSince RESTEasy 3.1.1
*/
@Test
@Category(NotForForwardCompatibility.class)
public void testContentTypeWithForbiddenMessage() {
Response response = unauthorizedClient.target(generateURL("/secured/denyWithContentType")).request().get();
Assert.assertEquals(HttpResponseCodes.SC_FORBIDDEN, response.getStatus());
Assert.assertEquals("Incorrect Content-type header", "text/html;charset=UTF-8", response.getHeaderString("Content-type"));
Assert.assertEquals("Missing forbidden message in the response", ACCESS_FORBIDDEN_MESSAGE, response.readEntity(String.class));
}
/**
* @tpTestDetails Test Content-type when unauthorized exception is raised
* @tpSince RESTEasy 3.1.1
*/
@Test
public void testContentTypeWithUnauthorizedMessage() {
Response response = noAutorizationClient.target(generateURL("/secured/denyWithContentType")).request().get();
Assert.assertEquals(HttpResponseCodes.SC_UNAUTHORIZED, response.getStatus());
Assert.assertEquals("Incorrect Content-type header", "text/html;charset=UTF-8", response.getHeaderString("Content-type"));
}
/**
* @tpTestDetails Test secured resource with correct credentials. Authentication is done using BasicAuthRequestFilter.
* @tpSince RESTEasy 3.7.0
*/
@Test
public void testWithClientRequestFilterAuthorizedUser() {
Response response = authorizedClientUsingRequestFilter.target(generateURL("/secured/authorized")).request().get();
Assert.assertEquals(HttpResponseCodes.SC_OK, response.getStatus());
Assert.assertEquals(WRONG_RESPONSE, "authorized", response.readEntity(String.class));
}
/**
* @tpTestDetails Test secured resource with incorrect credentials. Authentication is done using BasicAuthRequestFilter.
* @tpSince RESTEasy 3.7.0
*/
@Test
public void testWithClientRequestFilterWrongPassword(){
Response response = unauthorizedClientUsingRequestFilterWithWrongPassword.target(generateURL("/secured/authorized")).request().get();
Assert.assertEquals(HttpResponseCodes.SC_UNAUTHORIZED, response.getStatus());
}
/**
* @tpTestDetails Test secured resource with correct credentials of user that is not authorized to the resource. Authentication is done using BasicAuthRequestFilter.
* @tpSince RESTEasy 3.7.0
*/
@Test
public void testWithClientRequestFilterUnauthorizedUser() {
Response response = unauthorizedClientUsingRequestFilter.target(generateURL("/secured/authorized")).request().get();
Assert.assertEquals(HttpResponseCodes.SC_FORBIDDEN, response.getStatus());
Assert.assertEquals(WRONG_RESPONSE, ACCESS_FORBIDDEN_MESSAGE, response.readEntity(String.class));
}
static class SecurityDomainSetup extends AbstractUsersRolesSecurityDomainSetup {
@Override
public void setConfigurationPath() throws URISyntaxException {
Path filepath= Paths.get(BasicAuthTest.class.getResource("users.properties").toURI());
Path parent = filepath.getParent();
createPropertiesFiles(new File(parent.toUri()));
}
}
}