Authentication auth = ctx.getAuthentication();
assertNotNull(auth);
assertNull(SecurityContextHolder.getContext().getAuthentication());
checkForAuthenticatedRole(auth);
assertEquals(username, auth.getPrincipal());
assertTrue(auth.getAuthorities().contains(new GeoServerRole(rootRole)));
assertTrue(auth.getAuthorities().contains(new GeoServerRole(derivedRole)));
assertNotNull(GeoServerCasAuthenticationFilter.getHandler().getSessionMappingStorage()
.removeSessionByMappingId(ticket));
helper.ssoLogout();
// check unknown user
username = "unknown";
password = username;
helper = new CasFormAuthenticationHelper(casServerURLPrefix, username, password);
helper.ssoLogin();
request = createRequest("/foo/bar");
response = new MockHttpServletResponse();
chain = new MockFilterChain();
ticket =loginUsingTicket(helper, request, response, chain);
assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
assertFalse(response.wasRedirectSent());
ctx = (SecurityContext) request.getSession(true).getAttribute(
HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
assertNotNull(ctx);
auth = ctx.getAuthentication();
assertNotNull(auth);
assertNull(SecurityContextHolder.getContext().getAuthentication());
checkForAuthenticatedRole(ctx.getAuthentication());
assertEquals(username, auth.getPrincipal());
assertEquals(1, auth.getAuthorities().size());
assertNotNull(GeoServerCasAuthenticationFilter.getHandler().getSessionMappingStorage()
.removeSessionByMappingId(ticket));
helper.ssoLogout();
// test root user
username = GeoServerUser.ROOT_USERNAME;
password = username;
helper = new CasFormAuthenticationHelper(casServerURLPrefix, username, password);
helper.ssoLogin();
response = new MockHttpServletResponse();
chain = new MockFilterChain();
request = createRequest("/foo/bar");
response = new MockHttpServletResponse();
chain = new MockFilterChain();
ticket =loginUsingTicket(helper, request, response, chain);
ctx = (SecurityContext) request.getSession(true).getAttribute(
HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
assertFalse(response.wasRedirectSent());
auth = ctx.getAuthentication();
assertNotNull(auth);
assertNull(SecurityContextHolder.getContext().getAuthentication());
// checkForAuthenticatedRole(auth);
assertEquals(GeoServerUser.ROOT_USERNAME, auth.getPrincipal());
assertTrue(auth.getAuthorities().size() == 1);
assertTrue(auth.getAuthorities().contains(GeoServerRole.ADMIN_ROLE));
assertNotNull(GeoServerCasAuthenticationFilter.getHandler().getSessionMappingStorage()
.removeSessionByMappingId(ticket));
helper.ssoLogout();
// check disabled user
username = "castest";
password = username;
helper = new CasFormAuthenticationHelper(casServerURLPrefix, username, password);
helper.ssoLogin();
updateUser("ug1", username, false);
request = createRequest("/foo/bar");
response = new MockHttpServletResponse();
chain = new MockFilterChain();
ticket =loginUsingTicket(helper, request, response, chain);
assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
assertTrue(response.wasRedirectSent());
redirectURL = response.getHeader("Location");
assertTrue(redirectURL.contains("login"));
ctx = (SecurityContext) request.getSession(true).getAttribute(
HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
assertNull(ctx);
assertNull(SecurityContextHolder.getContext().getAuthentication());
assertNull(GeoServerCasAuthenticationFilter.getHandler().getSessionMappingStorage()
.removeSessionByMappingId(ticket));
updateUser("ug1", username, true);
helper.ssoLogout();
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();
// test invalid ticket
username = "castest";
password = username;
helper = new CasFormAuthenticationHelper(casServerURLPrefix, username, password);
helper.ssoLogin();
request = createRequest("/foo/bar");
response = new MockHttpServletResponse();
chain = new MockFilterChain();
ticket = helper.getServiceTicket(new URL(request.getRequestURL().toString()));
ticket += "ST-A";
request.setupAddParameter("ticket", ticket);
request.setQueryString("ticket=" + ticket);
getProxy().doFilter(request, response, chain);
assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
assertTrue(response.wasRedirectSent());
redirectURL = response.getHeader("Location");
assertTrue(redirectURL.contains(GeoServerCasConstants.LOGIN_URI));
ctx = (SecurityContext) request.getSession(true).getAttribute(
HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
assertNull(ctx);
assertNull(SecurityContextHolder.getContext().getAuthentication());
assertNull(GeoServerCasAuthenticationFilter.getHandler().getSessionMappingStorage()
.removeSessionByMappingId(ticket));
helper.ssoLogout();
// test success with proxy granting ticket
config.setProxyCallbackUrlPrefix(proxyCallbackUrlPrefix.toString());
getSecurityManager().saveFilter(config);
username = "castest";
password = username;
helper = new CasFormAuthenticationHelper(casServerURLPrefix, username, password);
helper.ssoLogin();
request = createRequest("/foo/bar");
response = new MockHttpServletResponse();
chain = new MockFilterChain();
ticket = helper.getServiceTicket(new URL(request.getRequestURL().toString()));
request.setupAddParameter("ticket", ticket);
request.setQueryString("ticket=" + ticket);
getProxy().doFilter(request, response, chain);
assertEquals(HttpServletResponse.SC_OK, response.getErrorCode());
// assertTrue(response.wasRedirectSent());
// redirectUrl = response.getHeader("Location");
// assertNotNull(redirectUrl);
ctx = (SecurityContext) request.getSession(true).getAttribute(
HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY);
assertNotNull(ctx);
PreAuthenticatedAuthenticationToken casAuth = (PreAuthenticatedAuthenticationToken) ctx.getAuthentication();
assertNotNull(casAuth);
assertNull(SecurityContextHolder.getContext().getAuthentication());
checkForAuthenticatedRole(casAuth);
assertEquals(username, casAuth.getPrincipal());
assertTrue(casAuth.getAuthorities().contains(new GeoServerRole(rootRole)));
assertTrue(casAuth.getAuthorities().contains(new GeoServerRole(derivedRole)));
Assertion ass = (Assertion) request.getSession(true).getAttribute(GeoServerCasConstants.CAS_ASSERTION_KEY);
assertNotNull(ass);
String proxyTicket = ass.getPrincipal()
.getProxyTicketFor("http://localhost/blabla");
assertNotNull(proxyTicket);