This repository has been archived by the owner on May 31, 2022. It is now read-only.
/
OAuth2MethodSecurityExpressionHandler.java
98 lines (88 loc) · 4.38 KB
/
OAuth2MethodSecurityExpressionHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package org.springframework.security.oauth2.provider.expression;
import java.util.List;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.AccessException;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.MethodExecutor;
import org.springframework.expression.MethodResolver;
import org.springframework.expression.TypedValue;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.security.access.expression.SecurityExpressionRoot;
import org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler;
import org.springframework.security.core.Authentication;
/**
* @author Ryan Heaton
* @author Dave Syer
*/
public class OAuth2MethodSecurityExpressionHandler extends DefaultMethodSecurityExpressionHandler {
@Override
public StandardEvaluationContext createEvaluationContextInternal(Authentication auth, MethodInvocation mi) {
StandardEvaluationContext ec = super.createEvaluationContextInternal(auth, mi);
ec.addMethodResolver(new OAuthMethodResolver());
return ec;
}
private static class OAuthMethodResolver implements MethodResolver {
public MethodExecutor resolve(EvaluationContext context, Object targetObject, String name,
List<TypeDescriptor> argumentTypes) throws AccessException {
if (targetObject instanceof SecurityExpressionRoot) {
if ("oauthClientHasRole".equals(name) || "oauthClientHasAnyRole".equals(name)) {
return new OAuthClientRoleExecutor();
}
else if ("oauthHasScope".equals(name) || "oauthHasAnyScope".equals(name)) {
return new OAuthScopeExecutor();
}
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;
}
}
private static class OAuthScopeExecutor implements MethodExecutor {
public TypedValue execute(EvaluationContext context, Object target, Object... arguments) throws AccessException {
String[] scopes = new String[arguments.length];
for (int i = 0; i < arguments.length; i++) {
scopes[i] = String.valueOf(arguments[i]);
}
return new TypedValue(OAuth2ExpressionUtils.hasAnyScope(
((SecurityExpressionRoot) target).getAuthentication(), scopes));
}
}
private static class OAuthClientRoleExecutor implements MethodExecutor {
public TypedValue execute(EvaluationContext context, Object target, Object... arguments) throws AccessException {
String[] roles = new String[arguments.length];
for (int i = 0; i < arguments.length; i++) {
roles[i] = String.valueOf(arguments[i]);
}
return new TypedValue(OAuth2ExpressionUtils.clientHasAnyRole(
((SecurityExpressionRoot) target).getAuthentication(), roles));
}
}
private static class DenyOAuthClientRoleExecutor implements MethodExecutor {
public TypedValue execute(EvaluationContext context, Object target, Object... arguments) throws AccessException {
return new TypedValue(!OAuth2ExpressionUtils.isOAuth(((SecurityExpressionRoot) target)
.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);
}
}
}