diff --git a/cas-client-core/src/main/java/org/jasig/cas/client/web/filter/AbstractCasFilter.java b/cas-client-core/src/main/java/org/jasig/cas/client/web/filter/AbstractCasFilter.java index 3c743e1..3123e8c 100644 --- a/cas-client-core/src/main/java/org/jasig/cas/client/web/filter/AbstractCasFilter.java +++ b/cas-client-core/src/main/java/org/jasig/cas/client/web/filter/AbstractCasFilter.java @@ -31,11 +31,6 @@ import java.io.IOException; */ public abstract class AbstractCasFilter implements Filter { - /** - * Constant string representing the ticket parameter. - */ - public static final String PARAM_TICKET = "ticket"; - /** * Constant representing where we store the Assertion in the * session. @@ -71,19 +66,15 @@ public abstract class AbstractCasFilter implements Filter { /** * Whether to store the entry in session or not. Defaults to true. */ - private final boolean useSession; + private boolean useSession = true; + private String artifactParameterName = "ticket"; protected AbstractCasFilter(final String service, final boolean isServerName) { - this(service, isServerName, true); - } - - protected AbstractCasFilter(final String service, final boolean isServerName, final boolean useSession) { CommonUtils.assertNotNull(service, "service must be set"); this.service = service; this.isServerName = isServerName; - this.useSession = useSession; log.info("Service set to: " + this.service + "; Is Server Name? set to: " + this.isServerName + "Use Session set to: " + this.useSession); } @@ -131,7 +122,7 @@ public abstract class AbstractCasFilter implements Filter { if (CommonUtils.isNotBlank(request.getQueryString())) { final int location = request.getQueryString().indexOf( - PARAM_TICKET + "="); + this.artifactParameterName + "="); if (location == 0) { final String returnValue = response.encodeURL(buffer @@ -148,7 +139,7 @@ public abstract class AbstractCasFilter implements Filter { buffer.append(request.getQueryString()); } else if (location > 0) { final int actualLocation = request.getQueryString() - .indexOf("&" + PARAM_TICKET + "="); + .indexOf("&" + this.artifactParameterName + "="); if (actualLocation == -1) { buffer.append(request.getQueryString()); @@ -170,4 +161,22 @@ public abstract class AbstractCasFilter implements Filter { protected final boolean isUseSession() { return this.useSession; } + + public final void setUseSession(final boolean useSession) { + this.useSession = useSession; + } + + /** + * Defaults to "ticket" based on the CAS 2 Specification. Other examples include SAML artifacts which are defined as + * "SAMLart" + * + * @param artifactName + */ + public final void setArtifactParameterName(final String artifactName) { + this.artifactParameterName = artifactName; + } + + protected final String getArtifactParameterName() { + return this.artifactParameterName; + } } diff --git a/cas-client-core/src/main/java/org/jasig/cas/client/web/filter/AuthenticationFilter.java b/cas-client-core/src/main/java/org/jasig/cas/client/web/filter/AuthenticationFilter.java index f403238..a863a75 100644 --- a/cas-client-core/src/main/java/org/jasig/cas/client/web/filter/AuthenticationFilter.java +++ b/cas-client-core/src/main/java/org/jasig/cas/client/web/filter/AuthenticationFilter.java @@ -34,35 +34,30 @@ public final class AuthenticationFilter extends AbstractCasFilter { /** * Whether to send the renew request or not. */ - private final boolean renew; + private boolean renew = false; /** * Whether to send the gateway request or not. */ - private final boolean gateway; + private boolean gateway = false; - public AuthenticationFilter(final String serverName, final boolean isServerName, final String casServerLoginUrl) { - this(serverName, isServerName, true, casServerLoginUrl, false, false); - } + /** + * Defines the parameter to look for when attempting to construct the login url. + */ + private String serviceParameterName = "service"; - public AuthenticationFilter(final String serverName, final boolean isServerName, final String casServerLoginUrl, boolean renew, boolean gateway) { - this(serverName, isServerName, true, casServerLoginUrl, renew, gateway); - } - - public AuthenticationFilter(final String serverName, final boolean isServerName, final boolean useSession, String casServerLoginUrl, final boolean renew, final boolean gateway) { - super(serverName, isServerName, useSession); + public AuthenticationFilter(final String serverName, final boolean isServerName, String casServerLoginUrl) { + super(serverName, isServerName); CommonUtils.assertNotNull(casServerLoginUrl, "the CAS Server Login URL cannot be null."); this.casServerLoginUrl = casServerLoginUrl; - this.renew = renew; - this.gateway = gateway; } protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response, final FilterChain filterChain) throws IOException, ServletException { final HttpSession session = request.getSession(isUseSession()); - final String ticket = request.getParameter(PARAM_TICKET); + final String ticket = request.getParameter(getArtifactParameterName()); final Assertion assertion = session != null ? (Assertion) session .getAttribute(CONST_ASSERTION) : null; final boolean wasGatewayed = session != null @@ -76,7 +71,7 @@ public final class AuthenticationFilter extends AbstractCasFilter { } final String serviceUrl = constructServiceUrl(request, response); - final String urlToRedirectTo = this.casServerLoginUrl + "?service=" + final String urlToRedirectTo = this.casServerLoginUrl + "?" + this.serviceParameterName + "=" + URLEncoder.encode(serviceUrl, "UTF-8") + (this.renew ? "&renew=true" : "") + (this.gateway ? "&gateway=true" : ""); @@ -96,4 +91,22 @@ public final class AuthenticationFilter extends AbstractCasFilter { filterChain.doFilter(request, response); } + + public void setRenew(final boolean renew) { + this.renew = renew; + } + + public void setGateway(final boolean gateway) { + this.gateway = gateway; + } + + /** + * Defaults to "service" due to the CAS 2.0 specification. Other options + * include the SAML specifications's TARGET attribute. + * + * @param serviceParameterName + */ + public void setServiceParameterName(final String serviceParameterName) { + this.serviceParameterName = serviceParameterName; + } } diff --git a/cas-client-core/src/main/java/org/jasig/cas/client/web/filter/TicketValidationFilter.java b/cas-client-core/src/main/java/org/jasig/cas/client/web/filter/TicketValidationFilter.java index 723b39b..f5aa47d 100644 --- a/cas-client-core/src/main/java/org/jasig/cas/client/web/filter/TicketValidationFilter.java +++ b/cas-client-core/src/main/java/org/jasig/cas/client/web/filter/TicketValidationFilter.java @@ -43,52 +43,23 @@ public final class TicketValidationFilter extends AbstractCasFilter { * successful validation to remove the ticket parameter from the query * string. */ - private final boolean redirectAfterValidation; + private boolean redirectAfterValidation = false; /** Determines whether an exception is thrown when there is a ticket validation failure. */ - private final boolean exceptionOnValidationFailure; + private boolean exceptionOnValidationFailure = true; /** - * Constructor that takes the severName (or serviceUrl) and the TicketValidator. Either serveName or serviceUrl is required (but not both). + * Constructor that takes the severName (or serviceUrl), TicketValidator, useSession and redirectAfterValidation. Either serveName or serviceUrl is required (but not both). * * @param service the name of the server in : combination, if using a non-standard port or the fully qualified url. * @param isServerName whether the service is the host name or the fully qualified url. - * @param ticketValidator the validator to validate the tickets. + * @param ticketValidator the validator to validate the tickets. */ public TicketValidationFilter(final String service, final boolean isServerName, final TicketValidator ticketValidator) { - this(service, isServerName, true, ticketValidator, false); - } - - /** - * Constructor that takes the severName (or serviceUrl), TicketValidator, useSession and redirectAfterValidation. Either serveName or serviceUrl is required (but not both). - * - * @param service the name of the server in : combination, if using a non-standard port or the fully qualified url. - * @param isServerName whether the service is the host name or the fully qualified url. - * @param useSession flag to set whether to store stuff in the session. - * @param ticketValidator the validator to validate the tickets. - * @param redirectAfterValidation whether to redirect to remove the ticket. - */ - public TicketValidationFilter(final String service, final boolean isServerName, final boolean useSession, final TicketValidator ticketValidator, final boolean redirectAfterValidation) { - this(service, isServerName, useSession, ticketValidator, redirectAfterValidation, true); - } - - /** - * Constructor that takes the severName (or serviceUrl), TicketValidator, useSession and redirectAfterValidation. Either serveName or serviceUrl is required (but not both). - * - * @param service the name of the server in : combination, if using a non-standard port or the fully qualified url. - * @param isServerName whether the service is the host name or the fully qualified url. - * @param useSession flag to set whether to store stuff in the session. - * @param ticketValidator the validator to validate the tickets. - * @param redirectAfterValidation whether to redirect to remove the ticket. - * @param exceptionOnValidationFailure whether to throw an exception if there is a validation failure or not. - */ - public TicketValidationFilter(final String service, final boolean isServerName, final boolean useSession, final TicketValidator ticketValidator, final boolean redirectAfterValidation, final boolean exceptionOnValidationFailure) { - super(service, isServerName, useSession); + super(service, isServerName); CommonUtils.assertNotNull(ticketValidator, "ticketValidator cannot be null."); this.ticketValidator = ticketValidator; - this.redirectAfterValidation = redirectAfterValidation; - this.exceptionOnValidationFailure = exceptionOnValidationFailure; log.info("Initialized with the following properties: " + "ticketValidator=" + this.ticketValidator.getClass().getName() + "; " + @@ -99,7 +70,7 @@ public final class TicketValidationFilter extends AbstractCasFilter { protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response, final FilterChain filterChain) throws IOException, ServletException { - final String ticket = request.getParameter(PARAM_TICKET); + final String ticket = request.getParameter(getArtifactParameterName()); if (CommonUtils.isNotBlank(ticket)) { if (log.isDebugEnabled()) { @@ -140,4 +111,13 @@ public final class TicketValidationFilter extends AbstractCasFilter { filterChain.doFilter(request, response); } + + public void setRedirectAfterValidation(final boolean redirectAfterValidation) { + this.redirectAfterValidation = redirectAfterValidation; + } + + + public void setExceptionOnValidationFailure(final boolean exceptionOnValidationFailure) { + this.exceptionOnValidationFailure = exceptionOnValidationFailure; + } } diff --git a/cas-client-core/src/test/java/org/jasig/cas/client/web/filter/AuthenticationFilterTests.java b/cas-client-core/src/test/java/org/jasig/cas/client/web/filter/AuthenticationFilterTests.java index 134f4c8..7324ea4 100644 --- a/cas-client-core/src/test/java/org/jasig/cas/client/web/filter/AuthenticationFilterTests.java +++ b/cas-client-core/src/test/java/org/jasig/cas/client/web/filter/AuthenticationFilterTests.java @@ -36,7 +36,7 @@ public final class AuthenticationFilterTests extends TestCase { private AuthenticationFilter filter; protected void setUp() throws Exception { - this.filter = new AuthenticationFilter(CAS_SERVICE_URL, false, CAS_LOGIN_URL, false, false); + this.filter = new AuthenticationFilter(CAS_SERVICE_URL, false, CAS_LOGIN_URL); this.filter.init(new MockFilterConfig()); } @@ -80,7 +80,7 @@ public final class AuthenticationFilterTests extends TestCase { }; request.setSession(session); - this.filter = new AuthenticationFilter("localhost:8443", true, CAS_LOGIN_URL, false, false); + this.filter = new AuthenticationFilter("localhost:8443", true, CAS_LOGIN_URL); this.filter.doFilter(request, response, filterChain); assertEquals(CAS_LOGIN_URL @@ -122,7 +122,8 @@ public final class AuthenticationFilterTests extends TestCase { } }; - this.filter = new AuthenticationFilter("localhost:8443", true, CAS_LOGIN_URL, true, false); + this.filter = new AuthenticationFilter("localhost:8443", true, CAS_LOGIN_URL); + this.filter.setRenew(true); request.setSession(session); this.filter.doFilter(request, response, filterChain); @@ -143,7 +144,9 @@ public final class AuthenticationFilterTests extends TestCase { }; request.setSession(session); - this.filter = new AuthenticationFilter("localhost:8443", true, CAS_LOGIN_URL, true, true); + this.filter = new AuthenticationFilter("localhost:8443", true, CAS_LOGIN_URL); + this.filter.setRenew(true); + this.filter.setGateway(true);; this.filter.doFilter(request, response, filterChain); assertNotNull(session.getAttribute(AbstractCasFilter.CONST_GATEWAY)); assertNotNull(response.getRedirectedUrl()); diff --git a/cas-client-core/src/test/java/org/jasig/cas/client/web/filter/ValidationFilterTests.java b/cas-client-core/src/test/java/org/jasig/cas/client/web/filter/ValidationFilterTests.java index f9681a3..ac180fb 100644 --- a/cas-client-core/src/test/java/org/jasig/cas/client/web/filter/ValidationFilterTests.java +++ b/cas-client-core/src/test/java/org/jasig/cas/client/web/filter/ValidationFilterTests.java @@ -74,7 +74,7 @@ public final class ValidationFilterTests extends TestCase { final MockHttpServletResponse response = new MockHttpServletResponse(); final MockHttpSession session = new MockHttpSession(); request.setSession(session); - request.setParameter(AbstractCasFilter.PARAM_TICKET, "true"); + request.setParameter("ticket", "true"); final FilterChain filterChain = new FilterChain() { public void doFilter(final ServletRequest arg0, @@ -95,7 +95,7 @@ public final class ValidationFilterTests extends TestCase { final MockHttpServletResponse response = new MockHttpServletResponse(); final MockHttpSession session = new MockHttpSession(); request.setSession(session); - request.setParameter(AbstractCasFilter.PARAM_TICKET, "false"); + request.setParameter("ticket", "false"); final FilterChain filterChain = new FilterChain() { public void doFilter(final ServletRequest arg0, diff --git a/cas-client-uportal/src/main/java/org/jasig/cas/client/integration/uportal/ThreadLocalAwareCasServiceFilter.java b/cas-client-uportal/src/main/java/org/jasig/cas/client/integration/uportal/ThreadLocalAwareCasServiceFilter.java index c57ea8b..979e811 100644 --- a/cas-client-uportal/src/main/java/org/jasig/cas/client/integration/uportal/ThreadLocalAwareCasServiceFilter.java +++ b/cas-client-uportal/src/main/java/org/jasig/cas/client/integration/uportal/ThreadLocalAwareCasServiceFilter.java @@ -29,20 +29,15 @@ import java.io.IOException; */ public final class ThreadLocalAwareCasServiceFilter extends AbstractCasFilter { - public ThreadLocalAwareCasServiceFilter(final String service, final boolean isServerName) { super(service, isServerName); } - public ThreadLocalAwareCasServiceFilter(final String service, final boolean isServerName, final boolean useSession) { - super(service, isServerName, useSession); - } - protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response, final FilterChain filterChain) throws IOException, ServletException { final boolean hasTicket = CommonUtils.isNotBlank(request - .getParameter(AbstractCasFilter.PARAM_TICKET)); + .getParameter(getArtifactParameterName())); try { if (hasTicket) { final Service service = new SimpleService(constructServiceUrl(