Search in sources :

Example 6 with SCIMException

use of org.gluu.oxtrust.model.exception.SCIMException in project oxTrust by GluuFederation.

the class ResourceValidator method validateValidableAttributes.

/**
 * Inspects the resource passed in the constructor and applies validations for every attribute annotated with
 * {@link Validator}. Validations are of different nature as seen{@link Validations here}.
 * @throws SCIMException When a validation does not pass (the {@link Validations#apply(Validations, Object) apply}
 * method returns false)
 */
public void validateValidableAttributes() throws SCIMException {
    Map<String, List<Method>> map = IntrospectUtil.validableCoreAttrs.get(resourceClass);
    for (String attributePath : map.keySet()) {
        Field f = IntrospectUtil.findFieldFromPath(resourceClass, attributePath);
        Validations valToApply = f.getAnnotation(Validator.class).value();
        log.debug("Validating value(s) of attribute '{}'", attributePath);
        for (Object val : IntrospectUtil.getAttributeValues(resource, map.get(attributePath))) {
            if (val != null && !Validations.apply(valToApply, val)) {
                log.error("Error validating attribute '{}', wrong value supplied: '{}'", attributePath, val.toString());
                throw new SCIMException(String.format(ATTR_VALIDATION_FAILED, attributePath));
            }
        }
    }
}
Also used : ExtensionField(org.gluu.oxtrust.model.scim2.extensions.ExtensionField) Field(java.lang.reflect.Field) Validations(org.gluu.oxtrust.model.scim2.Validations) SCIMException(org.gluu.oxtrust.model.exception.SCIMException) Validator(org.gluu.oxtrust.model.scim2.annotations.Validator)

Example 7 with SCIMException

use of org.gluu.oxtrust.model.exception.SCIMException in project oxTrust by GluuFederation.

the class ResourceValidator method validateExtendedAttributes.

/**
 * Inspects the resource passed in the constructor and for every extended attribute (see {@link BaseScimResource#getCustomAttributes()},
 * the attribute's value is checked to see if it complies with the data type it is supposed to belong to. This
 * information is obtained from the list of <code>Extension</code>s passed in the constructor (every {@link ExtensionField}
 * has an associated {@link ExtensionField#getType() type}.
 * <p>When an attribute is {@link ExtensionField#isMultiValued() multi-valued}, every single item inside the collection
 * is validated.</p>
 * @throws SCIMException When any of the validations do not pass or an attribute seems not to be part of a known schema.
 */
