Search in sources :

Example 1 with RequiredElements

use of org.apache.wss4j.policy.model.RequiredElements in project cxf by apache.

the class RequiredElementsPolicyValidator method validatePolicies.

/**
 * Validate policies.
 */
public void validatePolicies(PolicyValidatorParameters parameters, Collection<AssertionInfo> ais) {
    for (AssertionInfo ai : ais) {
        RequiredElements rp = (RequiredElements) ai.getAssertion();
        ai.setAsserted(true);
        if (rp != null && rp.getXPaths() != null && !rp.getXPaths().isEmpty()) {
            XPathFactory factory = XPathFactory.newInstance();
            for (org.apache.wss4j.policy.model.XPath xPath : rp.getXPaths()) {
                Map<String, String> namespaces = xPath.getPrefixNamespaceMap();
                String expression = xPath.getXPath();
                XPath xpath = factory.newXPath();
                if (namespaces != null) {
                    xpath.setNamespaceContext(new MapNamespaceContext(namespaces));
                }
                NodeList list;
                Element header = parameters.getSoapHeader();
                header = (Element) DOMUtils.getDomElement(header);
                try {
                    list = (NodeList) xpath.evaluate(expression, header, XPathConstants.NODESET);
                    if (list.getLength() == 0) {
                        ai.setNotAsserted("No header element matching XPath " + expression + " found.");
                    }
                } catch (XPathExpressionException e) {
                    ai.setNotAsserted("Invalid XPath expression " + expression + " " + e.getMessage());
                }
            }
        }
    }
}
Also used : RequiredElements(org.apache.wss4j.policy.model.RequiredElements) XPath(javax.xml.xpath.XPath) AssertionInfo(org.apache.cxf.ws.policy.AssertionInfo) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) MapNamespaceContext(org.apache.cxf.helpers.MapNamespaceContext) XPathFactory(javax.xml.xpath.XPathFactory)

Example 2 with RequiredElements

use of org.apache.wss4j.policy.model.RequiredElements in project cxf by apache.

the class SecuredElementsPolicyValidator method validatePolicies.

/**
 * Validate policies.
 */
public void validatePolicies(PolicyValidatorParameters parameters, Collection<AssertionInfo> ais) {
    // XPathFactory and XPath are not thread-safe so we must recreate them
    // each request.
    final XPathFactory factory = XPathFactory.newInstance();
    final XPath xpath = factory.newXPath();
    Element soapEnvelope = parameters.getSoapHeader().getOwnerDocument().getDocumentElement();
    Collection<WSDataRef> dataRefs = parameters.getEncrypted();
    if (coverageType == CoverageType.SIGNED) {
        dataRefs = parameters.getSigned();
    }
    for (AssertionInfo ai : ais) {
        RequiredElements elements = (RequiredElements) ai.getAssertion();
        ai.setAsserted(true);
        if (elements != null && elements.getXPaths() != null && !elements.getXPaths().isEmpty()) {
            List<String> expressions = new ArrayList<>();
            MapNamespaceContext namespaceContext = new MapNamespaceContext();
            for (org.apache.wss4j.policy.model.XPath xPath : elements.getXPaths()) {
                expressions.add(xPath.getXPath());
                Map<String, String> namespaceMap = xPath.getPrefixNamespaceMap();
                if (namespaceMap != null) {
                    namespaceContext.addNamespaces(namespaceMap);
                }
            }
            xpath.setNamespaceContext(namespaceContext);
            try {
                CryptoCoverageUtil.checkCoverage(soapEnvelope, dataRefs, xpath, expressions, coverageType, coverageScope);
            } catch (WSSecurityException e) {
                ai.setNotAsserted("No " + coverageType + " element found matching one of the XPaths " + Arrays.toString(expressions.toArray()));
            }
        }
    }
}
Also used : XPath(javax.xml.xpath.XPath) RequiredElements(org.apache.wss4j.policy.model.RequiredElements) AssertionInfo(org.apache.cxf.ws.policy.AssertionInfo) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) MapNamespaceContext(org.apache.cxf.helpers.MapNamespaceContext) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) WSDataRef(org.apache.wss4j.dom.WSDataRef) XPathFactory(javax.xml.xpath.XPathFactory)

Aggregations

XPath (javax.xml.xpath.XPath)2 XPathFactory (javax.xml.xpath.XPathFactory)2 MapNamespaceContext (org.apache.cxf.helpers.MapNamespaceContext)2 AssertionInfo (org.apache.cxf.ws.policy.AssertionInfo)2 RequiredElements (org.apache.wss4j.policy.model.RequiredElements)2 Element (org.w3c.dom.Element)2 ArrayList (java.util.ArrayList)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1 WSSecurityException (org.apache.wss4j.common.ext.WSSecurityException)1 WSDataRef (org.apache.wss4j.dom.WSDataRef)1 NodeList (org.w3c.dom.NodeList)1