Search in sources :

Example 1 with RequestValidationException

use of org.usermanagement.exception.RequestValidationException in project open-kilda by telstra.

the class SamlValidator method validateUpdateProvider.

/**
 * Validate update provider.
 *
 * @param uuid the uuid
 * @param file the metadata file
 * @param name the provider name
 * @param entityId the entityId
 * @param url the metadata url
 * @param userCreation the userCreation
 * @param roleIds the role ids
 * @return the saml config entity
 */
public SamlConfigEntity validateUpdateProvider(String uuid, MultipartFile file, String name, String entityId, String url, boolean userCreation, List<Long> roleIds) {
    SamlConfigEntity samlConfigEntity = getEntityByUuid(uuid);
    SamlConfigEntity configEntity = samlRepository.findByUuidNotAndEntityIdOrUuidNotAndNameEqualsIgnoreCase(uuid, entityId, uuid, name);
    if (configEntity != null) {
        throw new RequestValidationException(messageUtil.getAttributeUnique("Provider name or Entity Id"));
    }
    if (file != null) {
        if (!FilenameUtils.getExtension(file.getOriginalFilename()).equals("xml")) {
            throw new RequestValidationException(messageUtil.getAttributeMetadataInvalid("file"));
        }
    }
    if (file == null && url == null) {
        if (!samlConfigEntity.getEntityId().equals(entityId)) {
            throw new RequestValidationException(messageUtil.getAttributeInvalid("Entity Id", entityId));
        }
    } else {
        String metadataEntityId = validateEntityId(file, url);
        if (!metadataEntityId.equals(entityId)) {
            throw new RequestValidationException("Entity Id must be same as Metadata Entity Id");
        }
    }
    if (userCreation) {
        if (roleIds.isEmpty() || roleIds == null) {
            throw new RequestValidationException(messageUtil.getAttributeNotNull("role"));
        }
    }
    return samlConfigEntity;
}
Also used : SamlConfigEntity(org.openkilda.saml.dao.entity.SamlConfigEntity) RequestValidationException(org.usermanagement.exception.RequestValidationException)

Example 2 with RequestValidationException

use of org.usermanagement.exception.RequestValidationException in project open-kilda by telstra.

the class SamlValidator method validateEntityId.

/**
 * Validate entity id.
 *
 * @param file the metadata file
 * @param url the metadata url
 * @return the entity id
 */
private String validateEntityId(MultipartFile file, String url) {
    String entityId = null;
    try {
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = dbFactory.newDocumentBuilder();
        Document doc = null;
        if (file != null) {
            doc = docBuilder.parse(file.getInputStream());
        } else if (url != null) {
            doc = docBuilder.parse(new URL(url).openStream());
        }
        doc.getDocumentElement().normalize();
        NodeList nodeList = doc.getElementsByTagName(doc.getDocumentElement().getNodeName());
        for (int temp = 0; temp < nodeList.getLength(); temp++) {
            Node node = nodeList.item(temp);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element element = (Element) node;
                entityId = element.getAttribute("entityID");
            }
        }
        return entityId;
    } catch (Exception e) {
        LOGGER.error("Error occurred while validating entity ID" + e);
        throw new RequestValidationException(messageUtil.getAttributeMetadataInvalid("url"));
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) RequestValidationException(org.usermanagement.exception.RequestValidationException) URL(java.net.URL) RequestValidationException(org.usermanagement.exception.RequestValidationException)

Example 3 with RequestValidationException

use of org.usermanagement.exception.RequestValidationException in project open-kilda by telstra.

the class SamlValidator method validateCreateProvider.

/**
 * Validate create provider.
 *
 * @param file the metadata file
 * @param name the provider name
 * @param entityId the entityId
 * @param url the metadata url
 * @param userCreation the userCreation
 * @param roleIds the role ids
 */
public void validateCreateProvider(MultipartFile file, String name, String entityId, String url, boolean userCreation, List<Long> roleIds) {
    SamlConfigEntity samlConfigEntity = samlRepository.findByEntityIdOrNameEqualsIgnoreCase(entityId, name);
    if (samlConfigEntity != null) {
        throw new RequestValidationException(messageUtil.getAttributeUnique("Provider name or Entity Id"));
    }
    if (file == null && url == null) {
        throw new RequestValidationException(messageUtil.getAttributeNotNull("Metadata file or url"));
    }
    if (file != null) {
        if (!FilenameUtils.getExtension(file.getOriginalFilename()).equals("xml")) {
            throw new RequestValidationException(messageUtil.getAttributeMetadataInvalid("file"));
        }
    }
    String metadataEntityId = validateEntityId(file, url);
    if (!metadataEntityId.equals(entityId)) {
        throw new RequestValidationException("Entity Id must be same as Metadata Entity Id");
    }
    if (userCreation) {
        if (roleIds.isEmpty() || roleIds == null) {
            throw new RequestValidationException(messageUtil.getAttributeNotNull("role"));
        }
    }
}
Also used : SamlConfigEntity(org.openkilda.saml.dao.entity.SamlConfigEntity) RequestValidationException(org.usermanagement.exception.RequestValidationException)

Example 4 with RequestValidationException

use of org.usermanagement.exception.RequestValidationException in project open-kilda by telstra.

the class LinkStoreConfigValidator method validate.

/**
 * Validate.
 *
 * @param linkStoreConfigDto the link store config dto
 */