public void validateExtendedAttributes() throws SCIMException {
    // Note: throughout this method, we always ignore presence of nulls
    // Gets all extended attributes (see the @JsonAnySetter annotation in BaseScimResource)
    Map<String, Object> extendedAttributes = resource.getCustomAttributes();
    // Iterate over every extension of the resource object (in practice it will be just one at most)
    for (String schema : extendedAttributes.keySet()) {
        // Validate if the schema referenced in the extended attributes is contained in the valid set of extension
        Extension extension = null;
        for (Extension ext : extensions) if (ext.getUrn().equals(schema)) {
            extension = ext;
            break;
        }
        if (extension != null) {
            log.debug("validateExtendedAttributes. Revising attributes under schema {}", schema);
            try {
                // Obtains a generic map consisting of all name/value(s) pairs associated to this schema
                Map<String, Object> attrsMap = IntrospectUtil.strObjMap(extendedAttributes.get(schema));
                for (String attr : attrsMap.keySet()) {
                    Object value = attrsMap.get(attr);
                    if (value != null) {
                        /*
                             Gets the class associated to the value of current attribute. For extended attributes, we
                             should only see coming: String, Integer, Double, boolean, and Collection.
                             Different things will be rejected
                             */
                        Class cls = value.getClass();
                        boolean isCollection = IntrospectUtil.isCollection(cls);
                        // If the attribute coming is unknown, NPE will be thrown and we are covered
                        log.debug("validateExtendedAttributes. Got value(s) for attribute '{}'", attr);
                        // Check if the multivalued custom attribute is consistent with the nature of the value itself
                        if (isCollection == extension.getFields().get(attr).isMultiValued()) {
                            if (isCollection) {
                                for (Object elem : (Collection) value) if (elem != null)
                                    validateDataTypeExtendedAttr(extension, attr, elem);
                            } else
                                validateDataTypeExtendedAttr(extension, attr, value);
                        } else
                            throw new SCIMException(ERROR_PARSING_EXTENDED);
                    }
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                throw new SCIMException(ERROR_PARSING_EXTENDED);
            }
        } else
            throw new SCIMException(String.format(UNKNOWN_EXTENSION, schema));
    }
}
Also used : Extension(org.gluu.oxtrust.model.scim2.extensions.Extension) SCIMException(org.gluu.oxtrust.model.exception.SCIMException) SCIMException(org.gluu.oxtrust.model.exception.SCIMException)

Example 8 with SCIMException

use of org.gluu.oxtrust.model.exception.SCIMException in project oxTrust by GluuFederation.

the class ResourceValidator method validateCanonicalizedAttributes.

/**
 * Inspects the resource passed in the constructor and for every attribute annotated with a non-empty collection of
 * {@link Attribute#canonicalValues() canonical values}, it checks whether the attribute value matches any of the
 * canonical values supplied.
 * <p>This method should be called after a successful call to {@link #validateRequiredAttributes()}.</p>
 * @throws SCIMException When a validation does not pass (there is no match for any of the attributes inspected)
 */
public void validateCanonicalizedAttributes() throws SCIMException {
    Map<String, List<Method>> map = IntrospectUtil.canonicalCoreAttrs.get(resourceClass);
    for (String attributePath : map.keySet()) {
        Attribute attrAnnot = IntrospectUtil.getFieldAnnotation(attributePath, resourceClass, Attribute.class);
        List<String> canonicalVals = Arrays.asList(attrAnnot.canonicalValues());
        log.debug("Validating values of canonical attribute '{}'", attributePath);
        for (Object val : IntrospectUtil.getAttributeValues(resource, map.get(attributePath))) {
            if (!canonicalVals.contains(val.toString())) {
                log.error("Error validating canonical attribute '{}', wrong value supplied: '{}'", attributePath, val.toString());
                throw new SCIMException(String.format(ATTR_VALIDATION_FAILED, attributePath));
            }
        }
    }
}
Also used : SCIMException(org.gluu.oxtrust.model.exception.SCIMException) Attribute(org.gluu.oxtrust.model.scim2.annotations.Attribute)

Example 9 with SCIMException

use of org.gluu.oxtrust.model.exception.SCIMException in project oxTrust by GluuFederation.

the class ResourceValidator method validateSchemasAttribute.

/**
 * Inspects the {@link BaseScimResource#getSchemas() schemas} attribute of the resource passed in the constructor and
 * checks the default schema <code>urn</code> associated to the resource type is present in the list. If some of the
 * <code>urn</code>s part of the <code>Extension</code>s passed in the constructor are contained in the list, the validation is also
 * successful.
 * <p>This method should be called after a successful call to {@link #validateRequiredAttributes()}.</p>
 * @throws SCIMException If there is no {@link BaseScimResource#getSchemas() schemas} in this resource or if some of
 * the <code>urn</code>s there are not known.
 */
public void validateSchemasAttribute() throws SCIMException {
    Set<String> schemaList = new HashSet<String>(resource.getSchemas());
    if (schemaList.size() == 0)
        throw new SCIMException(WRONG_SCHEMAS_ATTR);
    Set<String> allSchemas = new HashSet<String>();
    allSchemas.add(ScimResourceUtil.getDefaultSchemaUrn(resourceClass));
    for (Extension ext : extensions) allSchemas.add(ext.getUrn());
    schemaList.removeAll(allSchemas);
    if (// means that some wrong extension urn is there
    schemaList.size() > 0)
        throw new SCIMException(WRONG_SCHEMAS_ATTR);
}
Also used : Extension(org.gluu.oxtrust.model.scim2.extensions.Extension) SCIMException(org.gluu.oxtrust.model.exception.SCIMException)

Example 10 with SCIMException

use of org.gluu.oxtrust.model.exception.SCIMException in project oxTrust by GluuFederation.

the class ScimFilterParserService method createLdapFilter.

public Filter createLdapFilter(String filter, String defaultStr, Class<? extends BaseScimResource> clazz) throws SCIMException {
    try {
        Filter ldapFilter;
        if (StringUtils.isEmpty(filter))
            ldapFilter = Filter.create(defaultStr);
        else {
            LdapFilterListener ldapFilterListener = new LdapFilterListener(clazz);
            walkTree(FilterUtil.preprocess(filter, clazz), ldapFilterListener);
            ldapFilter = ldapFilterListener.getFilter();
            if (ldapFilter == null)
                throw new Exception("An error occurred when building LDAP filter: " + ldapFilterListener.getError());
        }
        return ldapFilter;
    } catch (Exception e) {
        throw new SCIMException(e.getMessage(), e);
    }
}
Also used : SCIMException(org.gluu.oxtrust.model.exception.SCIMException) Filter(org.gluu.search.filter.Filter) SCIMException(org.gluu.oxtrust.model.exception.SCIMException)

Aggregations

SCIMException (org.gluu.oxtrust.model.exception.SCIMException)20 Response (javax.ws.rs.core.Response)12 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)8 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)7 URI (java.net.URI)7 DefaultValue (javax.ws.rs.DefaultValue)7 HeaderParam (javax.ws.rs.HeaderParam)7 Produces (javax.ws.rs.Produces)7 ListResponse (org.gluu.oxtrust.model.scim2.ListResponse)7 ProtectedApi (org.gluu.oxtrust.service.filter.ProtectedApi)7 RefAdjusted (org.gluu.oxtrust.service.scim2.interceptor.RefAdjusted)7 ListViewResponse (org.gluu.persist.model.ListViewResponse)7 GET (javax.ws.rs.GET)4 Path (javax.ws.rs.Path)4 BaseScimResource (org.gluu.oxtrust.model.scim2.BaseScimResource)4 DuplicateEntryException (org.gluu.persist.exception.operation.DuplicateEntryException)4 Consumes (javax.ws.rs.Consumes)3 Attribute (org.gluu.oxtrust.model.scim2.annotations.Attribute)3 Extension (org.gluu.oxtrust.model.scim2.extensions.Extension)3 FidoDeviceResource (org.gluu.oxtrust.model.scim2.fido.FidoDeviceResource)3