|
16 | 16 |
|
17 | 17 | package org.springframework.security.web.authentication.www;
|
18 | 18 |
|
19 |
| -import static org.assertj.core.api.Assertions.assertThat; |
20 |
| -import static org.mockito.Mockito.mock; |
21 |
| -import static org.mockito.Mockito.times; |
22 |
| -import static org.mockito.Mockito.verify; |
23 |
| - |
24 | 19 | import java.io.IOException;
|
25 | 20 | import java.util.Map;
|
26 |
| - |
27 | 21 | import javax.servlet.Filter;
|
28 | 22 | import javax.servlet.FilterChain;
|
29 | 23 | import javax.servlet.ServletException;
|
|
34 | 28 | import org.junit.After;
|
35 | 29 | import org.junit.Before;
|
36 | 30 | import org.junit.Test;
|
| 31 | + |
37 | 32 | import org.springframework.mock.web.MockHttpServletRequest;
|
38 | 33 | import org.springframework.mock.web.MockHttpServletResponse;
|
39 | 34 | import org.springframework.security.authentication.TestingAuthenticationToken;
|
|
47 | 42 | import org.springframework.security.core.userdetails.cache.NullUserCache;
|
48 | 43 | import org.springframework.util.StringUtils;
|
49 | 44 |
|
| 45 | +import static org.assertj.core.api.Assertions.assertThat; |
| 46 | +import static org.mockito.Mockito.mock; |
| 47 | +import static org.mockito.Mockito.times; |
| 48 | +import static org.mockito.Mockito.verify; |
| 49 | + |
50 | 50 | /**
|
51 | 51 | * Tests {@link DigestAuthenticationFilter}.
|
52 | 52 | *
|
@@ -110,8 +110,12 @@ private MockHttpServletResponse executeFilterInContainerSimulator(Filter filter,
|
110 | 110 | }
|
111 | 111 |
|
112 | 112 | private static String generateNonce(int validitySeconds) {
|
| 113 | + return generateNonce(validitySeconds, KEY); |
| 114 | + } |
| 115 | + |
| 116 | + private static String generateNonce(int validitySeconds, String key) { |
113 | 117 | long expiryTime = System.currentTimeMillis() + (validitySeconds * 1000);
|
114 |
| - String signatureValue = DigestUtils.md5Hex(expiryTime + ":" + KEY); |
| 118 | + String signatureValue = DigestUtils.md5Hex(expiryTime + ":" + key); |
115 | 119 | String nonceValue = expiryTime + ":" + signatureValue;
|
116 | 120 |
|
117 | 121 | return new String(Base64.encodeBase64(nonceValue.getBytes()));
|
@@ -172,6 +176,22 @@ public void testExpiredNonceReturnsForbiddenWithStaleHeader() throws Exception {
|
172 | 176 | assertThat(headerMap.get("stale")).isEqualTo("true");
|
173 | 177 | }
|
174 | 178 |
|
| 179 | + @Test |
| 180 | + public void doFilterWhenNonceHasBadKeyThenGeneratesError() throws Exception { |
| 181 | + String badNonce = generateNonce(60, "badkey"); |
| 182 | + String responseDigest = DigestAuthUtils.generateDigest(false, USERNAME, REALM, |
| 183 | + PASSWORD, "GET", REQUEST_URI, QOP, badNonce, NC, CNONCE); |
| 184 | + |
| 185 | + request.addHeader("Authorization", createAuthorizationHeader(USERNAME, REALM, |
| 186 | + badNonce, REQUEST_URI, responseDigest, QOP, NC, CNONCE)); |
| 187 | + |
| 188 | + MockHttpServletResponse response = |
| 189 | + executeFilterInContainerSimulator(filter, request, false); |
| 190 | + |
| 191 | + assertThat(response.getStatus()).isEqualTo(401); |
| 192 | + assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull(); |
| 193 | + } |
| 194 | + |
175 | 195 | @Test
|
176 | 196 | public void testFilterIgnoresRequestsContainingNoAuthorizationHeader()
|
177 | 197 | throws Exception {
|
|
0 commit comments