Search in sources :

Example 6 with BadRequestException

use of org.wso2.charon3.core.exceptions.BadRequestException in project OpenAM by OpenRock.

the class OpenIDConnectProviderDiscovery method discover.

/**
     * Returns the response to a request to discover the OpenId Connect provider.
     *
     * @param resource The resource.
     * @param rel The rel.
     * @param deploymentUrl The deployment url of the OpenId Connect provider.
     * @param request The OAuth2 request.
     * @return A {@code Map} of the OpenId Connect provider urls.
     * @throws BadRequestException If the request is malformed.
     * @throws NotFoundException If the user cannot be found.
     */
public Map<String, Object> discover(String resource, String rel, String deploymentUrl, OAuth2Request request) throws BadRequestException, NotFoundException {
    if (resource == null || resource.isEmpty()) {
        logger.error("No resource provided in discovery.");
        throw new BadRequestException("No resource provided in discovery.");
    }
    if (rel == null || rel.isEmpty() || !rel.equalsIgnoreCase("http://openid.net/specs/connect/1.0/issuer")) {
        logger.error("No or invalid rel provided in discovery.");
        throw new BadRequestException("No or invalid rel provided in discovery.");
    }
    String userid = null;
    //test if the resource is a uri
    try {
        final URI object = new URI(resource);
        if (object.getScheme().equalsIgnoreCase("https") || object.getScheme().equalsIgnoreCase("http")) {
            //resource is of the form of https://example.com/
            if (!object.getPath().isEmpty()) {
                //resource is of the form of https://example.com/joe
                userid = object.getPath();
                userid = userid.substring(1, userid.length());
            }
        } else if (object.getScheme().equalsIgnoreCase("acct")) {
            //resource is not uri so only option is it is an email of form acct:joe@example.com
            String s = new String(resource);
            s = s.replaceFirst("acct:", "");
            final int firstAt = s.indexOf('@');
            userid = s.substring(0, firstAt);
        } else {
            logger.error("Invalid parameters.");
            throw new BadRequestException("Invalid parameters.");
        }
    } catch (Exception e) {
        logger.error("Invalid parameters.", e);
        throw new BadRequestException("Invalid parameters.");
    }
    if (userid != null) {
        if (!openIDConnectProvider.isUserValid(userid, request)) {
            logger.error("Invalid parameters.");
            throw new NotFoundException("Invalid parameters.");
        }
    }
    final Map<String, Object> response = new HashMap<String, Object>();
    response.put("subject", resource);
    final Set<Object> set = new HashSet<Object>();
    final Map<String, Object> objectMap = new HashMap<String, Object>();
    objectMap.put("rel", rel);
    objectMap.put("href", deploymentUrl + "/oauth2");
    set.add(objectMap);
    response.put("links", set);
    return response;
}
Also used : HashMap(java.util.HashMap) BadRequestException(org.forgerock.oauth2.core.exceptions.BadRequestException) NotFoundException(org.forgerock.oauth2.core.exceptions.NotFoundException) URI(java.net.URI) BadRequestException(org.forgerock.oauth2.core.exceptions.BadRequestException) NotFoundException(org.forgerock.oauth2.core.exceptions.NotFoundException) HashSet(java.util.HashSet)

Example 7 with BadRequestException

use of org.wso2.charon3.core.exceptions.BadRequestException in project charon by wso2.

the class ServerSideValidator method validateCreatedSCIMObject.

/*
     * Validate created SCIMObject according to the spec
     *
     * @param scimObject
     * @param resourceSchema
     * @throw CharonException
     * @throw BadRequestException
     * @throw NotFoundException
     */
public static void validateCreatedSCIMObject(AbstractSCIMObject scimObject, SCIMResourceTypeSchema resourceSchema) throws CharonException, BadRequestException, NotFoundException {
    if (scimObject instanceof User) {
        // set display names for complex multivalued attributes
        setDisplayNameInComplexMultiValuedAttributes(scimObject, resourceSchema);
    }
    // remove any read only attributes
    removeAnyReadOnlyAttributes(scimObject, resourceSchema);
    // add created and last modified dates
    String id = UUID.randomUUID().toString();
    scimObject.setId(id);
    Date date = new Date();
    // set the created date and time
    scimObject.setCreatedDate(AttributeUtil.parseDateTime(AttributeUtil.formatDateTime(date)));
    // creates date and the last modified are the same if not updated.
    scimObject.setLastModified(AttributeUtil.parseDateTime(AttributeUtil.formatDateTime(date)));
    // set location and resourceType
    if (resourceSchema.isSchemaAvailable(SCIMConstants.USER_CORE_SCHEMA_URI)) {
        String location = createLocationHeader(AbstractResourceManager.getResourceEndpointURL(SCIMConstants.USER_ENDPOINT), scimObject.getId());
        scimObject.setLocation(location);
        scimObject.setResourceType(SCIMConstants.USER);
    } else if (resourceSchema.isSchemaAvailable(SCIMConstants.GROUP_CORE_SCHEMA_URI)) {
        String location = createLocationHeader(AbstractResourceManager.getResourceEndpointURL(SCIMConstants.GROUP_ENDPOINT), scimObject.getId());
        scimObject.setLocation(location);
        scimObject.setResourceType(SCIMConstants.GROUP);
    }
    // check for required attributes
    validateSCIMObjectForRequiredAttributes(scimObject, resourceSchema);
    validateSchemaList(scimObject, resourceSchema);
}
Also used : User(org.wso2.charon3.core.objects.User) Date(java.util.Date)

