-
Notifications
You must be signed in to change notification settings - Fork 6k
Added support for the CAS gateway feature. #9881
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
54 changes: 54 additions & 0 deletions
54
...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,54 @@ | ||
/* | ||
* 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 { | ||
|
||
|
||
private static final long serialVersionUID = 4864856111227081697L; | ||
|
||
//~ 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
131 changes: 131 additions & 0 deletions
131
cas/src/main/java/org/springframework/security/cas/web/CasCookieGatewayRequestMatcher.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,131 @@ | ||
package org.springframework.security.cas.web; | ||
|
||
import javax.servlet.http.Cookie; | ||
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.matcher.RequestMatcher; | ||
import org.springframework.util.Assert; | ||
import org.springframework.util.StringUtils; | ||
|
||
/** | ||
* 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 CasCookieGatewayRequestMatcher implements RequestMatcher { | ||
|
||
// ~ Instance fields | ||
// ================================================================================================ | ||
|
||
private ServiceProperties serviceProperties; | ||
|
||
private String cookieName; | ||
|
||
private GatewayResolver gatewayStorage = new DefaultGatewayResolverImpl(); | ||
|
||
// ~ Constructors | ||
// =================================================================================================== | ||
|
||
public CasCookieGatewayRequestMatcher(ServiceProperties serviceProperties, final String cookieName) { | ||
Assert.notNull(serviceProperties, "serviceProperties cannot be null"); | ||
this.serviceProperties = serviceProperties; | ||
this.cookieName = cookieName; | ||
} | ||
|
||
public final boolean matches(HttpServletRequest request) { | ||
|
||
// Test if we are already authenticated | ||
if (isAuthenticated(request)) { | ||
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. | ||
* This implementation returns <code>true</code> only if a {@link Cookie} with the configured | ||
* name is present at the request | ||
* | ||
* @param request | ||
* @return true if the request must trigger a CAS gateway authentication | ||
*/ | ||
protected boolean performGatewayAuthentication(HttpServletRequest request) { | ||
if (!StringUtils.hasText(this.cookieName)) { | ||
return true; | ||
} | ||
|
||
Cookie[] cookies = request.getCookies(); | ||
if (cookies == null || cookies.length == 0) { | ||
return false; | ||
} | ||
|
||
for (Cookie cookie: cookies) { | ||
// Check the cookie name. If it matches the configured cookie name, return true | ||
if (this.cookieName.equalsIgnoreCase(cookie.getName())) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public void setGatewayStorage(GatewayResolver gatewayStorage) { | ||
Assert.notNull(gatewayStorage, "gatewayStorage cannot be null"); | ||
this.gatewayStorage = gatewayStorage; | ||
} | ||
|
||
public String getCookieName() { | ||
return cookieName; | ||
} | ||
|
||
public void setCookieName(String cookieName) { | ||
this.cookieName = cookieName; | ||
} | ||
} |
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.
Among other tangles, this introduces a tangle between between
org.springframework.security.cas.authentication
andorg.springframework.security.cas.web
. In general, authentication package should not rely on web.