-
Notifications
You must be signed in to change notification settings - Fork 0
/
HelloWorldMapper.java
94 lines (79 loc) · 3.93 KB
/
HelloWorldMapper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package hamburg.schwartau;
import org.keycloak.models.ClientSessionContext;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.ProtocolMapperModel;
import org.keycloak.models.UserSessionModel;
import org.keycloak.protocol.ProtocolMapperUtils;
import org.keycloak.protocol.oidc.mappers.AbstractOIDCProtocolMapper;
import org.keycloak.protocol.oidc.mappers.OIDCAccessTokenMapper;
import org.keycloak.protocol.oidc.mappers.OIDCAttributeMapperHelper;
import org.keycloak.protocol.oidc.mappers.OIDCIDTokenMapper;
import org.keycloak.protocol.oidc.mappers.UserInfoTokenMapper;
import org.keycloak.provider.ProviderConfigProperty;
import org.keycloak.representations.IDToken;
import java.util.ArrayList;
import java.util.List;
/*
* Our own example protocol mapper.
*/
public class HelloWorldMapper extends AbstractOIDCProtocolMapper implements OIDCAccessTokenMapper, OIDCIDTokenMapper, UserInfoTokenMapper {
/*
* A config which keycloak uses to display a generic dialog to configure the token.
*/
private static final List<ProviderConfigProperty> configProperties = new ArrayList<>();
/*
* The ID of the token mapper. Is public, because we need this id in our data-setup project to
* configure the protocol mapper in keycloak.
*/
public static final String PROVIDER_ID = "oidc-hello-world-mapper";
static {
// The builtin protocol mapper let the user define under which claim name (key)
// the protocol mapper writes its value. To display this option in the generic dialog
// in keycloak, execute the following method.
OIDCAttributeMapperHelper.addTokenClaimNameConfig(configProperties);
// The builtin protocol mapper let the user define for which tokens the protocol mapper
// is executed (access token, id token, user info). To add the config options for the different types
// to the dialog execute the following method. Note that the following method uses the interfaces
// this token mapper implements to decide which options to add to the config. So if this token
// mapper should never be available for some sort of options, e.g. like the id token, just don't
// implement the corresponding interface.
OIDCAttributeMapperHelper.addIncludeInTokensConfig(configProperties, HelloWorldMapper.class);
//configProperties.set(0
//mappingModel.getConfig().get(ProtocolMapperUtils.MULTIVALUED));
//OIDCAttributeMapperHelper.addJsonTypeConfig(configProperties);
}
@Override
public String getDisplayCategory() {
return "Token mapper";
}
@Override
public String getDisplayType() {
return "Hello World Mapper";
}
@Override
public String getHelpText() {
return "Adds a hello world text to the claim";
}
@Override
public List<ProviderConfigProperty> getConfigProperties() {
return configProperties;
}
@Override
public String getId() {
return PROVIDER_ID;
}
@Override
protected void setClaim(final IDToken token,
final ProtocolMapperModel mappingModel,
final UserSessionModel userSession,
final KeycloakSession keycloakSession,
final ClientSessionContext clientSessionCtx) {
// adds our data to the token. Uses the parameters like the claim name which were set by the user
// when this protocol mapper was configured in keycloak. Note that the parameters which can
// be configured in keycloak for this protocol mapper were set in the static intializer of this class.
//
// Sets a static "Hello world" string, but we could write a dynamic value like a group attribute here too.
OIDCAttributeMapperHelper.mapClaim(token, mappingModel, clientSessionCtx.getScopeString());
//OIDCAttributeMapperHelper.mapClaim(token, mappingModel, "Hello World" );
}
}