mirror of https://github.com/jacekkow/keycloak-protocol-cas

Updater Bot
2020-05-01 7224325967dae3bccb4a113f4a44da4b5965c56e
commit | author | age
7f7e0c 1 package org.keycloak.protocol.cas.mappers;
MP 2
89e3d7 3 import org.keycloak.models.*;
513246 4 import org.keycloak.models.utils.ModelToRepresentation;
7f7e0c 5 import org.keycloak.protocol.oidc.mappers.OIDCAttributeMapperHelper;
MP 6 import org.keycloak.provider.ProviderConfigProperty;
7
0ad1a9 8 import java.util.ArrayList;
MP 9 import java.util.LinkedList;
10 import java.util.List;
11 import java.util.Map;
7f7e0c 12
MP 13 public class GroupMembershipMapper extends AbstractCASProtocolMapper {
14     private static final List<ProviderConfigProperty> configProperties = new ArrayList<ProviderConfigProperty>();
15
0ad1a9 16     private static final String FULL_PATH = "full.path";
MP 17
7f7e0c 18     static {
MP 19         OIDCAttributeMapperHelper.addTokenClaimNameConfig(configProperties);
20         ProviderConfigProperty property1 = new ProviderConfigProperty();
0ad1a9 21         property1.setName(FULL_PATH);
7f7e0c 22         property1.setLabel("Full group path");
MP 23         property1.setType(ProviderConfigProperty.BOOLEAN_TYPE);
24         property1.setDefaultValue("true");
25         property1.setHelpText("Include full path to group i.e. /top/level1/level2, false will just specify the group name");
26         configProperties.add(property1);
27     }
28
29     public static final String PROVIDER_ID = "cas-group-membership-mapper";
30
31
32     @Override
33     public List<ProviderConfigProperty> getConfigProperties() {
34         return configProperties;
35     }
36
37     @Override
38     public String getId() {
39         return PROVIDER_ID;
40     }
41
42     @Override
43     public String getDisplayType() {
44         return "Group Membership";
45     }
46
47     @Override
48     public String getHelpText() {
49         return "Map user group membership";
50     }
51
513246 52     @Override
89e3d7 53     public void setAttribute(Map<String, Object> attributes, ProtocolMapperModel mappingModel, UserSessionModel userSession,
MP 54                              KeycloakSession session, ClientSessionContext clientSessionCt) {
513246 55         List<String> membership = new LinkedList<>();
MP 56         boolean fullPath = useFullPath(mappingModel);
57         for (GroupModel group : userSession.getUser().getGroups()) {
58             if (fullPath) {
59                 membership.add(ModelToRepresentation.buildGroupPath(group));
60             } else {
61                 membership.add(group.getName());
62             }
63         }
0ad1a9 64         setPlainAttribute(attributes, mappingModel, membership);
513246 65     }
MP 66
7f7e0c 67     public static boolean useFullPath(ProtocolMapperModel mappingModel) {
0ad1a9 68         return "true".equals(mappingModel.getConfig().get(FULL_PATH));
7f7e0c 69     }
MP 70
b8d686 71     public static ProtocolMapperModel create(String name, String tokenClaimName, boolean fullPath) {
0ad1a9 72         ProtocolMapperModel mapper = CASAttributeMapperHelper.createClaimMapper(name, tokenClaimName,
b8d686 73                 "String", PROVIDER_ID);
0ad1a9 74         mapper.getConfig().put(FULL_PATH, Boolean.toString(fullPath));
7f7e0c 75         return mapper;
MP 76     }
77 }