use of org.apache.syncope.core.provisioning.api.IntAttrName in project syncope by apache.
the class MappingManagerImpl method prepareAttr.
/**
* Prepare an attribute to be sent to a connector instance.
*
* @param provision external resource
* @param mapItem mapping item for the given attribute
* @param any given any object
* @param password clear-text password
* @return connObjectKey + prepared attribute
*/
private Pair<String, Attribute> prepareAttr(final Provision provision, final Item mapItem, final Any<?> any, final String password) {
IntAttrName intAttrName;
try {
intAttrName = intAttrNameParser.parse(mapItem.getIntAttrName(), provision.getAnyType().getKind());
} catch (ParseException e) {
LOG.error("Invalid intAttrName '{}' specified, ignoring", mapItem.getIntAttrName(), e);
return null;
}
boolean readOnlyVirSchema = false;
Schema schema = null;
AttrSchemaType schemaType = AttrSchemaType.String;
if (intAttrName.getSchemaType() != null) {
switch(intAttrName.getSchemaType()) {
case PLAIN:
schema = plainSchemaDAO.find(intAttrName.getSchemaName());
if (schema != null) {
schemaType = schema.getType();
}
break;
case VIRTUAL:
schema = virSchemaDAO.find(intAttrName.getSchemaName());
readOnlyVirSchema = (schema != null && schema.isReadonly());
break;
default:
}
}
List<PlainAttrValue> values = getIntValues(provision, mapItem, intAttrName, any);
LOG.debug("Define mapping for: " + "\n* ExtAttrName " + mapItem.getExtAttrName() + "\n* is connObjectKey " + mapItem.isConnObjectKey() + "\n* is password " + mapItem.isPassword() + "\n* mandatory condition " + mapItem.getMandatoryCondition() + "\n* Schema " + intAttrName.getSchemaName() + "\n* ClassType " + schemaType.getType().getName() + "\n* Values " + values);
Pair<String, Attribute> result;
if (readOnlyVirSchema) {
result = null;
} else {
List<Object> objValues = new ArrayList<>();
for (PlainAttrValue value : values) {
if (FrameworkUtil.isSupportedAttributeType(schemaType.getType())) {
objValues.add(value.getValue());
} else {
objValues.add(value.getValueAsString(schemaType));
}
}
if (mapItem.isConnObjectKey()) {
result = Pair.of(objValues.isEmpty() ? null : objValues.iterator().next().toString(), null);
} else if (mapItem.isPassword() && any instanceof User) {
String passwordAttrValue = password;
if (StringUtils.isBlank(passwordAttrValue)) {
User user = (User) any;
if (user.canDecodePassword()) {
try {
passwordAttrValue = ENCRYPTOR.decode(user.getPassword(), user.getCipherAlgorithm());
} catch (Exception e) {
LOG.error("Could not decode password for {}", user, e);
}
} else if (provision.getResource().isRandomPwdIfNotProvided()) {
try {
passwordAttrValue = passwordGenerator.generate(provision.getResource());
} catch (InvalidPasswordRuleConf e) {
LOG.error("Could not generate policy-compliant random password for {}", user, e);
}
}
}
if (passwordAttrValue == null) {
result = null;
} else {
result = Pair.of(null, AttributeBuilder.buildPassword(passwordAttrValue.toCharArray()));
}
} else if (schema != null && schema.isMultivalue()) {
result = Pair.of(null, AttributeBuilder.build(mapItem.getExtAttrName(), objValues));
} else {
result = Pair.of(null, objValues.isEmpty() ? AttributeBuilder.build(mapItem.getExtAttrName()) : AttributeBuilder.build(mapItem.getExtAttrName(), objValues.iterator().next()));
}
}
return result;
}
use of org.apache.syncope.core.provisioning.api.IntAttrName in project syncope by apache.
the class MappingManagerImpl method setIntValues.
@Transactional(readOnly = true)
@Override
public void setIntValues(final Item mapItem, final Attribute attr, final AnyTO anyTO, final AnyUtils anyUtils) {
List<Object> values = null;
if (attr != null) {
values = attr.getValue();
for (ItemTransformer transformer : MappingUtils.getItemTransformers(mapItem)) {
values = transformer.beforePull(mapItem, anyTO, values);
}
}
values = values == null ? Collections.emptyList() : values;
IntAttrName intAttrName;
try {
intAttrName = intAttrNameParser.parse(mapItem.getIntAttrName(), anyUtils.getAnyTypeKind());
} catch (ParseException e) {
LOG.error("Invalid intAttrName '{}' specified, ignoring", mapItem.getIntAttrName(), e);
return;
}
if (intAttrName.getField() != null) {
switch(intAttrName.getField()) {
case "password":
if (anyTO instanceof UserTO && !values.isEmpty()) {
((UserTO) anyTO).setPassword(ConnObjectUtils.getPassword(values.get(0)));
}
break;
case "username":
if (anyTO instanceof UserTO) {
((UserTO) anyTO).setUsername(values.isEmpty() || values.get(0) == null ? null : values.get(0).toString());
}
break;
case "name":
if (anyTO instanceof GroupTO) {
((GroupTO) anyTO).setName(values.isEmpty() || values.get(0) == null ? null : values.get(0).toString());
} else if (anyTO instanceof AnyObjectTO) {
((AnyObjectTO) anyTO).setName(values.isEmpty() || values.get(0) == null ? null : values.get(0).toString());
}
break;
case "mustChangePassword":
if (anyTO instanceof UserTO && !values.isEmpty() && values.get(0) != null) {
((UserTO) anyTO).setMustChangePassword(BooleanUtils.toBoolean(values.get(0).toString()));
}
break;
case "userOwner":
case "groupOwner":
if (anyTO instanceof GroupTO && attr != null) {
// using a special attribute (with schema "", that will be ignored) for carrying the
// GroupOwnerSchema value
AttrTO attrTO = new AttrTO();
attrTO.setSchema(StringUtils.EMPTY);
if (values.isEmpty() || values.get(0) == null) {
attrTO.getValues().add(StringUtils.EMPTY);
} else {
attrTO.getValues().add(values.get(0).toString());
}
((GroupTO) anyTO).getPlainAttrs().add(attrTO);
}
break;
default:
}
} else if (intAttrName.getSchemaType() != null) {
GroupableRelatableTO groupableTO = null;
Group group = null;
if (anyTO instanceof GroupableRelatableTO && intAttrName.getMembershipOfGroup() != null) {
groupableTO = (GroupableRelatableTO) anyTO;
group = groupDAO.findByName(intAttrName.getMembershipOfGroup());
}
switch(intAttrName.getSchemaType()) {
case PLAIN:
AttrTO attrTO = new AttrTO();
attrTO.setSchema(intAttrName.getSchemaName());
PlainSchema schema = plainSchemaDAO.find(intAttrName.getSchemaName());
for (Object value : values) {
AttrSchemaType schemaType = schema == null ? AttrSchemaType.String : schema.getType();
if (value != null) {
PlainAttrValue attrValue = anyUtils.newPlainAttrValue();
switch(schemaType) {
case String:
attrValue.setStringValue(value.toString());
break;
case Binary:
attrValue.setBinaryValue((byte[]) value);
break;
default:
try {
attrValue.parseValue(schema, value.toString());
} catch (ParsingValidationException e) {
LOG.error("While parsing provided value {}", value, e);
attrValue.setStringValue(value.toString());
schemaType = AttrSchemaType.String;
}
break;
}
attrTO.getValues().add(attrValue.getValueAsString(schemaType));
}
}
if (groupableTO == null || group == null) {
anyTO.getPlainAttrs().add(attrTO);
} else {
Optional<MembershipTO> membership = groupableTO.getMembership(group.getKey());
if (!membership.isPresent()) {
membership = Optional.of(new MembershipTO.Builder().group(group.getKey(), group.getName()).build());
groupableTO.getMemberships().add(membership.get());
}
membership.get().getPlainAttrs().add(attrTO);
}
break;
case DERIVED:
attrTO = new AttrTO();
attrTO.setSchema(intAttrName.getSchemaName());
if (groupableTO == null || group == null) {
anyTO.getDerAttrs().add(attrTO);
} else {
Optional<MembershipTO> membership = groupableTO.getMembership(group.getKey());
if (!membership.isPresent()) {
membership = Optional.of(new MembershipTO.Builder().group(group.getKey(), group.getName()).build());
groupableTO.getMemberships().add(membership.get());
}
membership.get().getDerAttrs().add(attrTO);
}
break;
case VIRTUAL:
attrTO = new AttrTO();
attrTO.setSchema(intAttrName.getSchemaName());
// virtual attributes don't get transformed, iterate over original attr.getValue()
if (attr != null && attr.getValue() != null && !attr.getValue().isEmpty()) {
attr.getValue().stream().filter(value -> value != null).forEachOrdered(value -> attrTO.getValues().add(value.toString()));
}
if (groupableTO == null || group == null) {
anyTO.getVirAttrs().add(attrTO);
} else {
Optional<MembershipTO> membership = groupableTO.getMembership(group.getKey());
if (!membership.isPresent()) {
membership = Optional.of(new MembershipTO.Builder().group(group.getKey(), group.getName()).build());
groupableTO.getMemberships().add(membership.get());
}
membership.get().getVirAttrs().add(attrTO);
}
break;
default:
}
}
}
use of org.apache.syncope.core.provisioning.api.IntAttrName in project syncope by apache.
the class ResourceDataBinderImpl method populateMapping.
private void populateMapping(final MappingTO mappingTO, final Mapping mapping, final AnyTypeClassTO allowedSchemas) {
mapping.setConnObjectLink(mappingTO.getConnObjectLink());
SyncopeClientCompositeException scce = SyncopeClientException.buildComposite();
SyncopeClientException invalidMapping = SyncopeClientException.build(ClientExceptionType.InvalidMapping);
SyncopeClientException requiredValuesMissing = SyncopeClientException.build(ClientExceptionType.RequiredValuesMissing);
for (ItemTO itemTO : mappingTO.getItems()) {
if (itemTO == null) {
LOG.error("Null {}", ItemTO.class.getSimpleName());
invalidMapping.getElements().add("Null " + ItemTO.class.getSimpleName());
} else if (itemTO.getIntAttrName() == null) {
requiredValuesMissing.getElements().add("intAttrName");
scce.addException(requiredValuesMissing);
} else {
IntAttrName intAttrName = null;
try {
intAttrName = intAttrNameParser.parse(itemTO.getIntAttrName(), mapping.getProvision().getAnyType().getKind());
} catch (ParseException e) {
LOG.error("Invalid intAttrName '{}'", itemTO.getIntAttrName(), e);
}
if (intAttrName == null || intAttrName.getSchemaType() == null && intAttrName.getField() == null && intAttrName.getPrivilegesOfApplication() == null) {
LOG.error("'{}' not existing", itemTO.getIntAttrName());
invalidMapping.getElements().add("'" + itemTO.getIntAttrName() + "' not existing");
} else {
boolean allowed = true;
if (intAttrName.getSchemaType() != null && intAttrName.getEnclosingGroup() == null && intAttrName.getRelatedAnyObject() == null && intAttrName.getPrivilegesOfApplication() == null) {
switch(intAttrName.getSchemaType()) {
case PLAIN:
allowed = allowedSchemas.getPlainSchemas().contains(intAttrName.getSchemaName());
break;
case DERIVED:
allowed = allowedSchemas.getDerSchemas().contains(intAttrName.getSchemaName());
break;
case VIRTUAL:
allowed = allowedSchemas.getVirSchemas().contains(intAttrName.getSchemaName());
break;
default:
}
}
if (allowed) {
// no mandatory condition implies mandatory condition false
if (!JexlUtils.isExpressionValid(itemTO.getMandatoryCondition() == null ? "false" : itemTO.getMandatoryCondition())) {
SyncopeClientException invalidMandatoryCondition = SyncopeClientException.build(ClientExceptionType.InvalidValues);
invalidMandatoryCondition.getElements().add(itemTO.getMandatoryCondition());
scce.addException(invalidMandatoryCondition);
}
MappingItem item = entityFactory.newEntity(MappingItem.class);
BeanUtils.copyProperties(itemTO, item, ITEM_IGNORE_PROPERTIES);
item.setMapping(mapping);
if (item.isConnObjectKey()) {
if (intAttrName.getSchemaType() == SchemaType.VIRTUAL) {
invalidMapping.getElements().add("Virtual attributes cannot be set as ConnObjectKey");
}
if ("password".equals(intAttrName.getField())) {
invalidMapping.getElements().add("Password attributes cannot be set as ConnObjectKey");
}
mapping.setConnObjectKeyItem(item);
} else {
mapping.add(item);
}
itemTO.getTransformers().forEach(transformerKey -> {
Implementation transformer = implementationDAO.find(transformerKey);
if (transformer == null) {
LOG.debug("Invalid " + Implementation.class.getSimpleName() + " {}, ignoring...", transformerKey);
} else {
item.add(transformer);
}
});
// remove all implementations not contained in the TO
item.getTransformers().removeIf(implementation -> !itemTO.getTransformers().contains(implementation.getKey()));
if (intAttrName.getEnclosingGroup() != null && item.getPurpose() != MappingPurpose.PROPAGATION) {
invalidMapping.getElements().add("Only " + MappingPurpose.PROPAGATION.name() + " allowed when referring to groups");
}
if (intAttrName.getRelatedAnyObject() != null && item.getPurpose() != MappingPurpose.PROPAGATION) {
invalidMapping.getElements().add("Only " + MappingPurpose.PROPAGATION.name() + " allowed when referring to any objects");
}
if (intAttrName.getPrivilegesOfApplication() != null && item.getPurpose() != MappingPurpose.PROPAGATION) {
invalidMapping.getElements().add("Only " + MappingPurpose.PROPAGATION.name() + " allowed when referring to privileges");
}
if (intAttrName.getSchemaType() == SchemaType.DERIVED && item.getPurpose() != MappingPurpose.PROPAGATION) {
invalidMapping.getElements().add("Only " + MappingPurpose.PROPAGATION.name() + " allowed for derived");
}
if (intAttrName.getSchemaType() == SchemaType.VIRTUAL) {
if (item.getPurpose() != MappingPurpose.PROPAGATION) {
invalidMapping.getElements().add("Only " + MappingPurpose.PROPAGATION.name() + " allowed for virtual");
}
VirSchema schema = virSchemaDAO.find(item.getIntAttrName());
if (schema != null && schema.getProvision().equals(item.getMapping().getProvision())) {
invalidMapping.getElements().add("No need to map virtual schema on linking resource");
}
}
} else {
LOG.error("'{}' not allowed", itemTO.getIntAttrName());
invalidMapping.getElements().add("'" + itemTO.getIntAttrName() + "' not allowed");
}
}
}
}
if (!invalidMapping.getElements().isEmpty()) {
scce.addException(invalidMapping);
}
if (scce.hasExceptions()) {
throw scce;
}
}
use of org.apache.syncope.core.provisioning.api.IntAttrName in project syncope by apache.
the class SAML2UserManager method findMatchingUser.
@Transactional(readOnly = true)
public List<String> findMatchingUser(final String keyValue, final String idpKey) {
List<String> result = new ArrayList<>();
SAML2IdP idp = idpDAO.find(idpKey);
if (idp == null) {
LOG.warn("Invalid IdP: {}", idpKey);
return result;
}
String transformed = keyValue;
for (ItemTransformer transformer : MappingUtils.getItemTransformers(idp.getConnObjectKeyItem().get())) {
List<Object> output = transformer.beforePull(null, null, Collections.<Object>singletonList(transformed));
if (output != null && !output.isEmpty()) {
transformed = output.get(0).toString();
}
}
IntAttrName intAttrName;
try {
intAttrName = intAttrNameParser.parse(idp.getConnObjectKeyItem().get().getIntAttrName(), AnyTypeKind.USER);
} catch (ParseException e) {
LOG.error("Invalid intAttrName '{}' specified, ignoring", idp.getConnObjectKeyItem().get().getIntAttrName(), e);
return result;
}
if (intAttrName.getField() != null) {
switch(intAttrName.getField()) {
case "key":
User byKey = userDAO.find(transformed);
if (byKey != null) {
result.add(byKey.getUsername());
}
break;
case "username":
User byUsername = userDAO.findByUsername(transformed);
if (byUsername != null) {
result.add(byUsername.getUsername());
}
break;
default:
}
} else if (intAttrName.getSchemaType() != null) {
switch(intAttrName.getSchemaType()) {
case PLAIN:
PlainAttrValue value = entityFactory.newEntity(UPlainAttrValue.class);
PlainSchema schema = plainSchemaDAO.find(intAttrName.getSchemaName());
if (schema == null) {
value.setStringValue(transformed);
} else {
try {
value.parseValue(schema, transformed);
} catch (ParsingValidationException e) {
LOG.error("While parsing provided key value {}", transformed, e);
value.setStringValue(transformed);
}
}
result.addAll(userDAO.findByPlainAttrValue(intAttrName.getSchemaName(), value).stream().map(user -> user.getUsername()).collect(Collectors.toList()));
break;
case DERIVED:
result.addAll(userDAO.findByDerAttrValue(intAttrName.getSchemaName(), transformed).stream().map(user -> user.getUsername()).collect(Collectors.toList()));
break;
default:
}
}
return result;
}
use of org.apache.syncope.core.provisioning.api.IntAttrName in project syncope by apache.
the class SAML2UserManager method fill.
public void fill(final String idpKey, final SAML2LoginResponseTO responseTO, final UserTO userTO) {
SAML2IdP idp = idpDAO.find(idpKey);
if (idp == null) {
LOG.warn("Invalid IdP: {}", idpKey);
return;
}
idp.getItems().forEach(item -> {
List<String> values = Collections.emptyList();
Optional<AttrTO> samlAttr = responseTO.getAttr(item.getExtAttrName());
if (samlAttr.isPresent() && !samlAttr.get().getValues().isEmpty()) {
values = samlAttr.get().getValues();
List<Object> transformed = new ArrayList<>(values);
for (ItemTransformer transformer : MappingUtils.getItemTransformers(item)) {
transformed = transformer.beforePull(null, userTO, transformed);
}
values.clear();
for (Object value : transformed) {
values.add(value.toString());
}
}
IntAttrName intAttrName = null;
try {
intAttrName = intAttrNameParser.parse(item.getIntAttrName(), AnyTypeKind.USER);
} catch (ParseException e) {
LOG.error("Invalid intAttrName '{}' specified, ignoring", item.getIntAttrName(), e);
}
if (intAttrName != null && intAttrName.getField() != null) {
switch(intAttrName.getField()) {
case "username":
if (!values.isEmpty()) {
userTO.setUsername(values.get(0));
}
break;
default:
LOG.warn("Unsupported: {}", intAttrName.getField());
}
} else if (intAttrName != null && intAttrName.getSchemaType() != null) {
switch(intAttrName.getSchemaType()) {
case PLAIN:
Optional<AttrTO> attr = userTO.getPlainAttr(intAttrName.getSchemaName());
if (!attr.isPresent()) {
attr = Optional.of(new AttrTO.Builder().schema(intAttrName.getSchemaName()).build());
userTO.getPlainAttrs().add(attr.get());
} else {
attr.get().getValues().clear();
}
attr.get().getValues().addAll(values);
break;
default:
LOG.warn("Unsupported: {} {}", intAttrName.getSchemaType(), intAttrName.getSchemaName());
}
}
});
}
Aggregations