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

Matthias Piepkorn
2017-11-14 8352fb1726e5efab21531bc0b15df784dc5ae0df
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;
7f7e0c 6 import org.keycloak.protocol.ProtocolMapperUtils;
MP 7 import org.keycloak.protocol.oidc.mappers.OIDCAttributeMapperHelper;
8 import org.keycloak.provider.ProviderConfigProperty;
9
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.Map;
13
14 public class UserPropertyMapper 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_PROPERTY_LABEL);
22         property.setType(ProviderConfigProperty.STRING_TYPE);
23         property.setHelpText(ProtocolMapperUtils.USER_MODEL_PROPERTY_HELP_TEXT);
24         configProperties.add(property);
25         OIDCAttributeMapperHelper.addTokenClaimNameConfig(configProperties);
26         OIDCAttributeMapperHelper.addJsonTypeConfig(configProperties);
27     }
28
29     public static final String PROVIDER_ID = "cas-usermodel-property-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 "User Property";
45     }
46
47     @Override
48     public String getHelpText() {
49         return "Map a built in user property (email, firstName, lastName) to a token claim.";
50     }
51
513246 52     @Override
MP 53     public void setAttribute(Map<String, Object> attributes, ProtocolMapperModel mappingModel, UserSessionModel userSession) {
54         UserModel user = userSession.getUser();
55         String propertyName = mappingModel.getConfig().get(ProtocolMapperUtils.USER_ATTRIBUTE);
56         String propertyValue = ProtocolMapperUtils.getUserModelValue(user, propertyName);
0ad1a9 57         setMappedAttribute(attributes, mappingModel, propertyValue);
513246 58     }
MP 59
7f7e0c 60     public static ProtocolMapperModel create(String name, String userAttribute,
MP 61                                              String tokenClaimName, String claimType,
62                                              boolean consentRequired, String consentText) {
0ad1a9 63         ProtocolMapperModel mapper = CASAttributeMapperHelper.createClaimMapper(name, tokenClaimName,
MP 64                 claimType, consentRequired, consentText, PROVIDER_ID);
65         mapper.getConfig().put(ProtocolMapperUtils.USER_ATTRIBUTE, userAttribute);
7f7e0c 66         return mapper;
MP 67     }
68 }