SecurityContextHolder.getContext().setAuthentication(null);
// Test entry point
MockHttpServletRequest request= createRequest("/foo/bar");
MockHttpServletResponse response= new MockHttpServletResponse();
MockFilterChain chain = new MockFilterChain();
getProxy().doFilter(request, response, chain);
assertEquals(HttpServletResponse.SC_UNAUTHORIZED,response.getErrorCode());
String tmp = response.getHeader("WWW-Authenticate");
assertNotNull(tmp);
assert(tmp.indexOf(GeoServerSecurityManager.REALM) !=-1 );
assert(tmp.indexOf("Digest") !=-1 );
assertNull(SecurityContextHolder.getContext().getAuthentication());
// test successful login
request= createRequest("/foo/bar");
response= new MockHttpServletResponse();
chain = new MockFilterChain();
String headerValue=clientDigestString(tmp, testUserName, testPassword, request.getMethod());
request.addHeader("Authorization", headerValue);
getProxy().doFilter(request, response, chain);
assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
Authentication auth = getAuth(testFilterName2, testUserName,300,300);
assertNotNull(auth);
assertNull(SecurityContextHolder.getContext().getAuthentication());
checkForAuthenticatedRole(auth);
assertEquals(testUserName, ((UserDetails) auth.getPrincipal()).getUsername());
assertTrue(auth.getAuthorities().contains(new GeoServerRole(rootRole)));
assertTrue(auth.getAuthorities().contains(new GeoServerRole(derivedRole)));
// check wrong password
// request= createRequest("/foo/bar");
// response= new MockHttpServletResponse();
// chain = new MockFilterChain();
//
// headerValue=clientDigestString(tmp, testUserName, "wrongpass", request.getMethod());
// request.addHeader("Authorization", headerValue);
// getProxy().doFilter(request, response, chain);
// tmp = response.getHeader("WWW-Authenticate");
// assertNotNull(tmp);
// assert(tmp.indexOf(GeoServerSecurityManager.REALM) !=-1 );
// assert(tmp.indexOf("Digest") !=-1 );
// assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getErrorCode());
// auth = getAuth(testFilterName2, testUserName,300,300);
// assertNull(auth);
// assertNull(SecurityContextHolder.getContext().getAuthentication());
// check unknown user
request= createRequest("/foo/bar");
response= new MockHttpServletResponse();
chain = new MockFilterChain();
headerValue=clientDigestString(tmp, "unknown", testPassword, request.getMethod());
request.addHeader("Authorization", headerValue);
getProxy().doFilter(request, response, chain);
tmp = response.getHeader("WWW-Authenticate");
assertNotNull(tmp);
assert(tmp.indexOf(GeoServerSecurityManager.REALM) !=-1 );
assert(tmp.indexOf("Digest") !=-1 );
assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getErrorCode());
auth = getAuth(testFilterName2, "unknown",300,300);
assertNull(auth);
assertNull(SecurityContextHolder.getContext().getAuthentication());
// check root user
request= createRequest("/foo/bar");
response= new MockHttpServletResponse();
chain = new MockFilterChain();
headerValue=clientDigestString(tmp, GeoServerUser.ROOT_USERNAME, getMasterPassword(), request.getMethod());
request.addHeader("Authorization", headerValue);
getProxy().doFilter(request, response, chain);
assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
auth = getAuth(testFilterName2, GeoServerUser.ROOT_USERNAME,300,300);
assertNull(auth);
assertNull(SecurityContextHolder.getContext().getAuthentication());
// check root user with wrong password
request= createRequest("/foo/bar");
response= new MockHttpServletResponse();
chain = new MockFilterChain();
headerValue=clientDigestString(tmp, GeoServerUser.ROOT_USERNAME, "geoserver1", request.getMethod());
request.addHeader("Authorization", headerValue);
getProxy().doFilter(request, response, chain);
tmp = response.getHeader("WWW-Authenticate");
assertNotNull(tmp);
assert(tmp.indexOf(GeoServerSecurityManager.REALM) !=-1 );
assert(tmp.indexOf("Digest") !=-1 );
assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getErrorCode());
auth = getAuth(testFilterName2, GeoServerUser.ROOT_USERNAME,300,300);
assertNull(auth);
assertNull(SecurityContextHolder.getContext().getAuthentication());
// check disabled user, should not work becaus of cache
updateUser("ug1", testUserName, false);
request= createRequest("/foo/bar");
response= new MockHttpServletResponse();
chain = new MockFilterChain();
headerValue=clientDigestString(tmp, testUserName, testPassword, request.getMethod());
request.addHeader("Authorization", headerValue);
getProxy().doFilter(request, response, chain);
assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
auth = getAuth(testFilterName2, testUserName,300,300);
assertNotNull(auth);
assertNull(SecurityContextHolder.getContext().getAuthentication());
checkForAuthenticatedRole(auth);
assertEquals(testUserName, ((UserDetails) auth.getPrincipal()).getUsername());
assertTrue(auth.getAuthorities().contains(new GeoServerRole(rootRole)));
assertTrue(auth.getAuthorities().contains(new GeoServerRole(derivedRole)));
// clear cache, now disabling should work
getCache().removeAll();
request= createRequest("/foo/bar");
response= new MockHttpServletResponse();
chain = new MockFilterChain();
headerValue=clientDigestString(tmp, "unknown", testPassword, request.getMethod());
request.addHeader("Authorization", headerValue);
getProxy().doFilter(request, response, chain);
tmp = response.getHeader("WWW-Authenticate");
assertNotNull(tmp);
assert(tmp.indexOf(GeoServerSecurityManager.REALM) !=-1 );
assert(tmp.indexOf("Digest") !=-1 );
assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getErrorCode());
auth = getAuth(testFilterName2, testUserName,300,300);
assertNull(auth);
assertNull(SecurityContextHolder.getContext().getAuthentication());
updateUser("ug1", testUserName, true);
// Test anonymous
insertAnonymousFilter();
request= createRequest("/foo/bar");
response= new MockHttpServletResponse();
chain = new MockFilterChain();
getProxy().doFilter(request, response, chain);
assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
// Anonymous context is not stored in http session, no further testing
removeAnonymousFilter();
}