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