Example 8 with BadRequestException

use of org.wso2.charon3.core.exceptions.BadRequestException in project charon by wso2.

the class PatchOperationUtil method doPatchRemoveWithFiltersForLevelTwo.

/*
     *
     * @param oldResource
     * @param attributeParts
     * @param expressionNode
     * @return
     * @throws BadRequestException
     * @throws CharonException
     */
private static AbstractSCIMObject doPatchRemoveWithFiltersForLevelTwo(AbstractSCIMObject oldResource, String[] attributeParts, ExpressionNode expressionNode) throws BadRequestException, CharonException {
    Attribute attribute = oldResource.getAttribute(attributeParts[0]);
    if (attribute != null) {
        if (attribute.getMultiValued()) {
            List<Attribute> subValues = ((MultiValuedAttribute) attribute).getAttributeValues();
            if (subValues != null) {
                for (Attribute subValue : subValues) {
                    Map<String, Attribute> subAttributes = ((ComplexAttribute) subValue).getSubAttributesList();
                    // this map is to avoid concurrent modification exception.
                    Map<String, Attribute> tempSubAttributes = (Map<String, Attribute>) CopyUtil.deepCopy(subAttributes);
                    for (Iterator<Attribute> iterator = tempSubAttributes.values().iterator(); iterator.hasNext(); ) {
                        Attribute subAttribute = iterator.next();
                        if (subAttribute.getName().equals(expressionNode.getAttributeValue())) {
                            if (((SimpleAttribute) subAttribute).getValue().equals(expressionNode.getValue())) {
                                Attribute removingAttribute = subAttributes.get(attributeParts[1]);
                                if (removingAttribute == null) {
                                    throw new BadRequestException("No such sub attribute with the name : " + attributeParts[1] + " " + "within the attribute " + attributeParts[0], ResponseCodeConstants.INVALID_PATH);
                                }
                                if (removingAttribute.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || removingAttribute.getRequired().equals(true)) {
                                    throw new BadRequestException("Can not remove a required attribute or a read-only attribute", ResponseCodeConstants.MUTABILITY);
                                } else {
                                    ((ComplexAttribute) subValue).removeSubAttribute(removingAttribute.getName());
                                }
                            }
                        }
                    }
                }
                if (subValues.size() == 0) {
                    // if the attribute has no values, make it unassigned
                    oldResource.deleteAttribute(attribute.getName());
                }
            }
        } else if (attribute.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
            // this is only valid for extension
            Attribute subAttribute = attribute.getSubAttribute(attributeParts[1]);
            if (subAttribute == null) {
                throw new BadRequestException("No such sub attribute with the name : " + attributeParts[1] + " " + "within the attribute " + attributeParts[0], ResponseCodeConstants.INVALID_PATH);
            }
            List<Attribute> subValues = ((MultiValuedAttribute) (subAttribute)).getAttributeValues();
            if (subValues != null) {
                for (Iterator<Attribute> subValueIterator = subValues.iterator(); subValueIterator.hasNext(); ) {
                    Attribute subValue = subValueIterator.next();
                    Map<String, Attribute> subValuesSubAttribute = ((ComplexAttribute) subValue).getSubAttributesList();
                    for (Iterator<Attribute> iterator = subValuesSubAttribute.values().iterator(); iterator.hasNext(); ) {
                        Attribute subSubAttribute = iterator.next();
                        if (subSubAttribute.getName().equals(expressionNode.getAttributeValue())) {
                            if (((SimpleAttribute) (subSubAttribute)).getValue().equals(expressionNode.getValue())) {
                                if (subValue.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || subValue.getRequired().equals(true)) {
                                    throw new BadRequestException("Can not remove a required attribute or a read-only attribute", ResponseCodeConstants.MUTABILITY);
                                } else {
                                    subValueIterator.remove();
                                }
                            }
                        }
                    }
                }
                // if the attribute has no values, make it unassigned
                if (((MultiValuedAttribute) (subAttribute)).getAttributeValues().size() == 0) {
                    ((ComplexAttribute) attribute).removeSubAttribute(subAttribute.getName());
                }
            }
        } else {
            throw new BadRequestException("Attribute : " + expressionNode.getAttributeValue() + " " + "is not a multivalued attribute.", ResponseCodeConstants.INVALID_PATH);
        }
    } else {
        throw new BadRequestException("No such attribute with the name : " + attributeParts[0] + " " + "in the current resource", ResponseCodeConstants.INVALID_PATH);
    }
    return oldResource;
}
Also used : MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute) SimpleAttribute(org.wso2.charon3.core.attributes.SimpleAttribute) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) Attribute(org.wso2.charon3.core.attributes.Attribute) SimpleAttribute(org.wso2.charon3.core.attributes.SimpleAttribute) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) Iterator(java.util.Iterator) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) List(java.util.List) Map(java.util.Map) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute)

