forked from geoserver/geoserver
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GEOS-11235] preauthentication filters - session reuse even after hav…
…ing logout .. or if no preauth headers are provided anymore. See https://osgeo-org.atlassian.net/browse/GEOS-11235 for more details. Without this change, we can end up with a persisting session across logout. Tests: adding utests specific to this change. Runtime tested into a docker composition.
- Loading branch information
Showing
2 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...st/java/org/geoserver/security/filter/GeoServerRequestHeaderAuthenticationFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* (c) 2023 Open Source Geospatial Foundation - all rights reserved | ||
* This code is licensed under the GPL 2.0 license, available at the root | ||
* application directory. | ||
*/ | ||
package org.geoserver.security.filter; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNull; | ||
|
||
import java.io.File; | ||
import javax.servlet.http.HttpServletResponse; | ||
import org.geoserver.config.GeoServerDataDirectory; | ||
import org.geoserver.security.GeoServerSecurityManager; | ||
import org.geoserver.security.config.PreAuthenticatedUserNameFilterConfig; | ||
import org.junit.Test; | ||
import org.springframework.mock.web.MockFilterChain; | ||
import org.springframework.mock.web.MockHttpServletRequest; | ||
import org.springframework.mock.web.MockHttpServletResponse; | ||
import org.springframework.security.core.context.SecurityContext; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.core.context.SecurityContextImpl; | ||
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken; | ||
|
||
public class GeoServerRequestHeaderAuthenticationFilterTest { | ||
|
||
@Test | ||
public void testAuthenticationViaPreAuthChanging() throws Exception { | ||
MockHttpServletRequest request = new MockHttpServletRequest(); | ||
HttpServletResponse response = new MockHttpServletResponse(); | ||
MockFilterChain filterChain = new MockFilterChain(); | ||
SecurityContext sc = new SecurityContextImpl(); | ||
sc.setAuthentication(new PreAuthenticatedAuthenticationToken("testadmin", null)); | ||
SecurityContextHolder.setContext(sc); | ||
GeoServerRequestHeaderAuthenticationFilter toTest = | ||
new GeoServerRequestHeaderAuthenticationFilter(); | ||
toTest.setPrincipalHeaderAttribute("sec-username"); | ||
request.addHeader("sec-username", "testuser"); | ||
toTest.setSecurityManager( | ||
new GeoServerSecurityManager(new GeoServerDataDirectory(new File("/tmp")))); | ||
toTest.setRoleSource( | ||
PreAuthenticatedUserNameFilterConfig.PreAuthenticatedUserNameRoleSource.Header); | ||
|
||
toTest.doFilter(request, response, filterChain); | ||
|
||
assertEquals( | ||
"testuser", | ||
SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString()); | ||
} | ||
|
||
@Test | ||
public void testAuthenticationViaPreAuthNoHeader() throws Exception { | ||
MockHttpServletRequest request = new MockHttpServletRequest(); | ||
HttpServletResponse response = new MockHttpServletResponse(); | ||
MockFilterChain filterChain = new MockFilterChain(); | ||
SecurityContext sc = new SecurityContextImpl(); | ||
sc.setAuthentication(new PreAuthenticatedAuthenticationToken("testadmin", null)); | ||
SecurityContextHolder.setContext(sc); | ||
GeoServerRequestHeaderAuthenticationFilter toTest = | ||
new GeoServerRequestHeaderAuthenticationFilter(); | ||
toTest.setPrincipalHeaderAttribute("sec-username"); | ||
toTest.setSecurityManager( | ||
new GeoServerSecurityManager(new GeoServerDataDirectory(new File("/tmp")))); | ||
toTest.setRoleSource( | ||
PreAuthenticatedUserNameFilterConfig.PreAuthenticatedUserNameRoleSource.Header); | ||
|
||
toTest.doFilter(request, response, filterChain); | ||
|
||
// The security context should have been cleared | ||
assertNull(SecurityContextHolder.getContext().getAuthentication()); | ||
} | ||
} |