|
17 | 17 |
|
18 | 18 | package org.keycloak.protocol.cas.mappers;
|
19 | 19 |
|
20 |
| -import org.keycloak.models.*; |
21 |
| -import org.keycloak.models.utils.RoleUtils; |
| 20 | +import org.keycloak.models.ProtocolMapperModel; |
22 | 21 |
|
23 | 22 | import java.util.Map;
|
24 | 23 | import java.util.Set;
|
25 |
| -import java.util.function.Predicate; |
26 | 24 | import java.util.stream.Collectors;
|
27 |
| -import java.util.stream.Stream; |
28 | 25 |
|
29 | 26 | /**
|
30 | 27 | * Base class for mapping of user role mappings to an ID and Access Token claim.
|
|
33 | 30 | */
|
34 | 31 | abstract class AbstractUserRoleMappingMapper extends AbstractCASProtocolMapper {
|
35 | 32 |
|
36 |
| - /** |
37 |
| - * Returns a stream with roles that come from: |
38 |
| - * <ul> |
39 |
| - * <li>Direct assignment of the role to the user</li> |
40 |
| - * <li>Direct assignment of the role to any group of the user or any of its parent group</li> |
41 |
| - * <li>Composite roles are expanded recursively, the composite role itself is also contained in the returned stream</li> |
42 |
| - * </ul> |
43 |
| - * @param user User to enumerate the roles for |
44 |
| - */ |
45 |
| - public Stream<RoleModel> getAllUserRolesStream(UserModel user) { |
46 |
| - return Stream.concat( |
47 |
| - user.getRoleMappings().stream(), |
48 |
| - user.getGroups().stream() |
49 |
| - .flatMap(this::groupAndItsParentsStream) |
50 |
| - .flatMap(g -> g.getRoleMappings().stream())) |
51 |
| - .flatMap(RoleUtils::expandCompositeRolesStream); |
52 |
| - } |
53 |
| - |
54 |
| - /** |
55 |
| - * Returns stream of the given group and its parents (recursively). |
56 |
| - * @param group |
57 |
| - * @return |
58 |
| - */ |
59 |
| - private Stream<GroupModel> groupAndItsParentsStream(GroupModel group) { |
60 |
| - Stream.Builder<GroupModel> sb = Stream.builder(); |
61 |
| - while (group != null) { |
62 |
| - sb.add(group); |
63 |
| - group = group.getParent(); |
64 |
| - } |
65 |
| - return sb.build(); |
66 |
| - } |
67 |
| - |
68 | 33 | /**
|
69 | 34 | * Retrieves all roles of the current user based on direct roles set to the user, its groups and their parent groups.
|
70 | 35 | * Then it recursively expands all composite roles, and restricts according to the given predicate {@code restriction}.
|
71 | 36 | * If the current client sessions is restricted (i.e. no client found in active user session has full scope allowed),
|
72 | 37 | * the final list of roles is also restricted by the client scope. Finally, the list is mapped to the token into
|
73 | 38 | * a claim.
|
74 | 39 | */
|
75 |
| - protected void setAttribute(Map<String, Object> attributes, ProtocolMapperModel mappingModel, UserSessionModel userSession, |
76 |
| - Predicate<RoleModel> restriction, String prefix) { |
77 |
| - String rolePrefix = prefix == null ? "" : prefix; |
78 |
| - UserModel user = userSession.getUser(); |
79 |
| - |
80 |
| - // get a set of all realm roles assigned to the user or its group |
81 |
| - Stream<RoleModel> clientUserRoles = getAllUserRolesStream(user).filter(restriction); |
82 |
| - |
83 |
| - boolean dontLimitScope = userSession.getAuthenticatedClientSessions().values().stream().anyMatch(cs -> cs.getClient().isFullScopeAllowed()); |
84 |
| - if (! dontLimitScope) { |
85 |
| - Set<RoleModel> clientRoles = userSession.getAuthenticatedClientSessions().values().stream() |
86 |
| - .flatMap(cs -> cs.getClient().getScopeMappings().stream()) |
87 |
| - .collect(Collectors.toSet()); |
88 |
| - |
89 |
| - clientUserRoles = clientUserRoles.filter(clientRoles::contains); |
| 40 | + protected void setAttribute(Map<String, Object> attributes, ProtocolMapperModel mappingModel, Set<String> rolesToAdd, |
| 41 | + String prefix) { |
| 42 | + Set<String> realmRoleNames; |
| 43 | + if (prefix != null && !prefix.isEmpty()) { |
| 44 | + realmRoleNames = rolesToAdd.stream() |
| 45 | + .map(roleName -> prefix + roleName) |
| 46 | + .collect(Collectors.toSet()); |
| 47 | + } else { |
| 48 | + realmRoleNames = rolesToAdd; |
90 | 49 | }
|
91 | 50 |
|
92 |
| - Set<String> realmRoleNames = clientUserRoles |
93 |
| - .map(m -> rolePrefix + m.getName()) |
94 |
| - .collect(Collectors.toSet()); |
95 |
| - |
96 | 51 | setPlainAttribute(attributes, mappingModel, realmRoleNames);
|
97 | 52 | }
|
98 | 53 | }
|
0 commit comments