Example 9 with BadRequestException

use of org.wso2.charon3.core.exceptions.BadRequestException in project charon by wso2.

the class PatchOperationUtil method doPatchReplace.

/*
     * This is the main patch replace method.
     * @param operation
     * @param decoder
     * @param oldResource
     * @param copyOfOldResource
     * @param schema
     * @return
     * @throws CharonException
     * @throws NotImplementedException
     * @throws BadRequestException
     * @throws JSONException
     * @throws InternalErrorException
     */
public static AbstractSCIMObject doPatchReplace(PatchOperation operation, JSONDecoder decoder, AbstractSCIMObject oldResource, AbstractSCIMObject copyOfOldResource, SCIMResourceTypeSchema schema) throws CharonException, NotImplementedException, BadRequestException, InternalErrorException {
    if (operation.getPath() != null) {
        String path = operation.getPath();
        // split the path to extract the filter if present.
        String[] parts = path.split("[\\[\\]]");
        if (operation.getPath().contains("[")) {
            try {
                doPatchReplaceOnPathWithFilters(oldResource, schema, decoder, operation, parts);
            } catch (JSONException e) {
                throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
            }
        } else {
            doPatchReplaceOnPathWithoutFilters(oldResource, schema, decoder, operation, parts);
        }
    } else {
        doPatchReplaceOnResource(oldResource, copyOfOldResource, schema, decoder, operation);
    }
    // validate the updated object
    AbstractSCIMObject validatedResource = ServerSideValidator.validateUpdatedSCIMObject(copyOfOldResource, oldResource, schema);
    return validatedResource;
}
Also used : AbstractSCIMObject(org.wso2.charon3.core.objects.AbstractSCIMObject) JSONException(org.json.JSONException) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException)

Example 10 with BadRequestException

use of org.wso2.charon3.core.exceptions.BadRequestException in project charon by wso2.

the class PatchOperationUtil method doPatchRemoveWithoutFilters.

/*
     * This is the patch remove operation when the path is specified without a filter in it.
     * @param parts
     * @param oldResource
     * @return
     * @throws BadRequestException
     * @throws CharonException
     */
