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

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