Added support for the CAS gateway feature
This commit is contained in:
committed by
Marcus Hert Da Coregio
parent
ec02c22459
commit
f516fbc39a
@@ -36,6 +36,7 @@ import org.springframework.security.core.context.SecurityContext;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
||||
import org.springframework.security.web.context.SecurityContextRepository;
|
||||
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -219,4 +220,19 @@ public class CasAuthenticationFilterTests {
|
||||
verify(securityContextRepository).saveContext(any(SecurityContext.class), eq(request), eq(response));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullServiceButGateway() throws Exception {
|
||||
CasAuthenticationFilter filter = new CasAuthenticationFilter();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
request.getSession(true).setAttribute(TriggerCasGatewayFilter.TRIGGER_CAS_GATEWAY_AUTHENTICATION, true);
|
||||
|
||||
new HttpSessionRequestCache().saveRequest(request, response);
|
||||
|
||||
Authentication authn = filter.attemptAuthentication(request, response);
|
||||
assertThat(authn).isNull();
|
||||
assertThat(response.getStatus()).isEqualTo(302);
|
||||
assertThat(response.getRedirectedUrl()).isEqualTo("http://localhost?continue");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.security.cas.web;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.Cookie;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.apereo.cas.client.authentication.DefaultGatewayResolverImpl;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.security.cas.ServiceProperties;
|
||||
import org.springframework.security.cas.authentication.CasAuthenticationToken;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests {@link CasCookieGatewayRequestMatche}.
|
||||
*
|
||||
* @author Michael Remond
|
||||
*/
|
||||
public class CasCookieGatewayRequestMatcherTests {
|
||||
|
||||
@Test
|
||||
public void testNullServiceProperties() throws Exception {
|
||||
try {
|
||||
new CasCookieGatewayRequestMatcher(null, null);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
assertThat(expected.getMessage()).isEqualTo("serviceProperties cannot be null");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNormalOperationWithNoSSOSession() throws IOException, ServletException {
|
||||
SecurityContextHolder.getContext().setAuthentication(null);
|
||||
ServiceProperties serviceProperties = new ServiceProperties();
|
||||
serviceProperties.setService("http://localhost/j_spring_cas_security_check");
|
||||
CasCookieGatewayRequestMatcher rm = new CasCookieGatewayRequestMatcher(serviceProperties, null);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/some_path");
|
||||
|
||||
// First request
|
||||
assertThat(rm.matches(request)).isTrue();
|
||||
assertThat(request.getSession(false).getAttribute(DefaultGatewayResolverImpl.CONST_CAS_GATEWAY)).isNotNull();
|
||||
// Second request
|
||||
assertThat(rm.matches(request)).isFalse();
|
||||
assertThat(request.getSession(false).getAttribute(DefaultGatewayResolverImpl.CONST_CAS_GATEWAY)).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGatewayWhenCasAuthenticated() throws IOException, ServletException {
|
||||
SecurityContextHolder.getContext().setAuthentication(null);
|
||||
ServiceProperties serviceProperties = new ServiceProperties();
|
||||
serviceProperties.setService("http://localhost/j_spring_cas_security_check");
|
||||
CasCookieGatewayRequestMatcher rm = new CasCookieGatewayRequestMatcher(serviceProperties,
|
||||
"CAS_TGT_COOKIE_TEST_NAME");
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/some_path");
|
||||
request.setCookies(new Cookie("CAS_TGT_COOKIE_TEST_NAME", "casTGCookieValue"));
|
||||
|
||||
assertThat(rm.matches(request)).isTrue();
|
||||
|
||||
MockHttpServletRequest requestWithoutCasCookie = new MockHttpServletRequest("GET", "/some_path");
|
||||
requestWithoutCasCookie.setCookies(new Cookie("WRONG_CAS_TGT_COOKIE_TEST_NAME", "casTGCookieValue"));
|
||||
|
||||
assertThat(rm.matches(requestWithoutCasCookie)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGatewayWhenAlreadySessionCreated() throws IOException, ServletException {
|
||||
SecurityContextHolder.getContext().setAuthentication(mock(CasAuthenticationToken.class));
|
||||
|
||||
ServiceProperties serviceProperties = new ServiceProperties();
|
||||
serviceProperties.setService("http://localhost/j_spring_cas_security_check");
|
||||
CasCookieGatewayRequestMatcher rm = new CasCookieGatewayRequestMatcher(serviceProperties,
|
||||
"CAS_TGT_COOKIE_TEST_NAME");
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/some_path");
|
||||
assertThat(rm.matches(request)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGatewayWithNoMatchingRequest() throws IOException, ServletException {
|
||||
SecurityContextHolder.getContext().setAuthentication(null);
|
||||
ServiceProperties serviceProperties = new ServiceProperties();
|
||||
serviceProperties.setService("http://localhost/j_spring_cas_security_check");
|
||||
CasCookieGatewayRequestMatcher rm = new CasCookieGatewayRequestMatcher(serviceProperties,
|
||||
"CAS_TGT_COOKIE_TEST_NAME") {
|
||||
@Override
|
||||
protected boolean performGatewayAuthentication(HttpServletRequest request) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/some_path");
|
||||
|
||||
assertThat(rm.matches(request)).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.security.cas.web;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import jakarta.servlet.FilterChain;
|
||||
import jakarta.servlet.ServletException;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.security.cas.ServiceProperties;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
|
||||
import org.springframework.security.web.savedrequest.RequestCache;
|
||||
import org.springframework.security.web.util.matcher.RequestMatcher;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Tests {@link TriggerCasGatewayFilter}.
|
||||
*
|
||||
* @author Jerome LELEU
|
||||
*/
|
||||
public class TriggerCasGatewayFilterTests {
|
||||
|
||||
private static final String CAS_LOGIN_URL = "http://mycasserver/login";
|
||||
|
||||
@AfterEach
|
||||
public void tearDown() {
|
||||
SecurityContextHolder.clearContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGettersSetters() {
|
||||
ServiceProperties sp = new ServiceProperties();
|
||||
TriggerCasGatewayFilter filter = new TriggerCasGatewayFilter(CAS_LOGIN_URL, sp);
|
||||
assertThat(filter.getLoginUrl()).isEqualTo(CAS_LOGIN_URL);
|
||||
assertThat(filter.getServiceProperties()).isEqualTo(sp);
|
||||
assertThat(filter.getRequestMatcher().getClass()).isEqualTo(CasCookieGatewayRequestMatcher.class);
|
||||
assertThat(filter.getRequestCache().getClass()).isEqualTo(HttpSessionRequestCache.class);
|
||||
RequestMatcher requestMatcher = mock(RequestMatcher.class);
|
||||
filter.setRequestMatcher(requestMatcher);
|
||||
assertThat(filter.getRequestMatcher()).isEqualTo(requestMatcher);
|
||||
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> filter.setRequestMatcher(null));
|
||||
RequestCache requestCache = mock(RequestCache.class);
|
||||
filter.setRequestCache(requestCache);
|
||||
assertThat(filter.getRequestCache()).isEqualTo(requestCache);
|
||||
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> filter.setRequestCache(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOperation() throws IOException, ServletException {
|
||||
ServiceProperties sp = new ServiceProperties();
|
||||
sp.setService("http://myservice");
|
||||
TriggerCasGatewayFilter filter = new TriggerCasGatewayFilter(CAS_LOGIN_URL, sp);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
FilterChain chain = mock(FilterChain.class);
|
||||
|
||||
filter.doFilter(request, response, chain);
|
||||
assertThat(filter.getRequestCache().getRequest(request, response)).isNotNull();
|
||||
assertThat(request.getSession(false).getAttribute(TriggerCasGatewayFilter.TRIGGER_CAS_GATEWAY_AUTHENTICATION))
|
||||
.isEqualTo(true);
|
||||
assertThat(response.getStatus()).isEqualTo(302);
|
||||
assertThat(response.getRedirectedUrl())
|
||||
.isEqualTo(CAS_LOGIN_URL + "?service=http%3A%2F%2Fmyservice&gateway=true");
|
||||
verify(chain, never()).doFilter(request, response);
|
||||
|
||||
filter.doFilter(request, response, chain);
|
||||
verify(chain, times(1)).doFilter(request, response);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user