Search in sources :

Example 61 with AssertionInfoMap

use of org.apache.cxf.ws.policy.AssertionInfoMap in project cxf by apache.

the class CustomPolicyAlgorithmsTest method testSHA256AsymSigAlgorithm.

@Test
public void testSHA256AsymSigAlgorithm() throws Exception {
    final String rsaSha2SigMethod = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256";
    String policyName = "signed_elements_policy.xml";
    Policy policy = policyBuilder.getPolicy(this.getResourceAsStream(policyName));
    AssertionInfoMap aim = new AssertionInfoMap(policy);
    AssertionInfo assertInfo = aim.get(SP12Constants.ASYMMETRIC_BINDING).iterator().next();
    AsymmetricBinding binding = (AsymmetricBinding) assertInfo.getAssertion();
    // set Signature Algorithm to RSA SHA-256
    binding.getAlgorithmSuite().getAlgorithmSuiteType().setAsymmetricSignature(rsaSha2SigMethod);
    String sigMethod = binding.getAlgorithmSuite().getAlgorithmSuiteType().getAsymmetricSignature();
    assertNotNull(sigMethod);
    assertEquals(rsaSha2SigMethod, sigMethod);
}
Also used : Policy(org.apache.neethi.Policy) AssertionInfo(org.apache.cxf.ws.policy.AssertionInfo) AsymmetricBinding(org.apache.wss4j.policy.model.AsymmetricBinding) AssertionInfoMap(org.apache.cxf.ws.policy.AssertionInfoMap) Test(org.junit.Test)

Example 62 with AssertionInfoMap

use of org.apache.cxf.ws.policy.AssertionInfoMap in project cxf by apache.

the class SCTTokenValidator method validate.

