|
@@ -2,24 +2,22 @@ package org.hswebframework.web.oauth2.server.code;
|
|
|
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import org.hswebframework.web.authorization.Authentication;
|
|
|
-import org.hswebframework.web.authorization.Permission;
|
|
|
import org.hswebframework.web.id.IDGenerator;
|
|
|
import org.hswebframework.web.oauth2.ErrorType;
|
|
|
import org.hswebframework.web.oauth2.OAuth2Exception;
|
|
|
import org.hswebframework.web.oauth2.server.AccessToken;
|
|
|
import org.hswebframework.web.oauth2.server.AccessTokenManager;
|
|
|
import org.hswebframework.web.oauth2.server.OAuth2Client;
|
|
|
+import org.hswebframework.web.oauth2.server.ScopePredicate;
|
|
|
+import org.hswebframework.web.oauth2.server.utils.OAuth2ScopeUtils;
|
|
|
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
|
|
|
import org.springframework.data.redis.core.ReactiveRedisOperations;
|
|
|
import org.springframework.data.redis.core.ReactiveRedisTemplate;
|
|
|
import org.springframework.data.redis.serializer.RedisSerializationContext;
|
|
|
import org.springframework.data.redis.serializer.RedisSerializer;
|
|
|
-import org.springframework.util.StringUtils;
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
|
import java.time.Duration;
|
|
|
-import java.util.*;
|
|
|
-import java.util.function.BiPredicate;
|
|
|
|
|
|
@AllArgsConstructor
|
|
|
public class DefaultAuthorizationCodeGranter implements AuthorizationCodeGranter {
|
|
@@ -49,9 +47,10 @@ public class DefaultAuthorizationCodeGranter implements AuthorizationCodeGranter
|
|
|
request.getParameter("scope").map(String::valueOf).ifPresent(codeCache::setScope);
|
|
|
codeCache.setCode(code);
|
|
|
codeCache.setClientId(client.getClientId());
|
|
|
- codeCache.setAuthentication(authentication.copy(createPredicate(codeCache.getScope()), dimension -> true));
|
|
|
+ ScopePredicate permissionPredicate = OAuth2ScopeUtils.createScopePredicate(codeCache.getScope());
|
|
|
+
|
|
|
+ codeCache.setAuthentication(authentication.copy((permission, action) -> permissionPredicate.test(permission.getId(), action), dimension -> true));
|
|
|
|
|
|
- createPredicate(codeCache.getScope());
|
|
|
|
|
|
return redis
|
|
|
.opsForValue()
|
|
@@ -59,24 +58,6 @@ public class DefaultAuthorizationCodeGranter implements AuthorizationCodeGranter
|
|
|
.thenReturn(new AuthorizationCodeResponse(code));
|
|
|
}
|
|
|
|
|
|
- static BiPredicate<Permission, String> createPredicate(String scopeStr) {
|
|
|
- if (StringUtils.isEmpty(scopeStr)) {
|
|
|
- return ((permission, s) -> false);
|
|
|
- }
|
|
|
- String[] scopes = scopeStr.split("[ ,\n]");
|
|
|
- Map<String, Set<String>> actions = new HashMap<>();
|
|
|
- for (String scope : scopes) {
|
|
|
- String[] permissions = scope.split("[:]");
|
|
|
- String per = permissions[0];
|
|
|
- Set<String> acts = actions.computeIfAbsent(per, k -> new HashSet<>());
|
|
|
- acts.addAll(Arrays.asList(permissions).subList(1, permissions.length));
|
|
|
- }
|
|
|
-
|
|
|
- return ((permission, action) -> Optional
|
|
|
- .ofNullable(actions.get(permission.getId()))
|
|
|
- .map(acts -> acts.contains(action))
|
|
|
- .orElse(false));
|
|
|
- }
|
|
|
|
|
|
private String getRedisKey(String code) {
|
|
|
return "oauth2-code:" + code;
|