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.cas.CASLoginProtocol; |
|
9 |
import org.keycloak.protocol.oidc.mappers.OIDCAttributeMapperHelper; |
|
10 |
import org.keycloak.provider.ProviderConfigProperty; |
|
11 |
|
|
12 |
import java.util.ArrayList; |
|
13 |
import java.util.HashMap; |
|
14 |
import java.util.List; |
|
15 |
import java.util.Map; |
|
16 |
|
|
17 |
import static org.keycloak.protocol.oidc.mappers.OIDCAttributeMapperHelper.JSON_TYPE; |
|
18 |
import static org.keycloak.protocol.oidc.mappers.OIDCAttributeMapperHelper.TOKEN_CLAIM_NAME; |
|
19 |
|
|
20 |
public class UserAttributeMapper extends AbstractCASProtocolMapper { |
|
21 |
private static final List<ProviderConfigProperty> configProperties = new ArrayList<ProviderConfigProperty>(); |
|
22 |
|
|
23 |
static { |
|
24 |
ProviderConfigProperty property; |
|
25 |
property = new ProviderConfigProperty(); |
|
26 |
property.setName(ProtocolMapperUtils.USER_ATTRIBUTE); |
|
27 |
property.setLabel(ProtocolMapperUtils.USER_MODEL_ATTRIBUTE_LABEL); |
|
28 |
property.setHelpText(ProtocolMapperUtils.USER_MODEL_ATTRIBUTE_HELP_TEXT); |
|
29 |
property.setType(ProviderConfigProperty.STRING_TYPE); |
|
30 |
configProperties.add(property); |
|
31 |
OIDCAttributeMapperHelper.addTokenClaimNameConfig(configProperties); |
|
32 |
OIDCAttributeMapperHelper.addJsonTypeConfig(configProperties); |
|
33 |
|
|
34 |
property = new ProviderConfigProperty(); |
|
35 |
property.setName(ProtocolMapperUtils.MULTIVALUED); |
|
36 |
property.setLabel(ProtocolMapperUtils.MULTIVALUED_LABEL); |
|
37 |
property.setHelpText(ProtocolMapperUtils.MULTIVALUED_HELP_TEXT); |
|
38 |
property.setType(ProviderConfigProperty.BOOLEAN_TYPE); |
|
39 |
configProperties.add(property); |
|
40 |
|
|
41 |
} |
|
42 |
|
|
43 |
public static final String PROVIDER_ID = "cas-usermodel-attribute-mapper"; |
|
44 |
|
|
45 |
|
|
46 |
@Override |
|
47 |
public List<ProviderConfigProperty> getConfigProperties() { |
|
48 |
return configProperties; |
|
49 |
} |
|
50 |
|
|
51 |
@Override |
|
52 |
public String getId() { |
|
53 |
return PROVIDER_ID; |
|
54 |
} |
|
55 |
|
|
56 |
@Override |
|
57 |
public String getDisplayType() { |
|
58 |
return "User Attribute"; |
|
59 |
} |
|
60 |
|
|
61 |
@Override |
|
62 |
public String getHelpText() { |
|
63 |
return "Map a custom user attribute to a token claim."; |
|
64 |
} |
|
65 |
|
513246
|
66 |
@Override |
MP |
67 |
public void setAttribute(Map<String, Object> attributes, ProtocolMapperModel mappingModel, UserSessionModel userSession) { |
|
68 |
UserModel user = userSession.getUser(); |
|
69 |
String protocolClaim = mappingModel.getConfig().get(TOKEN_CLAIM_NAME); |
|
70 |
if (protocolClaim == null) { |
|
71 |
return; |
|
72 |
} |
|
73 |
String attributeName = mappingModel.getConfig().get(ProtocolMapperUtils.USER_ATTRIBUTE); |
|
74 |
List<String> attributeValue = KeycloakModelUtils.resolveAttribute(user, attributeName); |
|
75 |
if (attributeValue == null) return; |
|
76 |
attributes.put(protocolClaim, OIDCAttributeMapperHelper.mapAttributeValue(mappingModel, attributeValue)); |
|
77 |
} |
|
78 |
|
7f7e0c
|
79 |
public static ProtocolMapperModel create(String name, String userAttribute, |
MP |
80 |
String tokenClaimName, String claimType, |
|
81 |
boolean consentRequired, String consentText, boolean multivalued) { |
|
82 |
ProtocolMapperModel mapper = new ProtocolMapperModel(); |
|
83 |
mapper.setName(name); |
|
84 |
mapper.setProtocolMapper(PROVIDER_ID); |
|
85 |
mapper.setProtocol(CASLoginProtocol.LOGIN_PROTOCOL); |
|
86 |
mapper.setConsentRequired(consentRequired); |
|
87 |
mapper.setConsentText(consentText); |
|
88 |
Map<String, String> config = new HashMap<String, String>(); |
|
89 |
config.put(ProtocolMapperUtils.USER_ATTRIBUTE, userAttribute); |
|
90 |
config.put(TOKEN_CLAIM_NAME, tokenClaimName); |
|
91 |
config.put(JSON_TYPE, claimType); |
|
92 |
if (multivalued) { |
|
93 |
mapper.getConfig().put(ProtocolMapperUtils.MULTIVALUED, "true"); |
|
94 |
} |
|
95 |
mapper.setConfig(config); |
|
96 |
return mapper; |
|
97 |
} |
|
98 |
} |