public Credential validate(Credential credential, RequestData data) throws WSSecurityException {
    // Sleep to make sure token gets replicated
    try {
        Thread.sleep(2 * 1000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Credential validatedCredential = super.validate(credential, data);
    // Hack to verify the IssuedToken assertion, as this is not done by default in CXF for a
    // SecurityContextToken
    SoapMessage soapMessage = (SoapMessage) data.getMsgContext();
    AssertionInfoMap aim = soapMessage.get(AssertionInfoMap.class);
    Collection<AssertionInfo> ais = aim.get(SP12Constants.ISSUED_TOKEN);
    for (AssertionInfo ai : ais) {
        ai.setAsserted(true);
    }
    return validatedCredential;
}
Also used : Credential(org.apache.wss4j.dom.validate.Credential) AssertionInfo(org.apache.cxf.ws.policy.AssertionInfo) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) AssertionInfoMap(org.apache.cxf.ws.policy.AssertionInfoMap)

Example 63 with AssertionInfoMap

use of org.apache.cxf.ws.policy.AssertionInfoMap in project tesb-rt-se by Talend.

the class SchemaValidationPolicyAbstractInterceptor method handleMessageWithAssertionInfo.

protected void handleMessageWithAssertionInfo(Message message) throws Fault {
    AssertionInfoMap aim = message.get(AssertionInfoMap.class);
    if (null == aim) {
        return;
    }
    Collection<AssertionInfo> ais = aim.get(SchemaValidationPolicyBuilder.SCHEMA_VALIDATION);
    if (null == ais) {
        return;
    }
    for (AssertionInfo ai : ais) {
        if (ai.getAssertion() instanceof SchemaValidationPolicy) {
            SchemaValidationPolicy vPolicy = (SchemaValidationPolicy) ai.getAssertion();
            ValidationType vldType = vPolicy.getValidationType();
            AppliesToType appliesToType = vPolicy.getApplyToType();
            MessageType msgType = vPolicy.getMessageType();
            String customSchemaPath = vPolicy.getCustomSchemaPath();
            if (vldType != ValidationType.WSDLSchema) {
                ai.setAsserted(true);
            }
            if (shouldSchemaValidate(message, msgType, appliesToType)) {
                if (vldType == ValidationType.CustomSchema) {
                    // load custom schema from external source
                    try {
                        loadCustomSchema(message, customSchemaPath, this.getClass());
                    } catch (IOException ex) {
                        throw new RuntimeException("Can not load custom schema", ex);
                    }
                }
                // do schema validation by setting value to
                // "schema-validation-enabled" property
                validateBySettingProperty(message);
            }
            ai.setAsserted(true);
        }
        ai.setAsserted(true);
    }
}
Also used : AssertionInfo(org.apache.cxf.ws.policy.AssertionInfo) SchemaValidationPolicy(org.talend.esb.policy.schemavalidate.SchemaValidationPolicy) AppliesToType(org.talend.esb.policy.schemavalidate.SchemaValidationPolicy.AppliesToType) ValidationType(org.talend.esb.policy.schemavalidate.SchemaValidationPolicy.ValidationType) IOException(java.io.IOException) MessageType(org.talend.esb.policy.schemavalidate.SchemaValidationPolicy.MessageType) AssertionInfoMap(org.apache.cxf.ws.policy.AssertionInfoMap)

Example 64 with AssertionInfoMap

use of org.apache.cxf.ws.policy.AssertionInfoMap in project tesb-rt-se by Talend.

the class SamEnablingInterceptorProvider method process.

static void process(Message message) {
    AssertionInfoMap aim = message.get(AssertionInfoMap.class);
    if (aim != null) {
        Collection<AssertionInfo> ais = aim.get(SamEnablingPolicyBuilder.SAM_ENABLE);
        if (ais != null) {
            for (AssertionInfo ai : ais) {
                if (ai.getAssertion() instanceof SamEnablingPolicy) {
                    SamEnablingPolicy vPolicy = (SamEnablingPolicy) ai.getAssertion();
                    AppliesToType appliesToType = vPolicy.getAppliesToType();
                    // Service service = ServiceModelUtil.getService(message
                    // .getExchange());
                    Exchange ex = message.getExchange();
                    Bus b = ex.getBus();
                    if (b.getFeatures().contains(EventFeature.class)) {
                        ai.setAsserted(true);
                        return;
                    }
                    Endpoint ep = ex.getEndpoint();
                    Bundle bundle = FrameworkUtil.getBundle(SamEnablingInterceptorProvider.class);
                    EventFeature eventFeature = null;
                    if (bundle != null) {
                        // OSGi
                        BundleContext context = FrameworkUtil.getBundle(SamEnablingInterceptorProvider.class).getBundleContext();
                        ServiceReference sref = context.getServiceReference(EventFeature.class.getName());
                        eventFeature = (EventFeature) context.getService(sref);
                    } else {
                        // non-OSGi
                        if (springContext == null) {
                            throw springContextException;
                        }
                        eventFeature = (EventFeature) springContext.getBean("eventFeature");
                    }
                    if (MessageUtils.isRequestor(message)) {
                        if (MessageUtils.isOutbound(message)) {
                            // REQ_OUT
                            if ((appliesToType == AppliesToType.consumer || appliesToType == AppliesToType.always)) {
                                Client cli = ex.get(Client.class);
                                if (!cli.getOutInterceptors().contains(WireTapOut.class)) {
                                    eventFeature.initialize(cli, b);
                                    List<Interceptor<? extends Message>> outInterceptors = cli.getOutInterceptors();
                                    message.getInterceptorChain().add(outInterceptors);
                                    outInterceptors.getClass();
                                }
                            }
                        } else {
                            // RESP_IN
                            if ((appliesToType == AppliesToType.consumer || appliesToType == AppliesToType.always)) {
                                Client cli = ex.get(Client.class);
                                eventFeature.initialize(cli, b);
                            }
                        }
                    } else {
                        ServerRegistry registry = b.getExtension(ServerRegistry.class);
                        List<Server> servers = registry.getServers();
                        if (MessageUtils.isOutbound(message)) {
                            // RESP_OUT
                            if ((appliesToType == AppliesToType.provider || appliesToType == AppliesToType.always)) {
                                for (Server sr : servers) {
                                    EndpointInfo ei = sr.getEndpoint().getEndpointInfo();
                                    if (null != ei && ei.getAddress().equals(ep.getEndpointInfo().getAddress())) {
                                        eventFeature.initialize(sr, b);
                                    }
                                }
                            }
                        } else {
                            // REQ_IN
                            if ((appliesToType == AppliesToType.provider || appliesToType == AppliesToType.always)) {
                                for (Server sr : servers) {
                                    EndpointInfo ei = sr.getEndpoint().getEndpointInfo();
                                    if (null != ei && ei.getAddress().equals(ep.getEndpointInfo().getAddress()) && (!sr.getEndpoint().getInInterceptors().contains(WireTapIn.class))) {
                                        eventFeature.initialize(sr, b);
                                        List<Interceptor<? extends Message>> inInterceptors = sr.getEndpoint().getInInterceptors();
                                        message.getInterceptorChain().add(inInterceptors);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        for (AssertionInfo ai : ais) {
            ai.setAsserted(true);
        }
    }
}
Also used : Bus(org.apache.cxf.Bus) AssertionInfo(org.apache.cxf.ws.policy.AssertionInfo) Message(org.apache.cxf.message.Message) Server(org.apache.cxf.endpoint.Server) Bundle(org.osgi.framework.Bundle) ServerRegistry(org.apache.cxf.endpoint.ServerRegistry) WireTapIn(org.talend.esb.sam.agent.wiretap.WireTapIn) EventFeature(org.talend.esb.sam.agent.feature.EventFeature) AssertionInfoMap(org.apache.cxf.ws.policy.AssertionInfoMap) ServiceReference(org.osgi.framework.ServiceReference) Exchange(org.apache.cxf.message.Exchange) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) Endpoint(org.apache.cxf.endpoint.Endpoint) WireTapOut(org.talend.esb.sam.agent.wiretap.WireTapOut) AppliesToType(org.talend.esb.policy.samenabling.SamEnablingPolicy.AppliesToType) Client(org.apache.cxf.endpoint.Client) AbstractPhaseInterceptor(org.apache.cxf.phase.AbstractPhaseInterceptor) Interceptor(org.apache.cxf.interceptor.Interceptor) BundleContext(org.osgi.framework.BundleContext)

Example 65 with AssertionInfoMap

use of org.apache.cxf.ws.policy.AssertionInfoMap in project tesb-rt-se by Talend.

the class CorrelationIDInterceptorProvider method process.

static void process(Message message) throws SAXException, IOException, ParserConfigurationException {
    AssertionInfoMap aim = message.get(AssertionInfoMap.class);
    if (aim != null) {
        Collection<AssertionInfo> ais = aim.get(CorrelationIDPolicyBuilder.CORRELATION_ID);
        if (ais == null) {
            return;
        }
        for (AssertionInfo ai : ais) {
            if (ai.getAssertion() instanceof CorrelationIDAssertion) {
                CorrelationIDAssertion cAssertion = (CorrelationIDAssertion) ai.getAssertion();
                MethodType mType = cAssertion.getMethodType();
                // String value = cAssetrion.getValue();
                String correlationId = null;
                // get ID from Http header
                correlationId = CorrelationIdProtocolHeaderCodec.readCorrelationId(message);
                // get ID from SOAP header
                if (null == correlationId) {
                    correlationId = CorrelationIdSoapCodec.readCorrelationId(message);
                }
                // get from message
                if (null == correlationId) {
                    // Get ID from Message
                    correlationId = (String) message.get(CorrelationIDFeature.MESSAGE_CORRELATION_ID);
                }
                if ((message.getContent(javax.xml.stream.XMLStreamWriter.class) != null) && (message.getContent(javax.xml.stream.XMLStreamWriter.class) instanceof SAAJStreamWriter)) {
                    NodeList nodeList = ((SAAJStreamWriter) message.getContent(javax.xml.stream.XMLStreamWriter.class)).getDocument().getElementsByTagNameNS("http://www.talend.com/esb/sam/correlationId/v1", "correlationId");
                    if (nodeList.getLength() > 0) {
                        correlationId = nodeList.item(0).getTextContent();
                    }
                }
                // get from message exchange
                if (null == correlationId) {
                    // Get ID from Message exchange
                    Exchange ex = message.getExchange();
                    if (null != ex) {
                        Message reqMsg = null;
                        if (MessageUtils.isOutbound(message)) {
                            reqMsg = ex.getInMessage();
                        } else {
                            reqMsg = ex.getOutMessage();
                        }
                        if (null != reqMsg) {
                            correlationId = (String) reqMsg.get(CorrelationIDFeature.MESSAGE_CORRELATION_ID);
                        }
                    }
                }
                // If correlationId is null we should add it to headers
                if (null == correlationId) {
                    if (MethodType.XPATH.equals(mType)) {
                        XPathProcessor proc = new XPathProcessor(cAssertion, message);
                        correlationId = proc.getCorrelationID();
                    } else if (MethodType.CALLBACK.equals(mType)) {
                        CorrelationIDCallbackHandler handler = (CorrelationIDCallbackHandler) message.get(CorrelationIDFeature.CORRELATION_ID_CALLBACK_HANDLER);
                        if (null == handler) {
                            handler = (CorrelationIDCallbackHandler) message.getContextualProperty(CorrelationIDFeature.CORRELATION_ID_CALLBACK_HANDLER);
                        }
                        if (handler != null)
                            correlationId = handler.getCorrelationId();
                    }
                    // request
                    if (null == correlationId) {
                        correlationId = ContextUtils.generateUUID();
                    }
                }
                message.put(CorrelationIDFeature.MESSAGE_CORRELATION_ID, correlationId);
                // MessageUtils.isOutbound(message)) {// RESP_OUT
                if (isRestMessage(message)) {
                    // Add correlationId to http header
                    if (null == CorrelationIdProtocolHeaderCodec.readCorrelationId(message)) {
                        CorrelationIdProtocolHeaderCodec.writeCorrelationId(message, correlationId);
                    }
                } else {
                    // Add correlationId to soap header
                    if (null == CorrelationIdSoapCodec.readCorrelationId(message)) {
                        CorrelationIdSoapCodec.writeCorrelationId(message, correlationId);
                    }
                }
                // }
                ai.setAsserted(true);
            }
        }
    }
}
Also used : MethodType(org.talend.esb.policy.correlation.impl.CorrelationIDAssertion.MethodType) AssertionInfo(org.apache.cxf.ws.policy.AssertionInfo) Message(org.apache.cxf.message.Message) NodeList(org.w3c.dom.NodeList) AssertionInfoMap(org.apache.cxf.ws.policy.AssertionInfoMap) SAAJStreamWriter(org.apache.cxf.binding.soap.saaj.SAAJStreamWriter) Exchange(org.apache.cxf.message.Exchange) CorrelationIDCallbackHandler(org.talend.esb.policy.correlation.CorrelationIDCallbackHandler)

Aggregations

AssertionInfoMap (org.apache.cxf.ws.policy.AssertionInfoMap)65 AssertionInfo (org.apache.cxf.ws.policy.AssertionInfo)44 QName (javax.xml.namespace.QName)15 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)12 Message (org.apache.cxf.message.Message)10 ArrayList (java.util.ArrayList)9 PolicyException (org.apache.cxf.ws.policy.PolicyException)7 Fault (org.apache.cxf.interceptor.Fault)6 WSSecurityException (org.apache.wss4j.common.ext.WSSecurityException)6 Element (org.w3c.dom.Element)6 Exchange (org.apache.cxf.message.Exchange)5 TokenStoreException (org.apache.cxf.ws.security.tokenstore.TokenStoreException)5 Policy (org.apache.neethi.Policy)5 Message (org.apache.cxf.common.i18n.Message)4 SecurityToken (org.apache.cxf.ws.security.tokenstore.SecurityToken)4 SamlAssertionWrapper (org.apache.wss4j.common.saml.SamlAssertionWrapper)4 SOAPException (javax.xml.soap.SOAPException)3 Header (org.apache.cxf.headers.Header)3 PolicyAssertion (org.apache.cxf.ws.policy.PolicyAssertion)3 WSSecUsernameToken (org.apache.wss4j.dom.message.WSSecUsernameToken)3