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

Matthias Piepkorn
2018-11-16 e85007fc30adfe7e1fbd72c9faa5e95bc226940a
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 UserSessionNoteMapper extends AbstractCASProtocolMapper {
14
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_SESSION_NOTE);
21         property.setLabel(ProtocolMapperUtils.USER_SESSION_MODEL_NOTE_LABEL);
22         property.setHelpText(ProtocolMapperUtils.USER_SESSION_MODEL_NOTE_HELP_TEXT);
23         property.setType(ProviderConfigProperty.STRING_TYPE);
24         configProperties.add(property);
25         OIDCAttributeMapperHelper.addTokenClaimNameConfig(configProperties);
26         OIDCAttributeMapperHelper.addJsonTypeConfig(configProperties);
27     }
28
29     public static final String PROVIDER_ID = "cas-usersessionmodel-note-mapper";
30
31
32     public List<ProviderConfigProperty> getConfigProperties() {
33         return configProperties;
34     }
35
36     @Override
37     public String getId() {
38         return PROVIDER_ID;
39     }
40
41     @Override
42     public String getDisplayType() {
43         return "User Session Note";
44     }
45
46     @Override
47     public String getDisplayCategory() {
48         return TOKEN_MAPPER_CATEGORY;
49     }
50
51     @Override
52     public String getHelpText() {
53         return "Map a custom user session note to a token claim.";
54     }
55
56     @Override
57     public void setAttribute(Map<String, Object> attributes, ProtocolMapperModel mappingModel, UserSessionModel userSession) {
58         String noteName = mappingModel.getConfig().get(ProtocolMapperUtils.USER_SESSION_NOTE);
59         String noteValue = userSession.getNote(noteName);
60         if (noteValue == null) return;
61         setMappedAttribute(attributes, mappingModel, noteValue);
62     }
63
64     public static ProtocolMapperModel create(String name,
65                                              String userSessionNote,
b8d686 66                                              String tokenClaimName, String jsonType) {
0ad1a9 67         ProtocolMapperModel mapper = CASAttributeMapperHelper.createClaimMapper(name, tokenClaimName,
b8d686 68                 jsonType, PROVIDER_ID);
0ad1a9 69         mapper.getConfig().put(ProtocolMapperUtils.USER_SESSION_NOTE, userSessionNote);
MP 70         return mapper;
71     }
72 }