private static AbstractSCIMObject doPatchRemoveWithoutFilters(String[] parts, AbstractSCIMObject oldResource) throws BadRequestException, CharonException {
    String[] attributeParts = parts[0].split("[\\.]");
    if (attributeParts.length == 1) {
        Attribute attribute = oldResource.getAttribute(parts[0]);
        if (attribute != null) {
            if (attribute.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || attribute.getRequired().equals(true)) {
                throw new BadRequestException("Can not remove a required attribute or a read-only attribute", ResponseCodeConstants.MUTABILITY);
            } else {
                String attributeName = attribute.getName();
                oldResource.deleteAttribute(attributeName);
            }
        } else {
            throw new BadRequestException("No such attribute with the name : " + attributeParts[0] + " " + "in the current resource", ResponseCodeConstants.INVALID_PATH);
        }
    } else {
        Attribute attribute = oldResource.getAttribute(attributeParts[0]);
        if (attribute != null) {
            if (attribute.getMultiValued()) {
                // this is multivalued complex case
                List<Attribute> subValuesList = ((MultiValuedAttribute) attribute).getAttributeValues();
                if (subValuesList != null) {
                    for (Attribute subValue : subValuesList) {
                        Map<String, Attribute> subSubAttributeList = ((ComplexAttribute) subValue).getSubAttributesList();
                        // need to remove attributes while iterating through the list.
                        for (Iterator<Attribute> iterator = subSubAttributeList.values().iterator(); iterator.hasNext(); ) {
                            Attribute subSubAttribute = iterator.next();
                            if (subSubAttribute.getName().equals(attributeParts[1])) {
                                if (subSubAttribute.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || subSubAttribute.getRequired().equals(true)) {
                                    throw new BadRequestException("Can not remove a required attribute or a read-only attribute", ResponseCodeConstants.MUTABILITY);
                                } else {
                                    iterator.remove();
                                }
                            }
                        }
                    }
                }
            } else {
                Attribute subAttribute = attribute.getSubAttribute(attributeParts[1]);
                if (subAttribute != null) {
                    if (attributeParts.length == 3) {
                        if (subAttribute.getMultiValued()) {
                            List<Attribute> subSubValuesList = ((MultiValuedAttribute) subAttribute).getAttributeValues();
                            if (subSubValuesList != null) {
                                for (Attribute subSubValue : subSubValuesList) {
                                    Map<String, Attribute> subSubAttributeList = ((ComplexAttribute) subSubValue).getSubAttributesList();
                                    // need to remove attributes while iterating through the list.
                                    for (Iterator<Attribute> iterator = subSubAttributeList.values().iterator(); iterator.hasNext(); ) {
                                        Attribute subSubAttribute = iterator.next();
                                        if (subSubAttribute.getName().equals(attributeParts[2])) {
                                            if (subSubAttribute.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || subSubAttribute.getRequired().equals(true)) {
                                                throw new BadRequestException("Can not remove a required attribute or a read-only " + "attribute", ResponseCodeConstants.MUTABILITY);
                                            } else {
                                                iterator.remove();
                                            }
                                        }
                                    }
                                }
                            }
                        } else {
                            Attribute subSubAttribute = subAttribute.getSubAttribute(attributeParts[2]);
                            if (subSubAttribute.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || subSubAttribute.getRequired().equals(true)) {
                                throw new BadRequestException("Can not remove a required attribute or a read-only attribute", ResponseCodeConstants.MUTABILITY);
                            } else {
                                String subSubAttributeName = subSubAttribute.getName();
                                ((ComplexAttribute) subAttribute).removeSubAttribute(subSubAttributeName);
                            }
                        }
                    } else {
                        // this is complex attribute's sub attribute check
                        if (subAttribute.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || subAttribute.getRequired().equals(true)) {
                            throw new BadRequestException("Can not remove a required attribute or a read-only attribute", ResponseCodeConstants.MUTABILITY);
                        } else {
                            String subAttributeName = subAttribute.getName();
                            ((ComplexAttribute) attribute).removeSubAttribute(subAttributeName);
                        }
                    }
                } else {
                    throw new BadRequestException("No such sub attribute with the name : " + attributeParts[1] + " " + "in the attribute : " + attributeParts[0], ResponseCodeConstants.INVALID_PATH);
                }
            }
        } else {
            throw new BadRequestException("No such attribute with the name : " + attributeParts[0] + " " + "in the current resource", ResponseCodeConstants.INVALID_PATH);
        }
    }
    return oldResource;
}
Also used : MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute) SimpleAttribute(org.wso2.charon3.core.attributes.SimpleAttribute) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) Attribute(org.wso2.charon3.core.attributes.Attribute) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute)

Aggregations

BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)63 CharonException (org.wso2.charon3.core.exceptions.CharonException)31 SimpleAttribute (org.wso2.charon3.core.attributes.SimpleAttribute)30 ComplexAttribute (org.wso2.charon3.core.attributes.ComplexAttribute)27 HashMap (java.util.HashMap)23 MultiValuedAttribute (org.wso2.charon3.core.attributes.MultiValuedAttribute)23 Attribute (org.wso2.charon3.core.attributes.Attribute)20 InternalErrorException (org.wso2.charon3.core.exceptions.InternalErrorException)19 SCIMResponse (org.wso2.charon3.core.protocol.SCIMResponse)19 SCIMResourceTypeSchema (org.wso2.charon3.core.schema.SCIMResourceTypeSchema)19 NotFoundException (org.wso2.charon3.core.exceptions.NotFoundException)18 JSONException (org.json.JSONException)17 JSONObject (org.json.JSONObject)17 AbstractSCIMObject (org.wso2.charon3.core.objects.AbstractSCIMObject)16 JSONEncoder (org.wso2.charon3.core.encoder.JSONEncoder)15 JSONDecoder (org.wso2.charon3.core.encoder.JSONDecoder)14 NotImplementedException (org.wso2.charon3.core.exceptions.NotImplementedException)14 User (org.wso2.charon3.core.objects.User)12 JSONArray (org.json.JSONArray)11 ArrayList (java.util.ArrayList)9