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

Matthias Piepkorn
2017-07-26 635ded405fe61688f003e3560fb51c70a5f54960
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,
66                                              String tokenClaimName, String jsonType,
67                                              boolean consentRequired, String consentText) {
68         ProtocolMapperModel mapper = CASAttributeMapperHelper.createClaimMapper(name, tokenClaimName,
69                 jsonType, consentRequired, consentText, PROVIDER_ID);
70         mapper.getConfig().put(ProtocolMapperUtils.USER_SESSION_NOTE, userSessionNote);
71         return mapper;
72     }
73 }