Search in sources :

Example 6 with SAML2IdP

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));
}
Also used : SAML2IdP(org.apache.syncope.core.persistence.api.entity.SAML2IdP) SAML2IdPEntity(org.apache.syncope.core.logic.saml2.SAML2IdPEntity) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 7 with 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;
}
Also used : ItemTransformer(org.apache.syncope.core.provisioning.api.data.ItemTransformer) AttrTO(org.apache.syncope.common.lib.to.AttrTO) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) SerializationUtils(org.apache.commons.lang3.SerializationUtils) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) UserPatch(org.apache.syncope.common.lib.patch.UserPatch) SAML2IdP(org.apache.syncope.core.persistence.api.entity.SAML2IdP) ArrayList(java.util.ArrayList) AnyTypeKind(org.apache.syncope.common.lib.types.AnyTypeKind) UPlainAttrValue(org.apache.syncope.core.persistence.api.entity.user.UPlainAttrValue) Pair(org.apache.commons.lang3.tuple.Pair) Propagation(org.springframework.transaction.annotation.Propagation) UserDataBinder(org.apache.syncope.core.provisioning.api.data.UserDataBinder) SAML2LoginResponseTO(org.apache.syncope.common.lib.to.SAML2LoginResponseTO) PropagationStatus(org.apache.syncope.common.lib.to.PropagationStatus) SAML2IdPDAO(org.apache.syncope.core.persistence.api.dao.SAML2IdPDAO) ParseException(java.text.ParseException) TemplateUtils(org.apache.syncope.core.provisioning.java.utils.TemplateUtils) SyncopeConstants(org.apache.syncope.common.lib.SyncopeConstants) Logger(org.slf4j.Logger) UserDAO(org.apache.syncope.core.persistence.api.dao.UserDAO) PlainSchemaDAO(org.apache.syncope.core.persistence.api.dao.PlainSchemaDAO) User(org.apache.syncope.core.persistence.api.entity.user.User) PlainAttrValue(org.apache.syncope.core.persistence.api.entity.PlainAttrValue) Collectors(java.util.stream.Collectors) MappingUtils(org.apache.syncope.core.provisioning.java.utils.MappingUtils) EntityFactory(org.apache.syncope.core.persistence.api.entity.EntityFactory) List(java.util.List) Component(org.springframework.stereotype.Component) SAML2IdPActions(org.apache.syncope.core.provisioning.api.SAML2IdPActions) UserProvisioningManager(org.apache.syncope.core.provisioning.api.UserProvisioningManager) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) Optional(java.util.Optional) IntAttrNameParser(org.apache.syncope.core.provisioning.java.IntAttrNameParser) UserTO(org.apache.syncope.common.lib.to.UserTO) ApplicationContextProvider(org.apache.syncope.core.spring.ApplicationContextProvider) Collections(java.util.Collections) AnyOperations(org.apache.syncope.common.lib.AnyOperations) ParsingValidationException(org.apache.syncope.core.persistence.api.attrvalue.validation.ParsingValidationException) IntAttrName(org.apache.syncope.core.provisioning.api.IntAttrName) Transactional(org.springframework.transaction.annotation.Transactional) UPlainAttrValue(org.apache.syncope.core.persistence.api.entity.user.UPlainAttrValue) User(org.apache.syncope.core.persistence.api.entity.user.User) ItemTransformer(org.apache.syncope.core.provisioning.api.data.ItemTransformer) ArrayList(java.util.ArrayList) IntAttrName(org.apache.syncope.core.provisioning.api.IntAttrName) ParsingValidationException(org.apache.syncope.core.persistence.api.attrvalue.validation.ParsingValidationException) SAML2IdP(org.apache.syncope.core.persistence.api.entity.SAML2IdP) UPlainAttrValue(org.apache.syncope.core.persistence.api.entity.user.UPlainAttrValue) PlainAttrValue(org.apache.syncope.core.persistence.api.entity.PlainAttrValue) ParseException(java.text.ParseException) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with SAML2IdP

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());
            }
        }
    });
}
Also used : Optional(java.util.Optional) ItemTransformer(org.apache.syncope.core.provisioning.api.data.ItemTransformer) AttrTO(org.apache.syncope.common.lib.to.AttrTO) ArrayList(java.util.ArrayList) IntAttrName(org.apache.syncope.core.provisioning.api.IntAttrName) SAML2IdP(org.apache.syncope.core.persistence.api.entity.SAML2IdP) ParseException(java.text.ParseException)

Example 9 with SAML2IdP

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;
}
Also used : JPASAML2IdP(org.apache.syncope.core.persistence.jpa.entity.JPASAML2IdP) SAML2IdP(org.apache.syncope.core.persistence.api.entity.SAML2IdP) JPASAML2IdP(org.apache.syncope.core.persistence.jpa.entity.JPASAML2IdP) NoResultException(javax.persistence.NoResultException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

SAML2IdP (org.apache.syncope.core.persistence.api.entity.SAML2IdP)9 NotFoundException (org.apache.syncope.core.persistence.api.dao.NotFoundException)6 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)5 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)4 ArrayList (java.util.ArrayList)3 SAML2IdPEntity (org.apache.syncope.core.logic.saml2.SAML2IdPEntity)3 Transactional (org.springframework.transaction.annotation.Transactional)3 ParseException (java.text.ParseException)2 Optional (java.util.Optional)2 AttrTO (org.apache.syncope.common.lib.to.AttrTO)2 IntAttrName (org.apache.syncope.core.provisioning.api.IntAttrName)2 ItemTransformer (org.apache.syncope.core.provisioning.api.data.ItemTransformer)2 Collections (java.util.Collections)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 NoResultException (javax.persistence.NoResultException)1 SerializationUtils (org.apache.commons.lang3.SerializationUtils)1 Pair (org.apache.commons.lang3.tuple.Pair)1