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

Matthias Piepkorn
2017-02-14 0862359ecd359b75048f4295fb79983a211a32e3
commit | author | age
0ad1a9 1 package org.keycloak.protocol.cas.mappers;
MP 2
3 import org.keycloak.models.ProtocolMapperModel;
4 import org.keycloak.models.UserSessionModel;
5 import org.keycloak.protocol.ProtocolMapperUtils;
6 import org.keycloak.protocol.oidc.mappers.OIDCAttributeMapperHelper;
7 import org.keycloak.provider.ProviderConfigProperty;
8
9 import java.util.ArrayList;
10 import java.util.List;
11 import java.util.Map;
12
13 public class UserRealmRoleMappingMapper extends AbstractUserRoleMappingMapper {
14     public static final String PROVIDER_ID = "cas-usermodel-realm-role-mapper";
15
16     private static final List<ProviderConfigProperty> CONFIG_PROPERTIES = new ArrayList<>();
17
18     static {
19
20         ProviderConfigProperty realmRolePrefix = new ProviderConfigProperty();
21         realmRolePrefix.setName(ProtocolMapperUtils.USER_MODEL_REALM_ROLE_MAPPING_ROLE_PREFIX);
22         realmRolePrefix.setLabel(ProtocolMapperUtils.USER_MODEL_REALM_ROLE_MAPPING_ROLE_PREFIX_LABEL);
23         realmRolePrefix.setHelpText(ProtocolMapperUtils.USER_MODEL_REALM_ROLE_MAPPING_ROLE_PREFIX_HELP_TEXT);
24         realmRolePrefix.setType(ProviderConfigProperty.STRING_TYPE);
25         CONFIG_PROPERTIES.add(realmRolePrefix);
26
27         OIDCAttributeMapperHelper.addTokenClaimNameConfig(CONFIG_PROPERTIES);
28     }
29
30     @Override
31     public List<ProviderConfigProperty> getConfigProperties() {
32         return CONFIG_PROPERTIES;
33     }
34
35     @Override
36     public String getId() {
37         return PROVIDER_ID;
38     }
39
40     @Override
41     public String getDisplayType() {
42         return "User Realm Role";
43     }
44
45     @Override
46     public String getDisplayCategory() {
47         return TOKEN_MAPPER_CATEGORY;
48     }
49
50     @Override
51     public String getHelpText() {
52         return "Map a user realm role to a token claim.";
53     }
54
55     @Override
56     public void setAttribute(Map<String, Object> attributes, ProtocolMapperModel mappingModel, UserSessionModel userSession) {
57         String rolePrefix = mappingModel.getConfig().get(ProtocolMapperUtils.USER_MODEL_REALM_ROLE_MAPPING_ROLE_PREFIX);
58         setAttribute(attributes, mappingModel, userSession, role -> ! role.isClientRole(), rolePrefix);
59     }
60
61     public static ProtocolMapperModel create(String realmRolePrefix, String name, String tokenClaimName) {
62         ProtocolMapperModel mapper = CASAttributeMapperHelper.createClaimMapper(name, tokenClaimName,
63                 "String", true, name, PROVIDER_ID);
64         mapper.getConfig().put(ProtocolMapperUtils.USER_MODEL_REALM_ROLE_MAPPING_ROLE_PREFIX, realmRolePrefix);
65         return mapper;
66     }
67 }