Skip to content
This repository has been archived by the owner on May 31, 2022. It is now read-only.

Commit

Permalink
Ensure AccessDeniedException is thrown from expression handler
Browse files Browse the repository at this point in the history
... otherwise the AccessDeniedHandler doesn't get a chance to send
the proper response and user just sees 500 status

Fixes gh-118
  • Loading branch information
dsyer committed Apr 1, 2014
1 parent 3637044 commit 45a0964
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
Expand Up @@ -65,7 +65,8 @@ public OAuth2SecurityExpressionMethods(Authentication authentication) {
*/
public boolean throwOnError(boolean decision) {
if (!decision && !missingScopes.isEmpty()) {
throw new InsufficientScopeException("Insufficient scope for this resource", missingScopes);
Throwable failure = new InsufficientScopeException("Insufficient scope for this resource", missingScopes);
throw new AccessDeniedException(failure.getMessage(), failure);
}
return decision;
}
Expand Down
Expand Up @@ -25,10 +25,10 @@
import org.junit.Test;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.oauth2.common.exceptions.InsufficientScopeException;
import org.springframework.security.oauth2.provider.AuthorizationRequest;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.OAuth2Request;
Expand Down Expand Up @@ -64,7 +64,7 @@ public void testScopesWithOr() throws Exception {
assertTrue((Boolean) expression.getValue(context));
}

@Test(expected = InsufficientScopeException.class)
@Test(expected = AccessDeniedException.class)
public void testScopesInsufficient() throws Exception {
AuthorizationRequest request = new AuthorizationRequest("foo", Collections.singleton("read"));
request.setResourceIdsAndAuthoritiesFromClientDetails(new BaseClientDetails("foo", "bar", "",
Expand Down Expand Up @@ -135,7 +135,7 @@ public void testScopesRegex() throws Exception {
assertTrue((Boolean) expression.getValue(context));
}

@Test(expected = InsufficientScopeException.class)
@Test(expected = AccessDeniedException.class)
public void testScopesRegexThrowsException() throws Exception {

OAuth2Request clientAuthentication = RequestTokenFactory.createOAuth2Request(null, "foo", null, false,
Expand Down
Expand Up @@ -22,10 +22,10 @@
import java.util.Collections;

import org.junit.Test;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.oauth2.common.exceptions.InsufficientScopeException;
import org.springframework.security.oauth2.provider.AuthorizationRequest;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.OAuth2Request;
Expand Down Expand Up @@ -73,7 +73,7 @@ public void testScopesFalse() throws Exception {
assertFalse(root.hasAnyScope("write"));
}

@Test(expected = InsufficientScopeException.class)
@Test(expected = AccessDeniedException.class)
public void testScopesWithException() throws Exception {
OAuth2Request clientAuthentication = RequestTokenFactory.createOAuth2Request(null, "foo", null, false, Collections.singleton("read"), null, null, null, null);

Expand All @@ -84,7 +84,7 @@ public void testScopesWithException() throws Exception {
assertFalse(root.throwOnError(hasAnyScope));
}

@Test(expected = InsufficientScopeException.class)
@Test(expected = AccessDeniedException.class)
public void testInsufficientScope() throws Exception {
OAuth2Request clientAuthentication = RequestTokenFactory.createOAuth2Request(null, "foo", null, false, Collections.singleton("read"), null, null, null, null);

Expand Down
Expand Up @@ -24,10 +24,10 @@
import org.junit.Test;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.oauth2.common.exceptions.InsufficientScopeException;
import org.springframework.security.oauth2.provider.AuthorizationRequest;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.OAuth2Request;
Expand Down Expand Up @@ -90,7 +90,7 @@ public void testScopes() throws Exception {
assertTrue((Boolean) expression.getValue(handler.createEvaluationContext(oAuth2Authentication, invocation)));
}

@Test(expected = InsufficientScopeException.class)
@Test(expected = AccessDeniedException.class)
public void testInsufficientScope() throws Exception {
AuthorizationRequest request = new AuthorizationRequest("foo", Collections.singleton("read"));
request.setResourceIdsAndAuthoritiesFromClientDetails(new BaseClientDetails("foo", "bar", "",
Expand Down

0 comments on commit 45a0964

Please sign in to comment.