-
Notifications
You must be signed in to change notification settings - Fork 6.1k
SEC-977: Add support for CAS gateway feature #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
...main/java/org/springframework/security/cas/authentication/TriggerCasGatewayException.java
This file contains hidden or 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,52 @@ | ||
/* | ||
* Copyright 2013-2013 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 | ||
* | ||
* http://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.authentication; | ||
|
||
import org.springframework.security.cas.web.CasAuthenticationEntryPoint; | ||
import org.springframework.security.core.AuthenticationException; | ||
|
||
/** | ||
* Exception used to indicate the {@link CasAuthenticationEntryPoint} to make a CAS gateway authentication request. | ||
* | ||
* @author Michael Remond | ||
* | ||
*/ | ||
public class TriggerCasGatewayException extends | ||
AuthenticationException { | ||
|
||
//~ Constructors =================================================================================================== | ||
|
||
/** | ||
* Constructs an {@code InitiateCasGatewayAuthenticationException} with the specified message and no root cause. | ||
* | ||
* @param msg the detail message | ||
*/ | ||
public TriggerCasGatewayException(String msg) { | ||
super(msg); | ||
} | ||
|
||
/** | ||
* Constructs an {@code InitiateCasGatewayAuthenticationException} with the specified message and root cause. | ||
* | ||
* @param msg the detail message | ||
* @param t the root cause | ||
*/ | ||
public TriggerCasGatewayException(String msg, Throwable t) { | ||
super(msg, t); | ||
} | ||
|
||
} |
This file contains hidden or 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
This file contains hidden or 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
103 changes: 103 additions & 0 deletions
103
cas/src/main/java/org/springframework/security/cas/web/DefaultCasGatewayRequestMatcher.java
This file contains hidden or 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,103 @@ | ||
package org.springframework.security.cas.web; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
import org.jasig.cas.client.authentication.DefaultGatewayResolverImpl; | ||
import org.jasig.cas.client.authentication.GatewayResolver; | ||
import org.springframework.security.cas.ServiceProperties; | ||
import org.springframework.security.cas.authentication.CasAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.web.util.RequestMatcher; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* Default RequestMatcher implementation for the {@link TriggerCasGatewayFilter}. | ||
* | ||
* This RequestMatcher returns <code>true</code> iff : | ||
* <ul> | ||
* <li> | ||
* User is not already authenticated (see {@link #isAuthenticated})</li> | ||
* <li> | ||
* The request was not previously gatewayed</li> | ||
* <li> | ||
* The request matches additional criteria (see {@link #performGatewayAuthentication})</li> | ||
* </ul> | ||
* | ||
* Implementors can override this class to customize the authentication check and the gateway criteria. | ||
* <p> | ||
* The request is marked as "gatewayed" using the configured {@link GatewayResolver} to avoid infinite loop. | ||
* | ||
* @author Michael Remond | ||
* | ||
*/ | ||
public class DefaultCasGatewayRequestMatcher implements RequestMatcher { | ||
|
||
// ~ Instance fields | ||
// ================================================================================================ | ||
|
||
private ServiceProperties serviceProperties; | ||
|
||
private GatewayResolver gatewayStorage = new DefaultGatewayResolverImpl(); | ||
|
||
// ~ Constructors | ||
// =================================================================================================== | ||
|
||
public DefaultCasGatewayRequestMatcher(ServiceProperties serviceProperties) { | ||
Assert.notNull(serviceProperties, "serviceProperties cannot be null"); | ||
this.serviceProperties = serviceProperties; | ||
} | ||
|
||
public final boolean matches(HttpServletRequest request) { | ||
|
||
// Test if we are already authenticated | ||
if (isAuthenticated(request)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be omited for Single Logout to work |
||
return false; | ||
} | ||
|
||
// Test if the request was already gatewayed to avoid infinite loop | ||
final boolean wasGatewayed = this.gatewayStorage.hasGatewayedAlready(request, serviceProperties.getService()); | ||
|
||
if (wasGatewayed) { | ||
return false; | ||
} | ||
|
||
// If request matches gateway criteria, we mark the request as gatewayed and return true to trigger a CAS | ||
// gateway authentication | ||
if (performGatewayAuthentication(request)) { | ||
gatewayStorage.storeGatewayInformation(request, serviceProperties.getService()); | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Test if the user is authenticated in Spring Security. Default implementation test if the user is CAS | ||
* authenticated. | ||
* | ||
* @param request | ||
* @return true if the user is authenticated | ||
*/ | ||
protected boolean isAuthenticated(HttpServletRequest request) { | ||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
return authentication instanceof CasAuthenticationToken; | ||
} | ||
|
||
/** | ||
* Method that determines if the current request triggers a CAS gateway authentication. Default implementation | ||
* always returns <code>true</code>. | ||
* | ||
* @param request | ||
* @return true if the request must trigger a CAS gateway authentication | ||
*/ | ||
protected boolean performGatewayAuthentication(HttpServletRequest request) { | ||
return true; | ||
} | ||
|
||
public void setGatewayStorage(GatewayResolver gatewayStorage) { | ||
Assert.notNull(gatewayStorage, "gatewayStorage cannot be null"); | ||
this.gatewayStorage = gatewayStorage; | ||
} | ||
|
||
} |
78 changes: 78 additions & 0 deletions
78
cas/src/main/java/org/springframework/security/cas/web/TriggerCasGatewayFilter.java
This file contains hidden or 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,78 @@ | ||
/* | ||
* Copyright 2013-2013 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 | ||
* | ||
* http://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 javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.springframework.security.cas.authentication.TriggerCasGatewayException; | ||
import org.springframework.security.web.util.RequestMatcher; | ||
import org.springframework.util.Assert; | ||
import org.springframework.web.filter.GenericFilterBean; | ||
|
||
/** | ||
* Triggers a CAS gateway authentication attempt. | ||
* <p> | ||
* This filter must be placed after the <code>ExceptionTranslationFilter</code> in the filter chain in order to start | ||
* the authentication process. Throws a {@link TriggerCasGatewayException} when the current request matches against the | ||
* configured {@link RequestMatcher}. | ||
* <p> | ||
* The default implementation you can use is {@link DefaultCasGatewayRequestMatcher}. | ||
* | ||
* @author Michael Remond | ||
*/ | ||
public class TriggerCasGatewayFilter extends GenericFilterBean { | ||
|
||
// ~ Instance fields | ||
// ================================================================================================ | ||
|
||
private RequestMatcher requestMatcher; | ||
|
||
// ~ Constructors | ||
// =================================================================================================== | ||
|
||
public TriggerCasGatewayFilter(RequestMatcher requestMatcher) { | ||
Assert.notNull(requestMatcher, "requestMatcher cannot be null"); | ||
this.requestMatcher = requestMatcher; | ||
} | ||
|
||
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, | ||
ServletException { | ||
|
||
HttpServletRequest request = (HttpServletRequest) req; | ||
HttpServletResponse response = (HttpServletResponse) res; | ||
|
||
if (requestMatcher.matches(request)) { | ||
throw new TriggerCasGatewayException("Try a CAS gateway authentication"); | ||
} else { | ||
// Continue in the chain | ||
chain.doFilter(request, response); | ||
} | ||
|
||
} | ||
|
||
public void setRequestMatcher(RequestMatcher requestMatcher) { | ||
this.requestMatcher = requestMatcher; | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add null check here and a test to verify it cannot be null