Skip to content

Commit

Permalink
Merge pull request apereo#239 from mattdrees/issue-210-preserve-entit…
Browse files Browse the repository at this point in the history
…y-stream-for-non-logout-requests

Add option to prevent entity stream consumption
  • Loading branch information
SavvasMisaghMoayyed committed Jul 25, 2018
2 parents a408179 + a61f7f2 commit ee9dcc6
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ The `SingleSignOutFilter` can affect character encoding. This becomes most obvio
| `relayStateParameterName` | Defaults to `RelayState` | No
| `eagerlyCreateSessions` | Defaults to `true` | No
| `artifactParameterOverPost` | Defaults to `false` | No
| `logoutCallbackPath` | The path which is expected to receive logout callback requests from the CAS server. This is necessary if your app needs access to the raw input stream when handling form posts. If not configured, the default behavior will check every form post for a logout parameter. | No
| `casServerUrlPrefix` | URL to root of CAS Web application context. | Yes

<a name="cas-protocol"></a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,5 @@ public interface ConfigurationKeys {
ConfigurationKey<Class<? extends Cas20ServiceTicketValidator>> TICKET_VALIDATOR_CLASS = new ConfigurationKey<Class<? extends Cas20ServiceTicketValidator>>("ticketValidatorClass", null);
ConfigurationKey<String> PROXY_CALLBACK_URL = new ConfigurationKey<String>("proxyCallbackUrl", null);
ConfigurationKey<String> RELAY_STATE_PARAMETER_NAME = new ConfigurationKey<String>("relayStateParameterName", "RelayState");
ConfigurationKey<String> LOGOUT_CALLBACK_PATH = new ConfigurationKey<String>("logoutCallbackPath", null);
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public void init(final FilterConfig filterConfig) throws ServletException {
setLogoutParameterName(getString(ConfigurationKeys.LOGOUT_PARAMETER_NAME));
setRelayStateParameterName(getString(ConfigurationKeys.RELAY_STATE_PARAMETER_NAME));
setCasServerUrlPrefix(getString(ConfigurationKeys.CAS_SERVER_URL_PREFIX));
setLogoutCallbackPath(getString(ConfigurationKeys.LOGOUT_CALLBACK_PATH));
HANDLER.setArtifactParameterOverPost(getBoolean(ConfigurationKeys.ARTIFACT_PARAMETER_OVER_POST));
HANDLER.setEagerlyCreateSessions(getBoolean(ConfigurationKeys.EAGERLY_CREATE_SESSIONS));
}
Expand All @@ -71,6 +72,10 @@ public void setCasServerUrlPrefix(final String casServerUrlPrefix) {
HANDLER.setCasServerUrlPrefix(casServerUrlPrefix);
}

public void setLogoutCallbackPath(String logoutCallbackPath) {
HANDLER.setLogoutCallbackPath(logoutCallbackPath);
}

public void setSessionMappingStorage(final SessionMappingStorage storage) {
HANDLER.setSessionMappingStorage(storage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ public final class SingleSignOutHandler {
/** The prefix url of the CAS server */
private String casServerUrlPrefix = "";

/** The logout callback path configured at the CAS server, if there is one */
private String logoutCallbackPath;

private boolean artifactParameterOverPost = false;

private boolean eagerlyCreateSessions = true;
Expand Down Expand Up @@ -106,7 +109,14 @@ public void setLogoutParameterName(final String name) {
public void setCasServerUrlPrefix(final String casServerUrlPrefix) {
this.casServerUrlPrefix = casServerUrlPrefix;
}


/**
* @param logoutCallbackPath The logout callback path configured at the CAS server.
*/
public void setLogoutCallbackPath(String logoutCallbackPath) {
this.logoutCallbackPath = logoutCallbackPath;
}

/**
* @param name Name of parameter containing the state of the CAS server webflow.
*/
Expand Down Expand Up @@ -163,6 +173,7 @@ private boolean isTokenRequest(final HttpServletRequest request) {
private boolean isLogoutRequest(final HttpServletRequest request) {
if ("POST".equalsIgnoreCase(request.getMethod())) {
return !isMultipartRequest(request)
&& pathEligibleForLogout(request)
&& CommonUtils.isNotBlank(CommonUtils.safeGetParameter(request, this.logoutParameterName,
this.safeParameters));
}
Expand All @@ -172,7 +183,15 @@ private boolean isLogoutRequest(final HttpServletRequest request) {
}
return false;
}


private boolean pathEligibleForLogout(HttpServletRequest request) {
return logoutCallbackPath == null || logoutCallbackPath.equals(getPath(request));
}

private String getPath(HttpServletRequest request) {
return request.getServletPath() + CommonUtils.nullToEmpty(request.getPathInfo());
}

/**
* Process a request regarding the SLO process: record the session or destroy it.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,17 @@ public static int toInt(final String str, final int defaultValue) {
}
}

/**
* Returns the string as-is, unless it's <code>null</code>;
* in this case an empty string is returned.
*
* @param string a possibly <code>null</code> string
* @return a non-<code>null</code> string
*/
public static String nullToEmpty(String string) {
return string == null ? "" : string;
}

/**
* Adds a trailing slash to the given uri, if it doesn't already have one.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,36 @@ public void backChannelLogoutFailsIfNoSessionIndex() {

@Test
public void backChannelLogoutOK() {
final MockHttpSession session = doBackChannelLogout();
assertFalse(handler.process(request, response));
assertTrue(session.isInvalid());
}

@Test
public void backChannelLogoutDoesNotRunIfPathIsNotEligibleForLogout() {
handler.setLogoutCallbackPath("/logout");
request.setServletPath("/not-a-logout");
final MockHttpSession session = doBackChannelLogout();
assertTrue(handler.process(request, response));
assertFalse(session.isInvalid());
}

@Test
public void backChannelLogoutRunsIfPathEqualsLogoutPath() {
handler.setLogoutCallbackPath("/logout");
request.setServletPath("/logout");
final MockHttpSession session = doBackChannelLogout();
assertFalse(handler.process(request, response));
assertTrue(session.isInvalid());
}

private MockHttpSession doBackChannelLogout() {
final String logoutMessage = LogoutMessageGenerator.generateBackChannelLogoutMessage(TICKET);
request.setParameter(LOGOUT_PARAMETER_NAME, logoutMessage);
request.setMethod("POST");
final MockHttpSession session = new MockHttpSession();
handler.getSessionMappingStorage().addSessionById(TICKET, session);
assertFalse(handler.process(request, response));
assertTrue(session.isInvalid());
return session;
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public void setCasServerUrlPrefix(final String casServerUrlPrefix) {
this.handler.setCasServerUrlPrefix(casServerUrlPrefix);
}

public void setLogoutCallbackPath(String logoutCallbackPath) {
this.handler.setLogoutCallbackPath(logoutCallbackPath);
}

public void setSessionMappingStorage(final SessionMappingStorage storage) {
this.handler.setSessionMappingStorage(storage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public void setCasServerUrlPrefix(final String casServerUrlPrefix) {
this.handler.setCasServerUrlPrefix(casServerUrlPrefix);
}

public void setLogoutCallbackPath(String logoutCallbackPath) {
this.handler.setLogoutCallbackPath(logoutCallbackPath);
}

public void setSessionMappingStorage(final SessionMappingStorage storage) {
this.handler.setSessionMappingStorage(storage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public void setCasServerUrlPrefix(final String casServerUrlPrefix) {
this.handler.setCasServerUrlPrefix(casServerUrlPrefix);
}

public void setLogoutCallbackPath(String logoutCallbackPath) {
this.handler.setLogoutCallbackPath(logoutCallbackPath);
}

public void setSessionMappingStorage(final SessionMappingStorage storage) {
this.handler.setSessionMappingStorage(storage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public void setCasServerUrlPrefix(final String casServerUrlPrefix) {
this.handler.setCasServerUrlPrefix(casServerUrlPrefix);
}

public void setLogoutCallbackPath(String logoutCallbackPath) {
this.handler.setLogoutCallbackPath(logoutCallbackPath);
}

public void setSessionMappingStorage(final SessionMappingStorage storage) {
this.handler.setSessionMappingStorage(storage);
}
Expand Down

0 comments on commit ee9dcc6

Please sign in to comment.