This repository has been archived by the owner on May 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SECOAUTH-210: Consolidated all error handling
- Loading branch information
Showing
27 changed files
with
423 additions
and
564 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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
69 changes: 69 additions & 0 deletions
69
.../springframework/security/oauth2/provider/authentication/OAuth2AuthenticationManager.java
This file contains 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,69 @@ | ||
/* | ||
* Copyright 2006-2011 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.oauth2.provider.authentication; | ||
|
||
import java.util.Collection; | ||
|
||
import org.springframework.beans.factory.InitializingBean; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException; | ||
import org.springframework.security.oauth2.provider.OAuth2Authentication; | ||
import org.springframework.security.oauth2.provider.token.ResourceServerTokenServices; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* @author Dave Syer | ||
* | ||
*/ | ||
public class OAuth2AuthenticationManager implements AuthenticationManager, InitializingBean { | ||
|
||
private ResourceServerTokenServices tokenServices; | ||
|
||
private String resourceId; | ||
|
||
public void setResourceId(String resourceId) { | ||
this.resourceId = resourceId; | ||
} | ||
|
||
/** | ||
* @param tokenServices the tokenServices to set | ||
*/ | ||
public void setTokenServices(ResourceServerTokenServices tokenServices) { | ||
this.tokenServices = tokenServices; | ||
} | ||
|
||
public void afterPropertiesSet() { | ||
Assert.state(tokenServices!=null, "TokenServices are required"); | ||
} | ||
|
||
public Authentication authenticate(Authentication authentication) throws AuthenticationException { | ||
|
||
String token = (String) authentication.getPrincipal(); | ||
OAuth2Authentication auth = tokenServices.loadAuthentication(token); | ||
if (auth == null) { | ||
throw new InvalidTokenException("Invalid token: " + token); | ||
} | ||
|
||
Collection<String> resourceIds = auth.getAuthorizationRequest().getResourceIds(); | ||
if (resourceIds != null && !resourceIds.isEmpty() && !resourceIds.contains(resourceId)) { | ||
throw new InvalidTokenException("Invalid token does not contain resource id (" + resourceId + "): " | ||
+ token); | ||
} | ||
|
||
return auth; | ||
|
||
} | ||
|
||
} |
90 changes: 90 additions & 0 deletions
90
...amework/security/oauth2/provider/authentication/OAuth2AuthenticationProcessingFilter.java
This file contains 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,90 @@ | ||
/* | ||
* Copyright 2006-2011 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.oauth2.provider.authentication; | ||
|
||
import java.util.Enumeration; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.springframework.security.oauth2.common.OAuth2AccessToken; | ||
import org.springframework.security.web.authentication.preauth.AbstractPreAuthenticatedProcessingFilter; | ||
|
||
/** | ||
* @author Dave Syer | ||
* | ||
*/ | ||
public class OAuth2AuthenticationProcessingFilter extends AbstractPreAuthenticatedProcessingFilter { | ||
|
||
private final static Log logger = LogFactory.getLog(OAuth2AuthenticationProcessingFilter.class); | ||
|
||
public void afterPropertiesSet() { | ||
super.afterPropertiesSet(); | ||
} | ||
|
||
@Override | ||
protected Object getPreAuthenticatedCredentials(HttpServletRequest request) { | ||
return "N/A"; | ||
} | ||
|
||
@Override | ||
protected Object getPreAuthenticatedPrincipal(HttpServletRequest request) { | ||
return parseToken(request); | ||
} | ||
|
||
protected String parseToken(HttpServletRequest request) { | ||
// first check the header... | ||
String token = parseHeaderToken(request); | ||
|
||
// bearer type allows a request parameter as well | ||
if (token == null) { | ||
logger.debug("Token not found in headers. Trying request parameters."); | ||
token = request.getParameter(OAuth2AccessToken.ACCESS_TOKEN); | ||
if (token == null) { | ||
logger.debug("Token not found in request parameters. Not an OAuth2 request."); | ||
} | ||
} | ||
|
||
return token; | ||
} | ||
|
||
/** | ||
* Parse the OAuth header parameters. The parameters will be oauth-decoded. | ||
* | ||
* @param request The request. | ||
* @return The parsed parameters, or null if no OAuth authorization header was supplied. | ||
*/ | ||
protected String parseHeaderToken(HttpServletRequest request) { | ||
@SuppressWarnings("unchecked") | ||
Enumeration<String> headers = request.getHeaders("Authorization"); | ||
while (headers.hasMoreElements()) { | ||
String value = headers.nextElement(); | ||
if ((value.toLowerCase().startsWith(OAuth2AccessToken.BEARER_TYPE.toLowerCase()))) { | ||
String authHeaderValue = value.substring(OAuth2AccessToken.BEARER_TYPE.length()).trim(); | ||
int commaIndex = authHeaderValue.indexOf(','); | ||
if (commaIndex > 0) { | ||
authHeaderValue = authHeaderValue.substring(0, commaIndex); | ||
} | ||
return authHeaderValue; | ||
} | ||
else { | ||
// todo: support additional authorization schemes for different token types, e.g. "MAC" specified by | ||
// http://tools.ietf.org/html/draft-hammer-oauth-v2-mac-token | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
} |
This file contains 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 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 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
2 changes: 1 addition & 1 deletion
2
...ider/filter/EndpointValidationFilter.java → ...er/endpoint/EndpointValidationFilter.java
This file contains 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
Oops, something went wrong.