|
@@ -15,11 +15,20 @@
|
|
|
|
|
|
package org.springframework.security.web.authentication.www;
|
|
|
|
|
|
-import static org.junit.Assert.*;
|
|
|
-import static org.mockito.Mockito.*;
|
|
|
+import static org.fest.assertions.Assertions.*;
|
|
|
+
|
|
|
+import static org.junit.Assert.assertEquals;
|
|
|
+import static org.junit.Assert.assertFalse;
|
|
|
+import static org.junit.Assert.assertNotNull;
|
|
|
+import static org.junit.Assert.assertNull;
|
|
|
+import static org.junit.Assert.assertTrue;
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
+import static org.mockito.Mockito.times;
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
-import java.util.*;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
import javax.servlet.Filter;
|
|
|
import javax.servlet.FilterChain;
|
|
|
import javax.servlet.ServletException;
|
|
@@ -32,7 +41,9 @@ import org.junit.Before;
|
|
|
import org.junit.Test;
|
|
|
import org.springframework.mock.web.MockHttpServletRequest;
|
|
|
import org.springframework.mock.web.MockHttpServletResponse;
|
|
|
+import org.springframework.security.authentication.TestingAuthenticationToken;
|
|
|
import org.springframework.security.core.authority.AuthorityUtils;
|
|
|
+import org.springframework.security.core.context.SecurityContext;
|
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
import org.springframework.security.core.userdetails.User;
|
|
|
import org.springframework.security.core.userdetails.UserDetails;
|
|
@@ -41,7 +52,6 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
|
import org.springframework.security.core.userdetails.cache.NullUserCache;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* Tests {@link DigestAuthenticationFilter}.
|
|
|
*
|
|
@@ -49,7 +59,8 @@ import org.springframework.util.StringUtils;
|
|
|
* @author Luke Taylor
|
|
|
*/
|
|
|
public class DigestAuthenticationFilterTests {
|
|
|
- //~ Static fields/initializers =====================================================================================
|
|
|
+ // ~ Static fields/initializers
|
|
|
+ // =====================================================================================
|
|
|
|
|
|
private static final String NC = "00000002";
|
|
|
private static final String CNONCE = "c822c727a648aba7";
|
|
@@ -65,23 +76,26 @@ public class DigestAuthenticationFilterTests {
|
|
|
*/
|
|
|
private static final String NONCE = generateNonce(60);
|
|
|
|
|
|
- //~ Instance fields ================================================================================================
|
|
|
+ // ~ Instance fields
|
|
|
+ // ================================================================================================
|
|
|
|
|
|
- // private ApplicationContext ctx;
|
|
|
+ // private ApplicationContext ctx;
|
|
|
private DigestAuthenticationFilter filter;
|
|
|
private MockHttpServletRequest request;
|
|
|
|
|
|
+ // ~ Methods
|
|
|
+ // ========================================================================================================
|
|
|
|
|
|
- //~ Methods ========================================================================================================
|
|
|
-
|
|
|
- private String createAuthorizationHeader(String username, String realm, String nonce, String uri,
|
|
|
- String responseDigest, String qop, String nc, String cnonce) {
|
|
|
- return "Digest username=\"" + username + "\", realm=\"" + realm + "\", nonce=\"" + nonce + "\", uri=\"" + uri
|
|
|
- + "\", response=\"" + responseDigest + "\", qop=" + qop + ", nc=" + nc + ", cnonce=\"" + cnonce + "\"";
|
|
|
+ private String createAuthorizationHeader(String username, String realm, String nonce,
|
|
|
+ String uri, String responseDigest, String qop, String nc, String cnonce) {
|
|
|
+ return "Digest username=\"" + username + "\", realm=\"" + realm + "\", nonce=\""
|
|
|
+ + nonce + "\", uri=\"" + uri + "\", response=\"" + responseDigest
|
|
|
+ + "\", qop=" + qop + ", nc=" + nc + ", cnonce=\"" + cnonce + "\"";
|
|
|
}
|
|
|
|
|
|
- private MockHttpServletResponse executeFilterInContainerSimulator(Filter filter, final ServletRequest request,
|
|
|
- final boolean expectChainToProceed) throws ServletException, IOException {
|
|
|
+ private MockHttpServletResponse executeFilterInContainerSimulator(Filter filter,
|
|
|
+ final ServletRequest request, final boolean expectChainToProceed)
|
|
|
+ throws ServletException, IOException {
|
|
|
final MockHttpServletResponse response = new MockHttpServletResponse();
|
|
|
|
|
|
final FilterChain chain = mock(FilterChain.class);
|
|
@@ -111,8 +125,10 @@ public class DigestAuthenticationFilterTests {
|
|
|
|
|
|
// Create User Details Service
|
|
|
UserDetailsService uds = new UserDetailsService() {
|
|
|
- public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
|
|
- return new User("rod,ok", "koala", AuthorityUtils.createAuthorityList("ROLE_ONE","ROLE_TWO"));
|
|
|
+ public UserDetails loadUserByUsername(String username)
|
|
|
+ throws UsernameNotFoundException {
|
|
|
+ return new User("rod,ok", "koala", AuthorityUtils.createAuthorityList(
|
|
|
+ "ROLE_ONE", "ROLE_TWO"));
|
|
|
}
|
|
|
};
|
|
|
|
|
@@ -129,25 +145,28 @@ public class DigestAuthenticationFilterTests {
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void testExpiredNonceReturnsForbiddenWithStaleHeader()
|
|
|
- throws Exception {
|
|
|
+ public void testExpiredNonceReturnsForbiddenWithStaleHeader() throws Exception {
|
|
|
String nonce = generateNonce(0);
|
|
|
- String responseDigest = DigestAuthUtils.generateDigest(false, USERNAME, REALM, PASSWORD, "GET",
|
|
|
- REQUEST_URI, QOP, nonce, NC, CNONCE);
|
|
|
+ String responseDigest = DigestAuthUtils.generateDigest(false, USERNAME, REALM,
|
|
|
+ PASSWORD, "GET", REQUEST_URI, QOP, nonce, NC, CNONCE);
|
|
|
|
|
|
- request.addHeader("Authorization",
|
|
|
- createAuthorizationHeader(USERNAME, REALM, nonce, REQUEST_URI, responseDigest, QOP, NC, CNONCE));
|
|
|
+ request.addHeader(
|
|
|
+ "Authorization",
|
|
|
+ createAuthorizationHeader(USERNAME, REALM, nonce, REQUEST_URI,
|
|
|
+ responseDigest, QOP, NC, CNONCE));
|
|
|
|
|
|
Thread.sleep(1000); // ensures token expired
|
|
|
|
|
|
- MockHttpServletResponse response = executeFilterInContainerSimulator(filter, request, false);
|
|
|
+ MockHttpServletResponse response = executeFilterInContainerSimulator(filter,
|
|
|
+ request, false);
|
|
|
|
|
|
assertNull(SecurityContextHolder.getContext().getAuthentication());
|
|
|
assertEquals(401, response.getStatus());
|
|
|
|
|
|
String header = response.getHeader("WWW-Authenticate").toString().substring(7);
|
|
|
String[] headerEntries = StringUtils.commaDelimitedListToStringArray(header);
|
|
|
- Map<String,String> headerMap = DigestAuthUtils.splitEachArrayElementAndCreateMap(headerEntries, "=", "\"");
|
|
|
+ Map<String, String> headerMap = DigestAuthUtils
|
|
|
+ .splitEachArrayElementAndCreateMap(headerEntries, "=", "\"");
|
|
|
assertEquals("true", headerMap.get("stale"));
|
|
|
}
|
|
|
|
|
@@ -175,13 +194,14 @@ public class DigestAuthenticationFilterTests {
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void testInvalidDigestAuthorizationTokenGeneratesError()
|
|
|
- throws Exception {
|
|
|
+ public void testInvalidDigestAuthorizationTokenGeneratesError() throws Exception {
|
|
|
String token = "NOT_A_VALID_TOKEN_AS_MISSING_COLON";
|
|
|
|
|
|
- request.addHeader("Authorization", "Digest " + new String(Base64.encodeBase64(token.getBytes())));
|
|
|
+ request.addHeader("Authorization",
|
|
|
+ "Digest " + new String(Base64.encodeBase64(token.getBytes())));
|
|
|
|
|
|
- MockHttpServletResponse response = executeFilterInContainerSimulator(filter, request, false);
|
|
|
+ MockHttpServletResponse response = executeFilterInContainerSimulator(filter,
|
|
|
+ request, false);
|
|
|
|
|
|
assertEquals(401, response.getStatus());
|
|
|
assertNull(SecurityContextHolder.getContext().getAuthentication());
|
|
@@ -191,7 +211,8 @@ public class DigestAuthenticationFilterTests {
|
|
|
public void testMalformedHeaderReturnsForbidden() throws Exception {
|
|
|
request.addHeader("Authorization", "Digest scsdcsdc");
|
|
|
|
|
|
- MockHttpServletResponse response = executeFilterInContainerSimulator(filter, request, false);
|
|
|
+ MockHttpServletResponse response = executeFilterInContainerSimulator(filter,
|
|
|
+ request, false);
|
|
|
|
|
|
assertNull(SecurityContextHolder.getContext().getAuthentication());
|
|
|
assertEquals(401, response.getStatus());
|
|
@@ -201,28 +222,36 @@ public class DigestAuthenticationFilterTests {
|
|
|
public void testNonBase64EncodedNonceReturnsForbidden() throws Exception {
|
|
|
String nonce = "NOT_BASE_64_ENCODED";
|
|
|
|
|
|
- String responseDigest = DigestAuthUtils.generateDigest(false, USERNAME, REALM, PASSWORD, "GET",
|
|
|
- REQUEST_URI, QOP, nonce, NC, CNONCE);
|
|
|
+ String responseDigest = DigestAuthUtils.generateDigest(false, USERNAME, REALM,
|
|
|
+ PASSWORD, "GET", REQUEST_URI, QOP, nonce, NC, CNONCE);
|
|
|
|
|
|
- request.addHeader("Authorization",
|
|
|
- createAuthorizationHeader(USERNAME, REALM, nonce, REQUEST_URI, responseDigest, QOP, NC, CNONCE));
|
|
|
+ request.addHeader(
|
|
|
+ "Authorization",
|
|
|
+ createAuthorizationHeader(USERNAME, REALM, nonce, REQUEST_URI,
|
|
|
+ responseDigest, QOP, NC, CNONCE));
|
|
|
|
|
|
- MockHttpServletResponse response = executeFilterInContainerSimulator(filter, request, false);
|
|
|
+ MockHttpServletResponse response = executeFilterInContainerSimulator(filter,
|
|
|
+ request, false);
|
|
|
|
|
|
assertNull(SecurityContextHolder.getContext().getAuthentication());
|
|
|
assertEquals(401, response.getStatus());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void testNonceWithIncorrectSignatureForNumericFieldReturnsForbidden() throws Exception {
|
|
|
- String nonce = new String(Base64.encodeBase64("123456:incorrectStringPassword".getBytes()));
|
|
|
- String responseDigest = DigestAuthUtils.generateDigest(false, USERNAME, REALM, PASSWORD, "GET",
|
|
|
- REQUEST_URI, QOP, nonce, NC, CNONCE);
|
|
|
+ public void testNonceWithIncorrectSignatureForNumericFieldReturnsForbidden()
|
|
|
+ throws Exception {
|
|
|
+ String nonce = new String(Base64.encodeBase64("123456:incorrectStringPassword"
|
|
|
+ .getBytes()));
|
|
|
+ String responseDigest = DigestAuthUtils.generateDigest(false, USERNAME, REALM,
|
|
|
+ PASSWORD, "GET", REQUEST_URI, QOP, nonce, NC, CNONCE);
|
|
|
|
|
|
- request.addHeader("Authorization",
|
|
|
- createAuthorizationHeader(USERNAME, REALM, nonce, REQUEST_URI, responseDigest, QOP, NC, CNONCE));
|
|
|
+ request.addHeader(
|
|
|
+ "Authorization",
|
|
|
+ createAuthorizationHeader(USERNAME, REALM, nonce, REQUEST_URI,
|
|
|
+ responseDigest, QOP, NC, CNONCE));
|
|
|
|
|
|
- MockHttpServletResponse response = executeFilterInContainerSimulator(filter, request, false);
|
|
|
+ MockHttpServletResponse response = executeFilterInContainerSimulator(filter,
|
|
|
+ request, false);
|
|
|
|
|
|
assertNull(SecurityContextHolder.getContext().getAuthentication());
|
|
|
assertEquals(401, response.getStatus());
|
|
@@ -230,29 +259,38 @@ public class DigestAuthenticationFilterTests {
|
|
|
|
|
|
@Test
|
|
|
public void testNonceWithNonNumericFirstElementReturnsForbidden() throws Exception {
|
|
|
- String nonce = new String(Base64.encodeBase64("hello:ignoredSecondElement".getBytes()));
|
|
|
- String responseDigest = DigestAuthUtils.generateDigest(false, USERNAME, REALM, PASSWORD, "GET",
|
|
|
- REQUEST_URI, QOP, nonce, NC, CNONCE);
|
|
|
+ String nonce = new String(Base64.encodeBase64("hello:ignoredSecondElement"
|
|
|
+ .getBytes()));
|
|
|
+ String responseDigest = DigestAuthUtils.generateDigest(false, USERNAME, REALM,
|
|
|
+ PASSWORD, "GET", REQUEST_URI, QOP, nonce, NC, CNONCE);
|
|
|
|
|
|
- request.addHeader("Authorization",
|
|
|
- createAuthorizationHeader(USERNAME, REALM, nonce, REQUEST_URI, responseDigest, QOP, NC, CNONCE));
|
|
|
+ request.addHeader(
|
|
|
+ "Authorization",
|
|
|
+ createAuthorizationHeader(USERNAME, REALM, nonce, REQUEST_URI,
|
|
|
+ responseDigest, QOP, NC, CNONCE));
|
|
|
|
|
|
- MockHttpServletResponse response = executeFilterInContainerSimulator(filter, request, false);
|
|
|
+ MockHttpServletResponse response = executeFilterInContainerSimulator(filter,
|
|
|
+ request, false);
|
|
|
|
|
|
assertNull(SecurityContextHolder.getContext().getAuthentication());
|
|
|
assertEquals(401, response.getStatus());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void testNonceWithoutTwoColonSeparatedElementsReturnsForbidden() throws Exception {
|
|
|
- String nonce = new String(Base64.encodeBase64("a base 64 string without a colon".getBytes()));
|
|
|
- String responseDigest = DigestAuthUtils.generateDigest(false, USERNAME, REALM, PASSWORD, "GET",
|
|
|
- REQUEST_URI, QOP, nonce, NC, CNONCE);
|
|
|
+ public void testNonceWithoutTwoColonSeparatedElementsReturnsForbidden()
|
|
|
+ throws Exception {
|
|
|
+ String nonce = new String(Base64.encodeBase64("a base 64 string without a colon"
|
|
|
+ .getBytes()));
|
|
|
+ String responseDigest = DigestAuthUtils.generateDigest(false, USERNAME, REALM,
|
|
|
+ PASSWORD, "GET", REQUEST_URI, QOP, nonce, NC, CNONCE);
|
|
|
|
|
|
- request.addHeader("Authorization",
|
|
|
- createAuthorizationHeader(USERNAME, REALM, nonce, REQUEST_URI, responseDigest, QOP, NC, CNONCE));
|
|
|
+ request.addHeader(
|
|
|
+ "Authorization",
|
|
|
+ createAuthorizationHeader(USERNAME, REALM, nonce, REQUEST_URI,
|
|
|
+ responseDigest, QOP, NC, CNONCE));
|
|
|
|
|
|
- MockHttpServletResponse response = executeFilterInContainerSimulator(filter, request, false);
|
|
|
+ MockHttpServletResponse response = executeFilterInContainerSimulator(filter,
|
|
|
+ request, false);
|
|
|
|
|
|
assertNull(SecurityContextHolder.getContext().getAuthentication());
|
|
|
assertEquals(401, response.getStatus());
|
|
@@ -260,58 +298,67 @@ public class DigestAuthenticationFilterTests {
|
|
|
|
|
|
@Test
|
|
|
public void testNormalOperationWhenPasswordIsAlreadyEncoded() throws Exception {
|
|
|
- String encodedPassword = DigestAuthUtils.encodePasswordInA1Format(USERNAME, REALM, PASSWORD);
|
|
|
- String responseDigest = DigestAuthUtils.generateDigest(true, USERNAME, REALM, encodedPassword, "GET",
|
|
|
- REQUEST_URI, QOP, NONCE, NC, CNONCE);
|
|
|
+ String encodedPassword = DigestAuthUtils.encodePasswordInA1Format(USERNAME,
|
|
|
+ REALM, PASSWORD);
|
|
|
+ String responseDigest = DigestAuthUtils.generateDigest(true, USERNAME, REALM,
|
|
|
+ encodedPassword, "GET", REQUEST_URI, QOP, NONCE, NC, CNONCE);
|
|
|
|
|
|
- request.addHeader("Authorization",
|
|
|
- createAuthorizationHeader(USERNAME, REALM, NONCE, REQUEST_URI, responseDigest, QOP, NC, CNONCE));
|
|
|
+ request.addHeader(
|
|
|
+ "Authorization",
|
|
|
+ createAuthorizationHeader(USERNAME, REALM, NONCE, REQUEST_URI,
|
|
|
+ responseDigest, QOP, NC, CNONCE));
|
|
|
|
|
|
executeFilterInContainerSimulator(filter, request, true);
|
|
|
|
|
|
assertNotNull(SecurityContextHolder.getContext().getAuthentication());
|
|
|
- assertEquals(USERNAME,
|
|
|
- ((UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername());
|
|
|
+ assertEquals(USERNAME, ((UserDetails) SecurityContextHolder.getContext()
|
|
|
+ .getAuthentication().getPrincipal()).getUsername());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void testNormalOperationWhenPasswordNotAlreadyEncoded() throws Exception {
|
|
|
- String responseDigest = DigestAuthUtils.generateDigest(false, USERNAME, REALM, PASSWORD, "GET",
|
|
|
- REQUEST_URI, QOP, NONCE, NC, CNONCE);
|
|
|
+ String responseDigest = DigestAuthUtils.generateDigest(false, USERNAME, REALM,
|
|
|
+ PASSWORD, "GET", REQUEST_URI, QOP, NONCE, NC, CNONCE);
|
|
|
|
|
|
- request.addHeader("Authorization",
|
|
|
- createAuthorizationHeader(USERNAME, REALM, NONCE, REQUEST_URI, responseDigest, QOP, NC, CNONCE));
|
|
|
+ request.addHeader(
|
|
|
+ "Authorization",
|
|
|
+ createAuthorizationHeader(USERNAME, REALM, NONCE, REQUEST_URI,
|
|
|
+ responseDigest, QOP, NC, CNONCE));
|
|
|
|
|
|
executeFilterInContainerSimulator(filter, request, true);
|
|
|
|
|
|
assertNotNull(SecurityContextHolder.getContext().getAuthentication());
|
|
|
- assertEquals(USERNAME,
|
|
|
- ((UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername());
|
|
|
- assertFalse(SecurityContextHolder.getContext().getAuthentication().isAuthenticated());
|
|
|
+ assertEquals(USERNAME, ((UserDetails) SecurityContextHolder.getContext()
|
|
|
+ .getAuthentication().getPrincipal()).getUsername());
|
|
|
+ assertFalse(SecurityContextHolder.getContext().getAuthentication()
|
|
|
+ .isAuthenticated());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void testNormalOperationWhenPasswordNotAlreadyEncodedAndWithoutReAuthentication() throws Exception {
|
|
|
- String responseDigest = DigestAuthUtils.generateDigest(false, USERNAME, REALM, PASSWORD, "GET",
|
|
|
- REQUEST_URI, QOP, NONCE, NC, CNONCE);
|
|
|
+ public void testNormalOperationWhenPasswordNotAlreadyEncodedAndWithoutReAuthentication()
|
|
|
+ throws Exception {
|
|
|
+ String responseDigest = DigestAuthUtils.generateDigest(false, USERNAME, REALM,
|
|
|
+ PASSWORD, "GET", REQUEST_URI, QOP, NONCE, NC, CNONCE);
|
|
|
|
|
|
- request.addHeader("Authorization",
|
|
|
- createAuthorizationHeader(USERNAME, REALM, NONCE, REQUEST_URI, responseDigest, QOP, NC, CNONCE));
|
|
|
+ request.addHeader(
|
|
|
+ "Authorization",
|
|
|
+ createAuthorizationHeader(USERNAME, REALM, NONCE, REQUEST_URI,
|
|
|
+ responseDigest, QOP, NC, CNONCE));
|
|
|
|
|
|
filter.setCreateAuthenticatedToken(true);
|
|
|
executeFilterInContainerSimulator(filter, request, true);
|
|
|
|
|
|
assertNotNull(SecurityContextHolder.getContext().getAuthentication());
|
|
|
- assertEquals(USERNAME,
|
|
|
- ((UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername());
|
|
|
- assertTrue(SecurityContextHolder.getContext().getAuthentication().isAuthenticated());
|
|
|
- assertEquals(AuthorityUtils.createAuthorityList("ROLE_ONE","ROLE_TWO"),
|
|
|
+ assertEquals(USERNAME, ((UserDetails) SecurityContextHolder.getContext()
|
|
|
+ .getAuthentication().getPrincipal()).getUsername());
|
|
|
+ assertTrue(SecurityContextHolder.getContext().getAuthentication()
|
|
|
+ .isAuthenticated());
|
|
|
+ assertEquals(AuthorityUtils.createAuthorityList("ROLE_ONE", "ROLE_TWO"),
|
|
|
SecurityContextHolder.getContext().getAuthentication().getAuthorities());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void otherAuthorizationSchemeIsIgnored()
|
|
|
- throws Exception {
|
|
|
+ public void otherAuthorizationSchemeIsIgnored() throws Exception {
|
|
|
request.addHeader("Authorization", "SOME_OTHER_AUTHENTICATION_SCHEME");
|
|
|
|
|
|
executeFilterInContainerSimulator(filter, request, true);
|
|
@@ -319,14 +366,14 @@ public class DigestAuthenticationFilterTests {
|
|
|
assertNull(SecurityContextHolder.getContext().getAuthentication());
|
|
|
}
|
|
|
|
|
|
- @Test(expected=IllegalArgumentException.class)
|
|
|
+ @Test(expected = IllegalArgumentException.class)
|
|
|
public void startupDetectsMissingAuthenticationEntryPoint() throws Exception {
|
|
|
DigestAuthenticationFilter filter = new DigestAuthenticationFilter();
|
|
|
filter.setUserDetailsService(mock(UserDetailsService.class));
|
|
|
filter.afterPropertiesSet();
|
|
|
}
|
|
|
|
|
|
- @Test(expected=IllegalArgumentException.class)
|
|
|
+ @Test(expected = IllegalArgumentException.class)
|
|
|
public void startupDetectsMissingUserDetailsService() throws Exception {
|
|
|
DigestAuthenticationFilter filter = new DigestAuthenticationFilter();
|
|
|
filter.setAuthenticationEntryPoint(new DigestAuthenticationEntryPoint());
|
|
@@ -334,26 +381,32 @@ public class DigestAuthenticationFilterTests {
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void successfulLoginThenFailedLoginResultsInSessionLosingToken() throws Exception {
|
|
|
- String responseDigest = DigestAuthUtils.generateDigest(false, USERNAME, REALM, PASSWORD, "GET",
|
|
|
- REQUEST_URI, QOP, NONCE, NC, CNONCE);
|
|
|
+ public void successfulLoginThenFailedLoginResultsInSessionLosingToken()
|
|
|
+ throws Exception {
|
|
|
+ String responseDigest = DigestAuthUtils.generateDigest(false, USERNAME, REALM,
|
|
|
+ PASSWORD, "GET", REQUEST_URI, QOP, NONCE, NC, CNONCE);
|
|
|
|
|
|
- request.addHeader("Authorization",
|
|
|
- createAuthorizationHeader(USERNAME, REALM, NONCE, REQUEST_URI, responseDigest, QOP, NC, CNONCE));
|
|
|
+ request.addHeader(
|
|
|
+ "Authorization",
|
|
|
+ createAuthorizationHeader(USERNAME, REALM, NONCE, REQUEST_URI,
|
|
|
+ responseDigest, QOP, NC, CNONCE));
|
|
|
|
|
|
executeFilterInContainerSimulator(filter, request, true);
|
|
|
|
|
|
assertNotNull(SecurityContextHolder.getContext().getAuthentication());
|
|
|
|
|
|
// Now retry, giving an invalid nonce
|
|
|
- responseDigest = DigestAuthUtils.generateDigest(false, USERNAME, REALM, "WRONG_PASSWORD", "GET",
|
|
|
- REQUEST_URI, QOP, NONCE, NC, CNONCE);
|
|
|
+ responseDigest = DigestAuthUtils.generateDigest(false, USERNAME, REALM,
|
|
|
+ "WRONG_PASSWORD", "GET", REQUEST_URI, QOP, NONCE, NC, CNONCE);
|
|
|
|
|
|
request = new MockHttpServletRequest();
|
|
|
- request.addHeader("Authorization",
|
|
|
- createAuthorizationHeader(USERNAME, REALM, NONCE, REQUEST_URI, responseDigest, QOP, NC, CNONCE));
|
|
|
+ request.addHeader(
|
|
|
+ "Authorization",
|
|
|
+ createAuthorizationHeader(USERNAME, REALM, NONCE, REQUEST_URI,
|
|
|
+ responseDigest, QOP, NC, CNONCE));
|
|
|
|
|
|
- MockHttpServletResponse response = executeFilterInContainerSimulator(filter, request, false);
|
|
|
+ MockHttpServletResponse response = executeFilterInContainerSimulator(filter,
|
|
|
+ request, false);
|
|
|
|
|
|
// Check we lost our previous authentication
|
|
|
assertNull(SecurityContextHolder.getContext().getAuthentication());
|
|
@@ -364,13 +417,16 @@ public class DigestAuthenticationFilterTests {
|
|
|
public void wrongCnonceBasedOnDigestReturnsForbidden() throws Exception {
|
|
|
String cnonce = "NOT_SAME_AS_USED_FOR_DIGEST_COMPUTATION";
|
|
|
|
|
|
- String responseDigest = DigestAuthUtils.generateDigest(false, USERNAME, REALM, PASSWORD, "GET",
|
|
|
- REQUEST_URI, QOP, NONCE, NC, "DIFFERENT_CNONCE");
|
|
|
+ String responseDigest = DigestAuthUtils.generateDigest(false, USERNAME, REALM,
|
|
|
+ PASSWORD, "GET", REQUEST_URI, QOP, NONCE, NC, "DIFFERENT_CNONCE");
|
|
|
|
|
|
- request.addHeader("Authorization",
|
|
|
- createAuthorizationHeader(USERNAME, REALM, NONCE, REQUEST_URI, responseDigest, QOP, NC, cnonce));
|
|
|
+ request.addHeader(
|
|
|
+ "Authorization",
|
|
|
+ createAuthorizationHeader(USERNAME, REALM, NONCE, REQUEST_URI,
|
|
|
+ responseDigest, QOP, NC, cnonce));
|
|
|
|
|
|
- MockHttpServletResponse response = executeFilterInContainerSimulator(filter, request, false);
|
|
|
+ MockHttpServletResponse response = executeFilterInContainerSimulator(filter,
|
|
|
+ request, false);
|
|
|
|
|
|
assertNull(SecurityContextHolder.getContext().getAuthentication());
|
|
|
assertEquals(401, response.getStatus());
|
|
@@ -379,13 +435,16 @@ public class DigestAuthenticationFilterTests {
|
|
|
@Test
|
|
|
public void wrongDigestReturnsForbidden() throws Exception {
|
|
|
String password = "WRONG_PASSWORD";
|
|
|
- String responseDigest = DigestAuthUtils.generateDigest(false, USERNAME, REALM, password, "GET",
|
|
|
- REQUEST_URI, QOP, NONCE, NC, CNONCE);
|
|
|
+ String responseDigest = DigestAuthUtils.generateDigest(false, USERNAME, REALM,
|
|
|
+ password, "GET", REQUEST_URI, QOP, NONCE, NC, CNONCE);
|
|
|
|
|
|
- request.addHeader("Authorization",
|
|
|
- createAuthorizationHeader(USERNAME, REALM, NONCE, REQUEST_URI, responseDigest, QOP, NC, CNONCE));
|
|
|
+ request.addHeader(
|
|
|
+ "Authorization",
|
|
|
+ createAuthorizationHeader(USERNAME, REALM, NONCE, REQUEST_URI,
|
|
|
+ responseDigest, QOP, NC, CNONCE));
|
|
|
|
|
|
- MockHttpServletResponse response = executeFilterInContainerSimulator(filter, request, false);
|
|
|
+ MockHttpServletResponse response = executeFilterInContainerSimulator(filter,
|
|
|
+ request, false);
|
|
|
|
|
|
assertNull(SecurityContextHolder.getContext().getAuthentication());
|
|
|
assertEquals(401, response.getStatus());
|
|
@@ -394,13 +453,16 @@ public class DigestAuthenticationFilterTests {
|
|
|
@Test
|
|
|
public void wrongRealmReturnsForbidden() throws Exception {
|
|
|
String realm = "WRONG_REALM";
|
|
|
- String responseDigest = DigestAuthUtils.generateDigest(false, USERNAME, realm, PASSWORD, "GET",
|
|
|
- REQUEST_URI, QOP, NONCE, NC, CNONCE);
|
|
|
+ String responseDigest = DigestAuthUtils.generateDigest(false, USERNAME, realm,
|
|
|
+ PASSWORD, "GET", REQUEST_URI, QOP, NONCE, NC, CNONCE);
|
|
|
|
|
|
- request.addHeader("Authorization",
|
|
|
- createAuthorizationHeader(USERNAME, realm, NONCE, REQUEST_URI, responseDigest, QOP, NC, CNONCE));
|
|
|
+ request.addHeader(
|
|
|
+ "Authorization",
|
|
|
+ createAuthorizationHeader(USERNAME, realm, NONCE, REQUEST_URI,
|
|
|
+ responseDigest, QOP, NC, CNONCE));
|
|
|
|
|
|
- MockHttpServletResponse response = executeFilterInContainerSimulator(filter, request, false);
|
|
|
+ MockHttpServletResponse response = executeFilterInContainerSimulator(filter,
|
|
|
+ request, false);
|
|
|
|
|
|
assertNull(SecurityContextHolder.getContext().getAuthentication());
|
|
|
assertEquals(401, response.getStatus());
|
|
@@ -408,15 +470,41 @@ public class DigestAuthenticationFilterTests {
|
|
|
|
|
|
@Test
|
|
|
public void wrongUsernameReturnsForbidden() throws Exception {
|
|
|
- String responseDigest = DigestAuthUtils.generateDigest(false, "NOT_A_KNOWN_USER", REALM, PASSWORD,
|
|
|
- "GET", REQUEST_URI, QOP, NONCE, NC, CNONCE);
|
|
|
+ String responseDigest = DigestAuthUtils.generateDigest(false, "NOT_A_KNOWN_USER",
|
|
|
+ REALM, PASSWORD, "GET", REQUEST_URI, QOP, NONCE, NC, CNONCE);
|
|
|
|
|
|
- request.addHeader("Authorization",
|
|
|
- createAuthorizationHeader(USERNAME, REALM, NONCE, REQUEST_URI, responseDigest, QOP, NC, CNONCE));
|
|
|
+ request.addHeader(
|
|
|
+ "Authorization",
|
|
|
+ createAuthorizationHeader(USERNAME, REALM, NONCE, REQUEST_URI,
|
|
|
+ responseDigest, QOP, NC, CNONCE));
|
|
|
|
|
|
- MockHttpServletResponse response = executeFilterInContainerSimulator(filter, request, false);
|
|
|
+ MockHttpServletResponse response = executeFilterInContainerSimulator(filter,
|
|
|
+ request, false);
|
|
|
|
|
|
assertNull(SecurityContextHolder.getContext().getAuthentication());
|
|
|
assertEquals(401, response.getStatus());
|
|
|
}
|
|
|
+
|
|
|
+ // SEC-3108
|
|
|
+ @Test
|
|
|
+ public void authenticationCreatesEmptyContext() throws Exception {
|
|
|
+ SecurityContext existingContext = SecurityContextHolder.createEmptyContext();
|
|
|
+ TestingAuthenticationToken existingAuthentication = new TestingAuthenticationToken("existingauthenitcated", "pass", "ROLE_USER");
|
|
|
+ existingContext.setAuthentication(existingAuthentication);
|
|
|
+
|
|
|
+ SecurityContextHolder.setContext(existingContext);
|
|
|
+
|
|
|
+ String responseDigest = DigestAuthUtils.generateDigest(false, USERNAME, REALM,
|
|
|
+ PASSWORD, "GET", REQUEST_URI, QOP, NONCE, NC, CNONCE);
|
|
|
+
|
|
|
+ request.addHeader(
|
|
|
+ "Authorization",
|
|
|
+ createAuthorizationHeader(USERNAME, REALM, NONCE, REQUEST_URI,
|
|
|
+ responseDigest, QOP, NC, CNONCE));
|
|
|
+
|
|
|
+ filter.setCreateAuthenticatedToken(true);
|
|
|
+ executeFilterInContainerSimulator(filter, request, true);
|
|
|
+
|
|
|
+ assertThat(existingAuthentication).isSameAs(existingContext.getAuthentication());
|
|
|
+ }
|
|
|
}
|