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

Matthias Piepkorn
2018-11-16 e85007fc30adfe7e1fbd72c9faa5e95bc226940a
commit | author | age
7f7e0c 1 package org.keycloak.protocol.cas.mappers;
MP 2
3 import org.keycloak.models.ProtocolMapperModel;
513246 4 import org.keycloak.models.UserModel;
MP 5 import org.keycloak.models.UserSessionModel;
6 import org.keycloak.models.utils.KeycloakModelUtils;
7f7e0c 7 import org.keycloak.protocol.ProtocolMapperUtils;
MP 8 import org.keycloak.protocol.oidc.mappers.OIDCAttributeMapperHelper;
9 import org.keycloak.provider.ProviderConfigProperty;
10
11 import java.util.ArrayList;
e85007 12 import java.util.Collection;
7f7e0c 13 import java.util.List;
MP 14 import java.util.Map;
15
16 public class UserAttributeMapper extends AbstractCASProtocolMapper {
17     private static final List<ProviderConfigProperty> configProperties = new ArrayList<ProviderConfigProperty>();
18
19     static {
20         ProviderConfigProperty property;
21         property = new ProviderConfigProperty();
22         property.setName(ProtocolMapperUtils.USER_ATTRIBUTE);
23         property.setLabel(ProtocolMapperUtils.USER_MODEL_ATTRIBUTE_LABEL);
24         property.setHelpText(ProtocolMapperUtils.USER_MODEL_ATTRIBUTE_HELP_TEXT);
25         property.setType(ProviderConfigProperty.STRING_TYPE);
26         configProperties.add(property);
27         OIDCAttributeMapperHelper.addTokenClaimNameConfig(configProperties);
28         OIDCAttributeMapperHelper.addJsonTypeConfig(configProperties);
29
30         property = new ProviderConfigProperty();
31         property.setName(ProtocolMapperUtils.MULTIVALUED);
32         property.setLabel(ProtocolMapperUtils.MULTIVALUED_LABEL);
33         property.setHelpText(ProtocolMapperUtils.MULTIVALUED_HELP_TEXT);
34         property.setType(ProviderConfigProperty.BOOLEAN_TYPE);
35         configProperties.add(property);
36
e85007 37         property = new ProviderConfigProperty();
MP 38         property.setName(ProtocolMapperUtils.AGGREGATE_ATTRS);
39         property.setLabel(ProtocolMapperUtils.AGGREGATE_ATTRS_LABEL);
40         property.setHelpText(ProtocolMapperUtils.AGGREGATE_ATTRS_HELP_TEXT);
41         property.setType(ProviderConfigProperty.BOOLEAN_TYPE);
42         configProperties.add(property);
7f7e0c 43     }
MP 44
45     public static final String PROVIDER_ID = "cas-usermodel-attribute-mapper";
46
47
48     @Override
49     public List<ProviderConfigProperty> getConfigProperties() {
50         return configProperties;
51     }
52
53     @Override
54     public String getId() {
55         return PROVIDER_ID;
56     }
57
58     @Override
59     public String getDisplayType() {
60         return "User Attribute";
61     }
62
63     @Override
64     public String getHelpText() {
65         return "Map a custom user attribute to a token claim.";
66     }
67
513246 68     @Override
MP 69     public void setAttribute(Map<String, Object> attributes, ProtocolMapperModel mappingModel, UserSessionModel userSession) {
70         UserModel user = userSession.getUser();
71         String attributeName = mappingModel.getConfig().get(ProtocolMapperUtils.USER_ATTRIBUTE);
e85007 72         boolean aggregateAttrs = Boolean.valueOf(mappingModel.getConfig().get(ProtocolMapperUtils.AGGREGATE_ATTRS));
MP 73         Collection<String> attributeValue = KeycloakModelUtils.resolveAttribute(user, attributeName, aggregateAttrs);
0ad1a9 74         setMappedAttribute(attributes, mappingModel, attributeValue);
513246 75     }
MP 76
7f7e0c 77     public static ProtocolMapperModel create(String name, String userAttribute,
MP 78                                              String tokenClaimName, String claimType,
b8d686 79                                              boolean multivalued) {
e85007 80         return create(name, userAttribute, tokenClaimName, claimType, multivalued, false);
MP 81     }
82
83     public static ProtocolMapperModel create(String name, String userAttribute,
84                                              String tokenClaimName, String claimType,
85                                              boolean multivalued, boolean aggregateAttrs) {
0ad1a9 86         ProtocolMapperModel mapper = CASAttributeMapperHelper.createClaimMapper(name, tokenClaimName,
b8d686 87                 claimType, PROVIDER_ID);
0ad1a9 88         mapper.getConfig().put(ProtocolMapperUtils.USER_ATTRIBUTE, userAttribute);
7f7e0c 89         if (multivalued) {
MP 90             mapper.getConfig().put(ProtocolMapperUtils.MULTIVALUED, "true");
91         }
e85007 92         if (aggregateAttrs) {
MP 93             mapper.getConfig().put(ProtocolMapperUtils.AGGREGATE_ATTRS, "true");
94         }
7f7e0c 95         return mapper;
MP 96     }
97 }