This method attaches one or many attributes to the passed attribute statement. To obtain the attribute values, it executes the mapper's script and returns attaches the returned value to the attribute. If the returned attribute is an Array or is iterable, the mapper will either return multiple attributes, or an attribute with multiple values. The variant chosen depends on the configuration of the mapper
112 UserModel user = userSession.getUser();
113 String scriptSource = mappingModel.getConfig().get(ProviderConfigProperty.SCRIPT_TYPE);
114 RealmModel realm = userSession.getRealm();
117 boolean singleAttribute = Boolean.parseBoolean(single);
119 ScriptingProvider scripting = session.getProvider(ScriptingProvider.class);
120 ScriptModel scriptModel = scripting.createScript(realm.getId(), ScriptModel.TEXT_JAVASCRIPT,
"attribute-mapper-script_" + mappingModel.getName(), scriptSource, null);
122 EvaluatableScriptAdapter script = scripting.prepareEvaluatableScript(scriptModel);
123 Object attributeValue;
125 attributeValue = script.eval((bindings) -> {
126 bindings.put(
"user", user);
127 bindings.put(
"realm", realm);
128 bindings.put(
"clientSession", clientSession);
129 bindings.put(
"userSession", userSession);
130 bindings.put(
"keycloakSession", session);
133 if (attributeValue.getClass().isArray()){
134 attributeValue = Arrays.asList((Object[])attributeValue);
136 if (attributeValue instanceof Iterable) {
137 if (singleAttribute) {
138 AttributeType singleAttributeType = AttributeStatementHelper.createAttributeType(mappingModel);
139 attributeStatement.addAttribute(
new AttributeStatementType.ASTChoiceType(singleAttributeType));
140 for (Object value : (Iterable)attributeValue) {
141 singleAttributeType.addAttributeValue(value);
144 for (Object value : (Iterable)attributeValue) {
145 AttributeStatementHelper.addAttribute(attributeStatement, mappingModel, value.toString());
150 AttributeStatementHelper.addAttribute(attributeStatement, mappingModel, attributeValue.toString());
152 }
catch (Exception ex) {
153 LOGGER.error(
"Error during execution of ProtocolMapper script", ex);
154 AttributeStatementHelper.addAttribute(attributeStatement, mappingModel, null);
static final Logger LOGGER
Definition: ScriptBasedMapper.java:28
static final String SINGLE_VALUE_ATTRIBUTE
Definition: ScriptBasedMapper.java:27