use of org.keycloak.models.ProtocolMapperModel in project keycloak by keycloak.
the class ClientAdapter method entityToModel.
protected ProtocolMapperModel entityToModel(ProtocolMapperEntity entity) {
ProtocolMapperModel mapping = new ProtocolMapperModel();
mapping.setId(entity.getId());
mapping.setName(entity.getName());
mapping.setProtocol(entity.getProtocol());
mapping.setProtocolMapper(entity.getProtocolMapper());
Map<String, String> config = new HashMap<String, String>();
if (entity.getConfig() != null)
config.putAll(entity.getConfig());
mapping.setConfig(config);
return mapping;
}
use of org.keycloak.models.ProtocolMapperModel in project keycloak by keycloak.
the class ScriptBasedOIDCProtocolMapper method create.
public static ProtocolMapperModel create(String name, String userAttribute, String tokenClaimName, String claimType, boolean accessToken, boolean idToken, String script, boolean multiValued) {
ProtocolMapperModel mapper = OIDCAttributeMapperHelper.createClaimMapper(name, userAttribute, tokenClaimName, claimType, accessToken, idToken, PROVIDER_ID);
mapper.getConfig().put(SCRIPT, script);
mapper.getConfig().put(ProtocolMapperUtils.MULTIVALUED, String.valueOf(multiValued));
return mapper;
}
use of org.keycloak.models.ProtocolMapperModel in project keycloak by keycloak.
the class ScriptBasedOIDCProtocolMapper method evaluateScript.
private Object evaluateScript(Object tokenBinding, ProtocolMapperModel mappingModel, UserSessionModel userSession, KeycloakSession keycloakSession) {
UserModel user = userSession.getUser();
String scriptSource = getScriptCode(mappingModel);
RealmModel realm = userSession.getRealm();
ScriptingProvider scripting = keycloakSession.getProvider(ScriptingProvider.class);
ScriptModel scriptModel = scripting.createScript(realm.getId(), ScriptModel.TEXT_JAVASCRIPT, "token-mapper-script_" + mappingModel.getName(), scriptSource, null);
EvaluatableScriptAdapter script = scripting.prepareEvaluatableScript(scriptModel);
Object claimValue;
try {
claimValue = script.eval((bindings) -> {
bindings.put("user", user);
bindings.put("realm", realm);
if (tokenBinding instanceof IDToken) {
bindings.put("token", tokenBinding);
} else if (tokenBinding instanceof AccessTokenResponse) {
bindings.put("tokenResponse", tokenBinding);
}
bindings.put("userSession", userSession);
bindings.put("keycloakSession", keycloakSession);
});
} catch (Exception ex) {
LOGGER.error("Error during execution of ProtocolMapper script", ex);
claimValue = null;
}
return claimValue;
}
use of org.keycloak.models.ProtocolMapperModel in project keycloak by keycloak.
the class FullNameMapper method create.
public static ProtocolMapperModel create(String name, boolean accessToken, boolean idToken, boolean userInfo) {
ProtocolMapperModel mapper = new ProtocolMapperModel();
mapper.setName(name);
mapper.setProtocolMapper(PROVIDER_ID);
mapper.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);
Map<String, String> config = new HashMap<>();
if (accessToken)
config.put(OIDCAttributeMapperHelper.INCLUDE_IN_ACCESS_TOKEN, "true");
if (idToken)
config.put(OIDCAttributeMapperHelper.INCLUDE_IN_ID_TOKEN, "true");
if (userInfo)
config.put(OIDCAttributeMapperHelper.INCLUDE_IN_USERINFO, "true");
mapper.setConfig(config);
return mapper;
}
use of org.keycloak.models.ProtocolMapperModel in project keycloak by keycloak.
the class FullNameMapper method setClaim.
protected void setClaim(IDToken token, ProtocolMapperModel mappingModel, UserSessionModel userSession) {
UserModel user = userSession.getUser();
List<String> parts = new LinkedList<>();
Optional.ofNullable(user.getFirstName()).filter(s -> !s.isEmpty()).ifPresent(parts::add);
Optional.ofNullable(user.getLastName()).filter(s -> !s.isEmpty()).ifPresent(parts::add);
if (!parts.isEmpty()) {
token.getOtherClaims().put("name", String.join(" ", parts));
}
}
Aggregations