public void validate(final LinkStoreConfigDto linkStoreConfigDto) {
    List<OauthConfigEntity> oauthConfigEntityList = oauthConfigRepository.findByAuthType_authTypeId(AuthType.OAUTH_TWO.getAuthTypeEntity().getAuthTypeId());
    if (CollectionUtil.isEmpty(oauthConfigEntityList)) {
        LOGGER.warn(messageUtil.getStoreMustConfigured());
        throw new RequestValidationException(messageUtil.getStoreMustConfigured());
    }
    List<String> urls = StoreUrl.getUrlName(StoreType.LINK_STORE.getCode());
    for (Entry<String, UrlDto> urlEntrySet : linkStoreConfigDto.getUrls().entrySet()) {
        if (!urls.contains(urlEntrySet.getKey())) {
            LOGGER.warn("Validation fail for link store configuration. Error: " + messageUtil.getAttributeNotvalid(urlEntrySet.getKey()));
            throw new RequestValidationException(messageUtil.getAttributeNotvalid(urlEntrySet.getKey()));
        } else if (ValidatorUtil.isNull(urlEntrySet.getValue().getUrl())) {
            LOGGER.warn("Validation fail for link store configuration. Error: " + messageUtil.getAttributeNotNull("url of " + urlEntrySet.getKey()));
            throw new RequestValidationException(messageUtil.getAttributeNotNull(urlEntrySet.getKey()));
        } else if (ValidatorUtil.isNull(urlEntrySet.getValue().getMethodType())) {
            LOGGER.warn("Validation fail for link store configuration. Error: " + messageUtil.getAttributeNotNull("method-type of " + urlEntrySet.getKey()));
            throw new RequestValidationException(messageUtil.getAttributeNotNull("method-type of " + urlEntrySet.getKey()));
        }
    }
}
Also used : OauthConfigEntity(org.openkilda.store.auth.dao.entity.OauthConfigEntity) UrlDto(org.openkilda.store.model.UrlDto) RequestValidationException(org.usermanagement.exception.RequestValidationException)

Example 5 with RequestValidationException

use of org.usermanagement.exception.RequestValidationException in project open-kilda by telstra.

the class SwitchStoreConfigValidator method validate.

/**
 * Validate.
 *
 * @param switchStoreConfigDto the link store config dto
 */
public void validate(final SwitchStoreConfigDto switchStoreConfigDto) {
    List<OauthConfigEntity> oauthConfigEntityList = oauthConfigRepository.findByAuthType_authTypeId(AuthType.OAUTH_TWO.getAuthTypeEntity().getAuthTypeId());
    if (CollectionUtil.isEmpty(oauthConfigEntityList)) {
        LOGGER.warn(messageUtil.getStoreMustConfigured());
        throw new RequestValidationException(messageUtil.getStoreMustConfigured());
    }
    List<String> urls = StoreUrl.getUrlName(StoreType.SWITCH_STORE.getCode());
    for (Entry<String, UrlDto> urlEntrySet : switchStoreConfigDto.getUrls().entrySet()) {
        if (!urls.contains(urlEntrySet.getKey())) {
            LOGGER.warn("Validation fail for switch store configuration. Error: " + messageUtil.getAttributeNotvalid(urlEntrySet.getKey()));
            throw new RequestValidationException(messageUtil.getAttributeNotvalid(urlEntrySet.getKey()));
        } else if (ValidatorUtil.isNull(urlEntrySet.getValue().getUrl())) {
            LOGGER.warn("Validation fail for switch store configuration. Error: " + messageUtil.getAttributeNotNull("url of " + urlEntrySet.getKey()));
            throw new RequestValidationException(messageUtil.getAttributeNotNull(urlEntrySet.getKey()));
        } else if (ValidatorUtil.isNull(urlEntrySet.getValue().getMethodType())) {
            LOGGER.warn("Validation fail for switch store configuration. Error: " + messageUtil.getAttributeNotNull("method-type of " + urlEntrySet.getKey()));
            throw new RequestValidationException(messageUtil.getAttributeNotNull("method-type of " + urlEntrySet.getKey()));
        }
    }
}
Also used : OauthConfigEntity(org.openkilda.store.auth.dao.entity.OauthConfigEntity) UrlDto(org.openkilda.store.model.UrlDto) RequestValidationException(org.usermanagement.exception.RequestValidationException)

Aggregations

RequestValidationException (org.usermanagement.exception.RequestValidationException)25 Transactional (org.springframework.transaction.annotation.Transactional)16 RoleEntity (org.usermanagement.dao.entity.RoleEntity)9 UserEntity (org.usermanagement.dao.entity.UserEntity)7 PermissionEntity (org.usermanagement.dao.entity.PermissionEntity)6 AccessDeniedException (java.nio.file.AccessDeniedException)4 HashMap (java.util.HashMap)3 InvalidOtpException (org.openkilda.exception.InvalidOtpException)3 OtpRequiredException (org.openkilda.exception.OtpRequiredException)3 TwoFaKeyNotSetException (org.openkilda.exception.TwoFaKeyNotSetException)3 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)3 UserInfo (org.usermanagement.model.UserInfo)3 Date (java.util.Date)2 HashSet (java.util.HashSet)2 SamlConfigEntity (org.openkilda.saml.dao.entity.SamlConfigEntity)2 OauthConfigEntity (org.openkilda.store.auth.dao.entity.OauthConfigEntity)2 UrlDto (org.openkilda.store.model.UrlDto)2 Role (org.usermanagement.model.Role)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1