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

Commit

Permalink
Add method expression support for OauthIsUser and OauthIsClient to ma…
Browse files Browse the repository at this point in the history
…tch web expression handler
  • Loading branch information
exell-christopher authored and dsyer committed Apr 29, 2012
1 parent 795b28e commit 57456de
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
Expand Up @@ -40,6 +40,12 @@ else if ("oauthHasScope".equals(name) || "oauthHasAnyScope".equals(name)) {
else if ("denyOAuthClient".equals(name)) {
return new DenyOAuthClientRoleExecutor();
}
else if(("oauthIsClient").equals(name)) {
return new OauthIsClientExecutor();
}
else if(("oauthIsUser").equals(name)) {
return new OauthIsUserExecutor();
}
}

return null;
Expand Down Expand Up @@ -74,4 +80,19 @@ public TypedValue execute(EvaluationContext context, Object target, Object... ar
.getAuthentication()));
}
}
private static class OauthIsClientExecutor implements MethodExecutor {
public TypedValue execute(EvaluationContext context, Object target, Object... arguments) throws AccessException {
boolean is_client_auth = OAuth2ExpressionUtils.isOAuthClientAuth(((SecurityExpressionRoot) target)
.getAuthentication());
return new TypedValue(is_client_auth);
}
}

private static class OauthIsUserExecutor implements MethodExecutor {
public TypedValue execute(EvaluationContext context, Object target, Object... arguments) throws AccessException {
boolean is_user_auth = OAuth2ExpressionUtils.isOAuthUserAuth(((SecurityExpressionRoot) target)
.getAuthentication());
return new TypedValue(is_user_auth);
}
}
}
Expand Up @@ -90,5 +90,74 @@ public void testStandardSecurityRoot() throws Exception {
Expression expression = handler.getExpressionParser().parseExpression("isAuthenticated()");
assertTrue((Boolean) expression.getValue(context));
}

@Test
public void testOauthIsClient() throws Exception {
AuthorizationRequest clientAuthentication =
new AuthorizationRequest("foo", Collections.singleton("read"),
Collections.<GrantedAuthority> singleton(new SimpleGrantedAuthority(
"ROLE_CLIENT")),
Collections.singleton("bar"));
Authentication userAuthentication = null;
OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(clientAuthentication, userAuthentication);
MethodInvocation invocation = new SimpleMethodInvocation(this, ReflectionUtils.findMethod(getClass(),
"testOauthIsClient"));
EvaluationContext context = handler.createEvaluationContext(oAuth2Authentication, invocation);
Expression expression = handler.getExpressionParser().parseExpression("oauthIsClient()");
assertTrue((Boolean) expression.getValue(context));
}

@Test
public void testOauthIsClientUserAuth() throws Exception {
AuthorizationRequest clientAuthentication =
new AuthorizationRequest("foo", Collections.singleton("read"),
Collections.<GrantedAuthority> singleton(new SimpleGrantedAuthority(
"ROLE_CLIENT")),
Collections.singleton("bar"));
Authentication userAuthentication =
new UsernamePasswordAuthenticationToken("foobar","foobar",
Collections.<GrantedAuthority> singleton(new SimpleGrantedAuthority("ROLE_USER")));
OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(clientAuthentication, userAuthentication);
MethodInvocation invocation = new SimpleMethodInvocation(this, ReflectionUtils.findMethod(getClass(),
"testOauthIsClientUserAuth"));
EvaluationContext context = handler.createEvaluationContext(oAuth2Authentication, invocation);
Expression expression = handler.getExpressionParser().parseExpression("oauthIsClient()");
assertFalse((Boolean) expression.getValue(context));
}

@Test
public void testOauthIsUser() throws Exception {
AuthorizationRequest clientAuthentication =
new AuthorizationRequest("foo", Collections.singleton("read"),
Collections.<GrantedAuthority> singleton(new SimpleGrantedAuthority(
"ROLE_CLIENT")),
Collections.singleton("bar"));
Authentication userAuthentication =
new UsernamePasswordAuthenticationToken("foobar","foobar",
Collections.<GrantedAuthority> singleton(new SimpleGrantedAuthority("ROLE_USER")));
OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(clientAuthentication, userAuthentication);
MethodInvocation invocation = new SimpleMethodInvocation(this, ReflectionUtils.findMethod(getClass(),
"testOauthIsUser"));
EvaluationContext context = handler.createEvaluationContext(oAuth2Authentication, invocation);
Expression expression = handler.getExpressionParser().parseExpression("oauthIsUser()");
assertTrue((Boolean) expression.getValue(context));
}

@Test
public void testOauthIsUserClientAuth() throws Exception {
AuthorizationRequest clientAuthentication =
new AuthorizationRequest("foo", Collections.singleton("read"),
Collections.<GrantedAuthority> singleton(new SimpleGrantedAuthority(
"ROLE_CLIENT")), Collections.singleton("bar"));
Authentication userAuthentication = null;
OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(clientAuthentication, userAuthentication);
MethodInvocation invocation = new SimpleMethodInvocation(this, ReflectionUtils.findMethod(getClass(),
"testOauthIsUserClientAuth"));
EvaluationContext context = handler.createEvaluationContext(oAuth2Authentication, invocation);
Expression expression = handler.getExpressionParser().parseExpression("oauthIsUser()");
assertFalse((Boolean) expression.getValue(context));
}



}

0 comments on commit 57456de

Please sign in to comment.