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

Laurent Meunier
2024-06-11 b92028de86e6b25a87c12ff34ae00338a455308d
commit | author | age
0ad1a9 1 package org.keycloak.protocol.cas.mappers;
MP 2
89e3d7 3 import org.keycloak.models.ClientSessionContext;
MP 4 import org.keycloak.models.KeycloakSession;
0ad1a9 5 import org.keycloak.models.ProtocolMapperModel;
MP 6 import org.keycloak.models.UserSessionModel;
7 import org.keycloak.protocol.ProtocolMapperUtils;
8 import org.keycloak.protocol.oidc.mappers.OIDCAttributeMapperHelper;
9 import org.keycloak.provider.ProviderConfigProperty;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Map;
14
15 public class UserSessionNoteMapper extends AbstractCASProtocolMapper {
16
17     private static final List<ProviderConfigProperty> configProperties = new ArrayList<ProviderConfigProperty>();
18
19     static {
20         ProviderConfigProperty property;
21         property = new ProviderConfigProperty();
22         property.setName(ProtocolMapperUtils.USER_SESSION_NOTE);
23         property.setLabel(ProtocolMapperUtils.USER_SESSION_MODEL_NOTE_LABEL);
24         property.setHelpText(ProtocolMapperUtils.USER_SESSION_MODEL_NOTE_HELP_TEXT);
25         property.setType(ProviderConfigProperty.STRING_TYPE);
26         configProperties.add(property);
27         OIDCAttributeMapperHelper.addTokenClaimNameConfig(configProperties);
28         OIDCAttributeMapperHelper.addJsonTypeConfig(configProperties);
29     }
30
31     public static final String PROVIDER_ID = "cas-usersessionmodel-note-mapper";
32
33
34     public List<ProviderConfigProperty> getConfigProperties() {
35         return configProperties;
36     }
37
38     @Override
39     public String getId() {
40         return PROVIDER_ID;
41     }
42
43     @Override
44     public String getDisplayType() {
45         return "User Session Note";
46     }
47
48     @Override
49     public String getDisplayCategory() {
50         return TOKEN_MAPPER_CATEGORY;
51     }
52
53     @Override
54     public String getHelpText() {
55         return "Map a custom user session note to a token claim.";
56     }
57
58     @Override
89e3d7 59     public void setAttribute(Map<String, Object> attributes, ProtocolMapperModel mappingModel, UserSessionModel userSession,
MP 60                              KeycloakSession session, ClientSessionContext clientSessionCt) {
0ad1a9 61         String noteName = mappingModel.getConfig().get(ProtocolMapperUtils.USER_SESSION_NOTE);
MP 62         String noteValue = userSession.getNote(noteName);
63         if (noteValue == null) return;
64         setMappedAttribute(attributes, mappingModel, noteValue);
65     }
66
67     public static ProtocolMapperModel create(String name,
68                                              String userSessionNote,
b8d686 69                                              String tokenClaimName, String jsonType) {
0ad1a9 70         ProtocolMapperModel mapper = CASAttributeMapperHelper.createClaimMapper(name, tokenClaimName,
b8d686 71                 jsonType, PROVIDER_ID);
0ad1a9 72         mapper.getConfig().put(ProtocolMapperUtils.USER_SESSION_NOTE, userSessionNote);
MP 73         return mapper;
74     }
75 }