commit | author | age
|
7f7e0c
|
1 |
package org.keycloak.protocol.cas.mappers; |
MP |
2 |
|
|
3 |
import org.keycloak.models.ProtocolMapperModel; |
513246
|
4 |
import org.keycloak.models.UserModel; |
MP |
5 |
import org.keycloak.models.UserSessionModel; |
|
6 |
import org.keycloak.models.utils.KeycloakModelUtils; |
7f7e0c
|
7 |
import org.keycloak.protocol.ProtocolMapperUtils; |
MP |
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 UserAttributeMapper extends AbstractCASProtocolMapper { |
|
16 |
private static final List<ProviderConfigProperty> configProperties = new ArrayList<ProviderConfigProperty>(); |
|
17 |
|
|
18 |
static { |
|
19 |
ProviderConfigProperty property; |
|
20 |
property = new ProviderConfigProperty(); |
|
21 |
property.setName(ProtocolMapperUtils.USER_ATTRIBUTE); |
|
22 |
property.setLabel(ProtocolMapperUtils.USER_MODEL_ATTRIBUTE_LABEL); |
|
23 |
property.setHelpText(ProtocolMapperUtils.USER_MODEL_ATTRIBUTE_HELP_TEXT); |
|
24 |
property.setType(ProviderConfigProperty.STRING_TYPE); |
|
25 |
configProperties.add(property); |
|
26 |
OIDCAttributeMapperHelper.addTokenClaimNameConfig(configProperties); |
|
27 |
OIDCAttributeMapperHelper.addJsonTypeConfig(configProperties); |
|
28 |
|
|
29 |
property = new ProviderConfigProperty(); |
|
30 |
property.setName(ProtocolMapperUtils.MULTIVALUED); |
|
31 |
property.setLabel(ProtocolMapperUtils.MULTIVALUED_LABEL); |
|
32 |
property.setHelpText(ProtocolMapperUtils.MULTIVALUED_HELP_TEXT); |
|
33 |
property.setType(ProviderConfigProperty.BOOLEAN_TYPE); |
|
34 |
configProperties.add(property); |
|
35 |
|
|
36 |
} |
|
37 |
|
|
38 |
public static final String PROVIDER_ID = "cas-usermodel-attribute-mapper"; |
|
39 |
|
|
40 |
|
|
41 |
@Override |
|
42 |
public List<ProviderConfigProperty> getConfigProperties() { |
|
43 |
return configProperties; |
|
44 |
} |
|
45 |
|
|
46 |
@Override |
|
47 |
public String getId() { |
|
48 |
return PROVIDER_ID; |
|
49 |
} |
|
50 |
|
|
51 |
@Override |
|
52 |
public String getDisplayType() { |
|
53 |
return "User Attribute"; |
|
54 |
} |
|
55 |
|
|
56 |
@Override |
|
57 |
public String getHelpText() { |
|
58 |
return "Map a custom user attribute to a token claim."; |
|
59 |
} |
|
60 |
|
513246
|
61 |
@Override |
MP |
62 |
public void setAttribute(Map<String, Object> attributes, ProtocolMapperModel mappingModel, UserSessionModel userSession) { |
|
63 |
UserModel user = userSession.getUser(); |
|
64 |
String attributeName = mappingModel.getConfig().get(ProtocolMapperUtils.USER_ATTRIBUTE); |
|
65 |
List<String> attributeValue = KeycloakModelUtils.resolveAttribute(user, attributeName); |
0ad1a9
|
66 |
setMappedAttribute(attributes, mappingModel, attributeValue); |
513246
|
67 |
} |
MP |
68 |
|
7f7e0c
|
69 |
public static ProtocolMapperModel create(String name, String userAttribute, |
MP |
70 |
String tokenClaimName, String claimType, |
b8d686
|
71 |
boolean multivalued) { |
0ad1a9
|
72 |
ProtocolMapperModel mapper = CASAttributeMapperHelper.createClaimMapper(name, tokenClaimName, |
b8d686
|
73 |
claimType, PROVIDER_ID); |
0ad1a9
|
74 |
mapper.getConfig().put(ProtocolMapperUtils.USER_ATTRIBUTE, userAttribute); |
7f7e0c
|
75 |
if (multivalued) { |
MP |
76 |
mapper.getConfig().put(ProtocolMapperUtils.MULTIVALUED, "true"); |
|
77 |
} |
|
78 |
return mapper; |
|
79 |
} |
|
80 |
} |