|
16 | 16 |
|
17 | 17 | package org.springframework.security.web.authentication.www;
|
18 | 18 |
|
19 |
| -import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.assertj.core.api.Assertions.*; |
20 | 20 | import static org.mockito.AdditionalMatchers.not;
|
21 | 21 | import static org.mockito.Mockito.*;
|
22 | 22 |
|
23 | 23 | import javax.servlet.FilterChain;
|
24 | 24 | import javax.servlet.ServletRequest;
|
25 | 25 | import javax.servlet.ServletResponse;
|
| 26 | +import javax.servlet.http.HttpServletResponse; |
26 | 27 |
|
27 | 28 | import org.apache.commons.codec.binary.Base64;
|
28 | 29 | import org.junit.After;
|
|
40 | 41 | import org.springframework.security.web.authentication.WebAuthenticationDetails;
|
41 | 42 | import org.springframework.web.util.WebUtils;
|
42 | 43 |
|
| 44 | +import java.nio.charset.StandardCharsets; |
| 45 | + |
43 | 46 | /**
|
44 | 47 | * Tests {@link BasicAuthenticationFilter}.
|
45 | 48 | *
|
@@ -320,4 +323,105 @@ public void skippedOnErrorDispatch() throws Exception {
|
320 | 323 |
|
321 | 324 | assertThat(response.getStatus()).isEqualTo(200);
|
322 | 325 | }
|
| 326 | + |
| 327 | + @Test |
| 328 | + public void doFilterWhenTokenAndFilterCharsetMatchDefaultThenAuthenticated() throws Exception { |
| 329 | + SecurityContextHolder.clearContext(); |
| 330 | + |
| 331 | + UsernamePasswordAuthenticationToken rodRequest = new UsernamePasswordAuthenticationToken("rod", "äöü"); |
| 332 | + rodRequest.setDetails(new WebAuthenticationDetails(new MockHttpServletRequest())); |
| 333 | + Authentication rod = new UsernamePasswordAuthenticationToken("rod", "äöü", AuthorityUtils.createAuthorityList("ROLE_1")); |
| 334 | + |
| 335 | + manager = mock(AuthenticationManager.class); |
| 336 | + when(manager.authenticate(rodRequest)).thenReturn(rod); |
| 337 | + when(manager.authenticate(not(eq(rodRequest)))).thenThrow(new BadCredentialsException("")); |
| 338 | + |
| 339 | + filter = new BasicAuthenticationFilter(manager, new BasicAuthenticationEntryPoint()); |
| 340 | + |
| 341 | + String token = "rod:äöü"; |
| 342 | + MockHttpServletRequest request = new MockHttpServletRequest(); |
| 343 | + request.addHeader("Authorization", "Basic " + new String(Base64.encodeBase64(token.getBytes(StandardCharsets.UTF_8)))); |
| 344 | + request.setServletPath("/some_file.html"); |
| 345 | + |
| 346 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 347 | + |
| 348 | + // Test |
| 349 | + assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull(); |
| 350 | + FilterChain chain = mock(FilterChain.class); |
| 351 | + |
| 352 | + filter.doFilter(request, response, chain); |
| 353 | + |
| 354 | + assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_OK); |
| 355 | + verify(chain).doFilter(any(ServletRequest.class), any(ServletResponse.class)); |
| 356 | + assertThat(SecurityContextHolder.getContext().getAuthentication().getName()).isEqualTo("rod"); |
| 357 | + assertThat(SecurityContextHolder.getContext().getAuthentication().getCredentials()).isEqualTo("äöü"); |
| 358 | + } |
| 359 | + |
| 360 | + @Test |
| 361 | + public void doFilterWhenTokenAndFilterCharsetMatchNonDefaultThenAuthenticated() throws Exception { |
| 362 | + SecurityContextHolder.clearContext(); |
| 363 | + |
| 364 | + UsernamePasswordAuthenticationToken rodRequest = new UsernamePasswordAuthenticationToken("rod", "äöü"); |
| 365 | + rodRequest.setDetails(new WebAuthenticationDetails(new MockHttpServletRequest())); |
| 366 | + Authentication rod = new UsernamePasswordAuthenticationToken("rod", "äöü", AuthorityUtils.createAuthorityList("ROLE_1")); |
| 367 | + |
| 368 | + manager = mock(AuthenticationManager.class); |
| 369 | + when(manager.authenticate(rodRequest)).thenReturn(rod); |
| 370 | + when(manager.authenticate(not(eq(rodRequest)))).thenThrow(new BadCredentialsException("")); |
| 371 | + |
| 372 | + filter = new BasicAuthenticationFilter(manager, new BasicAuthenticationEntryPoint()); |
| 373 | + filter.setCredentialsCharset("ISO-8859-1"); |
| 374 | + |
| 375 | + String token = "rod:äöü"; |
| 376 | + MockHttpServletRequest request = new MockHttpServletRequest(); |
| 377 | + request.addHeader("Authorization", "Basic " + new String(Base64.encodeBase64(token.getBytes(StandardCharsets.ISO_8859_1)))); |
| 378 | + request.setServletPath("/some_file.html"); |
| 379 | + |
| 380 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 381 | + |
| 382 | + // Test |
| 383 | + assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull(); |
| 384 | + FilterChain chain = mock(FilterChain.class); |
| 385 | + |
| 386 | + filter.doFilter(request, response, chain); |
| 387 | + |
| 388 | + assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_OK); |
| 389 | + verify(chain).doFilter(any(ServletRequest.class), any(ServletResponse.class)); |
| 390 | + assertThat(SecurityContextHolder.getContext().getAuthentication().getName()).isEqualTo("rod"); |
| 391 | + assertThat(SecurityContextHolder.getContext().getAuthentication().getCredentials()).isEqualTo("äöü"); |
| 392 | + } |
| 393 | + |
| 394 | + @Test |
| 395 | + public void doFilterWhenTokenAndFilterCharsetDoNotMatchThenUnauthorized() throws Exception { |
| 396 | + SecurityContextHolder.clearContext(); |
| 397 | + |
| 398 | + UsernamePasswordAuthenticationToken rodRequest = new UsernamePasswordAuthenticationToken("rod", "äöü"); |
| 399 | + rodRequest.setDetails(new WebAuthenticationDetails(new MockHttpServletRequest())); |
| 400 | + Authentication rod = new UsernamePasswordAuthenticationToken("rod", "äöü", AuthorityUtils.createAuthorityList("ROLE_1")); |
| 401 | + |
| 402 | + manager = mock(AuthenticationManager.class); |
| 403 | + when(manager.authenticate(rodRequest)).thenReturn(rod); |
| 404 | + when(manager.authenticate(not(eq(rodRequest)))).thenThrow(new BadCredentialsException("")); |
| 405 | + |
| 406 | + filter = new BasicAuthenticationFilter(manager, new BasicAuthenticationEntryPoint()); |
| 407 | + filter.setCredentialsCharset("ISO-8859-1"); |
| 408 | + |
| 409 | + String token = "rod:äöü"; |
| 410 | + MockHttpServletRequest request = new MockHttpServletRequest(); |
| 411 | + request.addHeader("Authorization", "Basic " + new String(Base64.encodeBase64(token.getBytes(StandardCharsets.UTF_8)))); |
| 412 | + request.setServletPath("/some_file.html"); |
| 413 | + |
| 414 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 415 | + |
| 416 | + // Test |
| 417 | + assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull(); |
| 418 | + FilterChain chain = mock(FilterChain.class); |
| 419 | + |
| 420 | + filter.doFilter(request, response, chain); |
| 421 | + |
| 422 | + assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_UNAUTHORIZED); |
| 423 | + verify(chain, never()).doFilter(any(ServletRequest.class), any(ServletResponse.class)); |
| 424 | + assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull(); |
| 425 | + } |
| 426 | + |
323 | 427 | }
|
0 commit comments