use of org.apache.syncope.core.persistence.api.entity.SAML2IdP in project syncope by apache.
the class SAML2IdPLogic method update.
@PreAuthorize("hasRole('" + SAML2SPEntitlement.IDP_UPDATE + "')")
public void update(final SAML2IdPTO saml2IdpTO) {
check();
SAML2IdP saml2Idp = idpDAO.find(saml2IdpTO.getKey());
if (saml2Idp == null) {
throw new NotFoundException("SAML 2.0 IdP '" + saml2IdpTO.getKey() + "'");
}
SAML2IdPEntity idpEntity = cache.get(saml2Idp.getEntityID());
if (idpEntity == null) {
try {
idpEntity = cache.put(saml2Idp);
} catch (Exception e) {
LOG.error("Unexpected error while updating {}", saml2Idp.getEntityID(), e);
SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidEntity);
sce.getElements().add(e.getMessage());
throw sce;
}
}
if (idpEntity.getSSOLocation(saml2IdpTO.getBindingType()) == null) {
SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidEntity);
sce.getElements().add(saml2IdpTO.getBindingType() + " not supported by " + saml2Idp.getEntityID());
throw sce;
}
saml2Idp = idpDAO.save(binder.update(saml2Idp, saml2IdpTO));
idpEntity.setIdpTO(binder.getIdPTO(saml2Idp));
}
use of org.apache.syncope.core.persistence.api.entity.SAML2IdP 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.persistence.api.entity.SAML2IdP 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());
}
}
});
}
use of org.apache.syncope.core.persistence.api.entity.SAML2IdP in project syncope by apache.
the class JPASAML2IdPDAO method findByEntityID.
@Transactional(readOnly = true)
@Override
public SAML2IdP findByEntityID(final String entityID) {
TypedQuery<SAML2IdP> query = entityManager().createQuery("SELECT e FROM " + JPASAML2IdP.class.getSimpleName() + " e WHERE e.entityID = :entityID", SAML2IdP.class);
query.setParameter("entityID", entityID);
SAML2IdP result = null;
try {
result = query.getSingleResult();
} catch (NoResultException e) {
LOG.debug("No IdP found with entityID {}", entityID, e);
}
return result;
}
Aggregations