Skip to content

Commit a8bd950

Browse files
committed
优化scope判断
1 parent 0e2bc85 commit a8bd950

File tree

5 files changed

+82
-51
lines changed

5 files changed

+82
-51
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.hswebframework.web.oauth2.server;
2+
3+
import java.util.function.BiPredicate;
4+
5+
@FunctionalInterface
6+
public interface ScopePredicate extends BiPredicate<String, String[]> {
7+
8+
boolean test(String permission, String... actions);
9+
10+
}

hsweb-authorization/hsweb-authorization-oauth2/src/main/java/org/hswebframework/web/oauth2/server/code/DefaultAuthorizationCodeGranter.java

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,22 @@
22

33
import lombok.AllArgsConstructor;
44
import org.hswebframework.web.authorization.Authentication;
5-
import org.hswebframework.web.authorization.Permission;
65
import org.hswebframework.web.id.IDGenerator;
76
import org.hswebframework.web.oauth2.ErrorType;
87
import org.hswebframework.web.oauth2.OAuth2Exception;
98
import org.hswebframework.web.oauth2.server.AccessToken;
109
import org.hswebframework.web.oauth2.server.AccessTokenManager;
1110
import org.hswebframework.web.oauth2.server.OAuth2Client;
11+
import org.hswebframework.web.oauth2.server.ScopePredicate;
12+
import org.hswebframework.web.oauth2.server.utils.OAuth2ScopeUtils;
1213
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
1314
import org.springframework.data.redis.core.ReactiveRedisOperations;
1415
import org.springframework.data.redis.core.ReactiveRedisTemplate;
1516
import org.springframework.data.redis.serializer.RedisSerializationContext;
1617
import org.springframework.data.redis.serializer.RedisSerializer;
17-
import org.springframework.util.StringUtils;
1818
import reactor.core.publisher.Mono;
1919

2020
import java.time.Duration;
21-
import java.util.*;
22-
import java.util.function.BiPredicate;
2321

2422
@AllArgsConstructor
2523
public class DefaultAuthorizationCodeGranter implements AuthorizationCodeGranter {
@@ -49,34 +47,17 @@ public Mono<AuthorizationCodeResponse> requestCode(AuthorizationCodeRequest requ
4947
request.getParameter("scope").map(String::valueOf).ifPresent(codeCache::setScope);
5048
codeCache.setCode(code);
5149
codeCache.setClientId(client.getClientId());
52-
codeCache.setAuthentication(authentication.copy(createPredicate(codeCache.getScope()), dimension -> true));
50+
ScopePredicate permissionPredicate = OAuth2ScopeUtils.createScopePredicate(codeCache.getScope());
51+
52+
codeCache.setAuthentication(authentication.copy((permission, action) -> permissionPredicate.test(permission.getId(), action), dimension -> true));
5353

54-
createPredicate(codeCache.getScope());
5554

5655
return redis
5756
.opsForValue()
5857
.set(getRedisKey(code), codeCache, Duration.ofMinutes(5))
5958
.thenReturn(new AuthorizationCodeResponse(code));
6059
}
6160

62-
static BiPredicate<Permission, String> createPredicate(String scopeStr) {
63-
if (StringUtils.isEmpty(scopeStr)) {
64-
return ((permission, s) -> false);
65-
}
66-
String[] scopes = scopeStr.split("[ ,\n]");
67-
Map<String, Set<String>> actions = new HashMap<>();
68-
for (String scope : scopes) {
69-
String[] permissions = scope.split("[:]");
70-
String per = permissions[0];
71-
Set<String> acts = actions.computeIfAbsent(per, k -> new HashSet<>());
72-
acts.addAll(Arrays.asList(permissions).subList(1, permissions.length));
73-
}
74-
75-
return ((permission, action) -> Optional
76-
.ofNullable(actions.get(permission.getId()))
77-
.map(acts -> acts.contains(action))
78-
.orElse(false));
79-
}
8061

8162
private String getRedisKey(String code) {
8263
return "oauth2-code:" + code;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.hswebframework.web.oauth2.server.utils;
2+
3+
import org.hswebframework.web.oauth2.server.ScopePredicate;
4+
import org.springframework.util.StringUtils;
5+
6+
import java.util.*;
7+
8+
/**
9+
* @author zhouhao
10+
* @since 4.0.8
11+
*/
12+
public class OAuth2ScopeUtils {
13+
14+
public static ScopePredicate createScopePredicate(String scopeStr) {
15+
if (StringUtils.isEmpty(scopeStr)) {
16+
return ((permission, action) -> false);
17+
}
18+
String[] scopes = scopeStr.split("[ ,\n]");
19+
Map<String, Set<String>> actions = new HashMap<>();
20+
for (String scope : scopes) {
21+
String[] permissions = scope.split("[:]");
22+
String per = permissions[0];
23+
Set<String> acts = actions.computeIfAbsent(per, k -> new HashSet<>());
24+
acts.addAll(Arrays.asList(permissions).subList(1, permissions.length));
25+
}
26+
27+
return ((permission, action) -> Optional
28+
.ofNullable(actions.get(permission))
29+
.map(acts -> action.length == 0 || acts.containsAll(Arrays.asList(action)))
30+
.orElse(false));
31+
}
32+
}

hsweb-authorization/hsweb-authorization-oauth2/src/test/java/org/hswebframework/web/oauth2/server/code/DefaultAuthorizationCodeGranterTest.java

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,6 @@
1616

1717
public class DefaultAuthorizationCodeGranterTest {
1818

19-
20-
@Test
21-
public void testPermission() {
22-
BiPredicate<Permission, String> predicate = DefaultAuthorizationCodeGranter.createPredicate("user:info device:query");
23-
24-
{
25-
SimplePermission permission=new SimplePermission();
26-
permission.setId("user");
27-
permission.setActions(Collections.singleton("info"));
28-
29-
30-
assertTrue(predicate.test(permission,"info"));
31-
assertFalse(predicate.test(permission,"info2"));
32-
}
33-
34-
{
35-
SimplePermission permission=new SimplePermission();
36-
permission.setId("device");
37-
permission.setActions(Collections.singleton("query"));
38-
39-
40-
assertTrue(predicate.test(permission,"query"));
41-
assertFalse(predicate.test(permission,"query2"));
42-
}
43-
44-
}
45-
4619
@Test
4720
public void testRequestToken() {
4821

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.hswebframework.web.oauth2.server.utils;
2+
3+
import org.hswebframework.web.oauth2.server.ScopePredicate;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertFalse;
7+
import static org.junit.Assert.assertTrue;
8+
9+
public class OAuth2ScopeUtilsTest {
10+
11+
12+
@Test
13+
public void testEmpty() {
14+
ScopePredicate predicate = OAuth2ScopeUtils.createScopePredicate(null);
15+
assertFalse(predicate.test("basic"));
16+
}
17+
18+
@Test
19+
public void testScope() {
20+
ScopePredicate predicate = OAuth2ScopeUtils.createScopePredicate("basic user:info device:query");
21+
22+
assertTrue(predicate.test("basic"));
23+
{
24+
25+
assertTrue(predicate.test("user", "info"));
26+
assertFalse(predicate.test("user", "info2"));
27+
}
28+
29+
{
30+
assertTrue(predicate.test("device", "query"));
31+
assertFalse(predicate.test("device", "query2"));
32+
}
33+
34+
}
35+
}

0 commit comments

Comments
 (0)