org.apereo.cas.syncope.SyncopeUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cas-server-support-syncope-authentication Show documentation
Show all versions of cas-server-support-syncope-authentication Show documentation
cas-server-support-syncope-authentication
package org.apereo.cas.syncope;
import org.apereo.cas.authentication.credential.UsernamePasswordCredential;
import org.apereo.cas.authentication.principal.Principal;
import org.apereo.cas.configuration.model.support.syncope.BaseSyncopeSearchProperties;
import org.apereo.cas.util.CollectionUtils;
import org.apereo.cas.util.EncodingUtils;
import org.apereo.cas.util.function.FunctionUtils;
import org.apereo.cas.util.http.HttpExecutionRequest;
import org.apereo.cas.util.http.HttpUtils;
import org.apereo.cas.util.serialization.JacksonObjectMapperFactory;
import org.apereo.cas.util.spring.SpringExpressionLanguageValueResolver;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.commons.lang3.StringUtils;
import org.apache.hc.core5.http.HttpEntityContainer;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
/**
* This is {@link SyncopeUtils}.
*
* @author Francesco Chicchiriccò
* @since 6.5.0
*/
@UtilityClass
@Slf4j
public class SyncopeUtils {
private static final ObjectMapper MAPPER =
JacksonObjectMapperFactory.builder().defaultTypingEnabled(false).build().toObjectMapper();
/**
* Convert user as a JSON node into a map of details.
*
* @param user the user
* @param attributeMappings the attribute mappings
* @return the map
*/
public static Map> convertFromUserEntity(final JsonNode user,
final Map attributeMappings) {
val attributes = new HashMap>();
if (user.has("securityQuestion") && !user.get("securityQuestion").isNull()) {
var name = attributeMappings.getOrDefault("securityQuestion", "syncopeUserSecurityQuestion");
attributes.put(name, CollectionUtils.wrapList(user.get("securityQuestion").asText()));
}
var name = attributeMappings.getOrDefault("key", "syncopeUserKey");
attributes.put(name, CollectionUtils.wrapList(user.get("key").asText()));
name = attributeMappings.getOrDefault("username", "username");
attributes.put(name, CollectionUtils.wrapList(user.get("username").asText()));
name = attributeMappings.getOrDefault("status", "syncopeUserStatus");
attributes.put(name, CollectionUtils.wrapList(user.get("status").asText()));
name = attributeMappings.getOrDefault("realm", "syncopeUserRealm");
attributes.put(name, CollectionUtils.wrapList(user.get("realm").asText()));
name = attributeMappings.getOrDefault("creator", "syncopeUserCreator");
attributes.put(name, CollectionUtils.wrapList(user.get("creator").asText()));
name = attributeMappings.getOrDefault("creationDate", "syncopeUserCreationDate");
attributes.put(name, CollectionUtils.wrapList(user.get("creationDate").asText()));
if (user.has("changePwdDate") && !user.get("changePwdDate").isNull()) {
name = attributeMappings.getOrDefault("changePwdDate", "syncopeUserChangePwdDate");
attributes.put(name, CollectionUtils.wrapList(user.get("changePwdDate").asText()));
}
if (user.has("lastLoginDate") && !user.get("lastLoginDate").isNull()) {
name = attributeMappings.getOrDefault("lastLoginDate", "syncopeUserLastLoginDate");
attributes.put(name, CollectionUtils.wrapList(user.get("lastLoginDate").asText()));
}
collectListableAttribute(attributes, user, "roles", "syncopeUserRoles", attributeMappings);
collectListableAttribute(attributes, user, "dynRoles", "syncopeUserDynRoles", attributeMappings);
collectListableAttribute(attributes, user, "dynRealms", "syncopeUserDynRealms", attributeMappings);
if (user.has("memberships")) {
val memberships = new ArrayList<>();
user.get("memberships").forEach(member -> memberships.add(member.get("groupName").asText()));
if (!memberships.isEmpty()) {
name = attributeMappings.getOrDefault("memberships", "syncopeUserMemberships");
attributes.put(name, memberships);
}
}
if (user.has("dynMemberships")) {
val dynMemberships = new ArrayList<>();
user.get("dynMemberships").forEach(m -> dynMemberships.add(m.get("groupName").asText()));
if (!dynMemberships.isEmpty()) {
name = attributeMappings.getOrDefault("dynMemberships", "syncopeUserDynMemberships");
attributes.put(name, dynMemberships);
}
}
if (user.has("relationships")) {
val relationships = new ArrayList<>();
user.get("relationships").forEach(
r -> relationships.add(r.get("type").asText() + ';' + r.get("otherEndName").asText()));
if (!relationships.isEmpty()) {
name = attributeMappings.getOrDefault("relationships", "syncopeUserRelationships");
attributes.put(name, relationships);
}
}
mapSyncopeUserAttributes(user, "plainAttrs", attributeMappings, attributes);
mapSyncopeUserAttributes(user, "derAttrs", attributeMappings, attributes);
mapSyncopeUserAttributes(user, "virAttrs", attributeMappings, attributes);
return attributes;
}
private void mapSyncopeUserAttributes(final JsonNode user, final String attributeName,
final Map attributeMappings,
final Map> attributes) {
if (user.has(attributeName)) {
val prefix = attributeMappings.getOrDefault(attributeName, "syncopeUserAttr_");
user.get(attributeName).forEach(attr -> {
val attrName = prefix + attr.get("schema").asText();
attributes.put(
attributeMappings.getOrDefault(attrName, attrName),
MAPPER.convertValue(attr.get("values"), ArrayList.class));
});
}
}
private void collectListableAttribute(final Map> attributes,
final JsonNode user, final String syncopeAttribute,
final String casAttribute,
final Map attributeMappings) {
val values = user.has(syncopeAttribute)
? MAPPER.convertValue(user.get(syncopeAttribute), ArrayList.class)
: CollectionUtils.wrapList();
if (!values.isEmpty()) {
val name = attributeMappings.getOrDefault(syncopeAttribute, casAttribute);
attributes.put(name, values);
}
}
/**
* Syncope search.
*
* @param properties the properties
* @param user the user
* @return the optional
*/
public static List