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

github-actions
2024-11-22 22bf9bbfea68a3e71d80c31ecff64e6c9d460554
commit | author | age
7f7e0c 1 package org.keycloak.protocol.cas.mappers;
MP 2
89e3d7 3 import org.keycloak.models.*;
7f7e0c 4 import org.keycloak.protocol.ProtocolMapperUtils;
MP 5 import org.keycloak.protocol.oidc.mappers.OIDCAttributeMapperHelper;
6 import org.keycloak.provider.ProviderConfigProperty;
7
8 import java.util.ArrayList;
9 import java.util.List;
10 import java.util.Map;
11
12 public class UserPropertyMapper extends AbstractCASProtocolMapper {
13     private static final List<ProviderConfigProperty> configProperties = new ArrayList<ProviderConfigProperty>();
14
15     static {
16         ProviderConfigProperty property;
17         property = new ProviderConfigProperty();
18         property.setName(ProtocolMapperUtils.USER_ATTRIBUTE);
19         property.setLabel(ProtocolMapperUtils.USER_MODEL_PROPERTY_LABEL);
20         property.setType(ProviderConfigProperty.STRING_TYPE);
21         property.setHelpText(ProtocolMapperUtils.USER_MODEL_PROPERTY_HELP_TEXT);
22         configProperties.add(property);
23         OIDCAttributeMapperHelper.addTokenClaimNameConfig(configProperties);
24         OIDCAttributeMapperHelper.addJsonTypeConfig(configProperties);
25     }
26
27     public static final String PROVIDER_ID = "cas-usermodel-property-mapper";
28
29
30     @Override
31     public List<ProviderConfigProperty> getConfigProperties() {
32         return configProperties;
33     }
34
35     @Override
36     public String getId() {
37         return PROVIDER_ID;
38     }
39
40     @Override
41     public String getDisplayType() {
42         return "User Property";
43     }
44
45     @Override
46     public String getHelpText() {
47         return "Map a built in user property (email, firstName, lastName) to a token claim.";
48     }
49
513246 50     @Override
89e3d7 51     public void setAttribute(Map<String, Object> attributes, ProtocolMapperModel mappingModel, UserSessionModel userSession,
MP 52                              KeycloakSession session, ClientSessionContext clientSessionCt) {
513246 53         UserModel user = userSession.getUser();
MP 54         String propertyName = mappingModel.getConfig().get(ProtocolMapperUtils.USER_ATTRIBUTE);
55         String propertyValue = ProtocolMapperUtils.getUserModelValue(user, propertyName);
0ad1a9 56         setMappedAttribute(attributes, mappingModel, propertyValue);
513246 57     }
MP 58
7f7e0c 59     public static ProtocolMapperModel create(String name, String userAttribute,
b8d686 60                                              String tokenClaimName, String claimType) {
0ad1a9 61         ProtocolMapperModel mapper = CASAttributeMapperHelper.createClaimMapper(name, tokenClaimName,
b8d686 62                 claimType, PROVIDER_ID);
0ad1a9 63         mapper.getConfig().put(ProtocolMapperUtils.USER_ATTRIBUTE, userAttribute);
7f7e0c 64         return mapper;
MP 65     }
66 }