Search in sources :

Example 1 with W3CDOMStreamWriter

use of org.apache.cxf.staxutils.W3CDOMStreamWriter in project camel by apache.

the class CxfUtils method elementToString.

public static String elementToString(Element element) throws Exception {
    Map<String, String> namespaces = new HashMap<String, String>();
    visitNodesForNameSpace(element, namespaces);
    W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
    writeElement(element, writer, namespaces);
    XmlConverter converter = new XmlConverter();
    return converter.toString(converter.toDOMSource(writer.getDocument()), null);
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) HashMap(java.util.HashMap) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter)

Example 2 with W3CDOMStreamWriter

use of org.apache.cxf.staxutils.W3CDOMStreamWriter in project ddf by codice.

the class SamlProtocol method convertDomImplementation.

//converting the DOM impl is necessary because OpenSAML expects a particular implementation
public static Element convertDomImplementation(Element node) throws XMLStreamException {
    if (DOMUtils.createDocument().getImplementation() != node.getOwnerDocument().getImplementation()) {
        W3CDOMStreamWriter xmlStreamWriter = new W3CDOMStreamWriter();
        StaxUtils.copy(node, xmlStreamWriter);
        node = xmlStreamWriter.getDocument().getDocumentElement();
    }
    return node;
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter)

Example 3 with W3CDOMStreamWriter

use of org.apache.cxf.staxutils.W3CDOMStreamWriter in project ddf by codice.

the class StsIssueTest method testBearerPkiTokenSaml2.

/**
     * Test the User PKI Token
     */
public void testBearerPkiTokenSaml2(StsPortTypes portType) throws Exception {
    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = StsIssueTest.class.getResource("/cxf-client.xml");
    Bus bus = bf.createBus(busFile.toString());
    SpringBusFactory.setDefaultBus(bus);
    SpringBusFactory.setThreadDefaultBus(bus);
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.newDocument();
    // Build the Claims object
    W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
    writer.writeStartElement(WST, CLAIMS, STSUtils.WST_NS_05_12);
    writer.writeNamespace(WST, STSUtils.WST_NS_05_12);
    writer.writeNamespace(IC, IDENTITY_URI);
    writer.writeAttribute(DIALECT, IDENTITY_URI);
    // Add the Role claim
    writer.writeStartElement(IC, CLAIM_TYPE, IDENTITY_URI);
    writer.writeAttribute("URI", "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/role");
    writer.writeEndElement();
    Element claims = writer.getDocument().getDocumentElement();
    // Alerternatively we can use a certificate to request a SAML
    X509Security oboToken = new X509Security(doc);
    Crypto crypto = CryptoFactory.getInstance("clientKeystore.properties");
    CryptoType cryptoType = new CryptoType(CryptoType.TYPE.ALIAS);
    cryptoType.setAlias("client");
    X509Certificate[] certs = crypto.getX509Certificates(cryptoType);
    if (null != certs) {
        oboToken.setX509Certificate(certs[0]);
        // Get a token
        SecurityToken token = requestSecurityToken(SAML2_TOKEN_TYPE, BEARER_KEYTYPE, oboToken.getElement(), bus, StsAddresses.valueOf(portType.toString()).toString(), WsdlLocations.valueOf(portType.toString()).toString(), EndPoints.valueOf(portType.toString()).toString(), claims);
        if (token != null) {
            validateSecurityToken(token);
        }
    }
    bus.shutdown(true);
}
Also used : Bus(org.apache.cxf.Bus) W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Element(org.w3c.dom.Element) CryptoType(org.apache.wss4j.common.crypto.CryptoType) Document(org.w3c.dom.Document) URL(java.net.URL) X509Certificate(java.security.cert.X509Certificate) SecurityToken(org.apache.cxf.ws.security.tokenstore.SecurityToken) Crypto(org.apache.wss4j.common.crypto.Crypto) SpringBusFactory(org.apache.cxf.bus.spring.SpringBusFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) X509Security(org.apache.wss4j.common.token.X509Security)

Example 4 with W3CDOMStreamWriter

use of org.apache.cxf.staxutils.W3CDOMStreamWriter in project ddf by codice.

the class AbstractStsRealm method createClaimsElement.

/**
     * Create the claims element with the claims provided in the STS client configuration in the
     * admin console.
     */
protected Element createClaimsElement() {
    Element claimsElement = null;
    List<String> claims = new ArrayList<>();
    claims.addAll(getClaims());
    if (contextPolicyManager != null) {
        Collection<ContextPolicy> contextPolicies = contextPolicyManager.getAllContextPolicies();
        Set<String> attributes = new LinkedHashSet<>();
        if (contextPolicies != null && contextPolicies.size() > 0) {
            for (ContextPolicy contextPolicy : contextPolicies) {
                attributes.addAll(contextPolicy.getAllowedAttributeNames());
            }
        }
        if (attributes.size() > 0) {
            claims.addAll(attributes);
        }
    }
    if (claims.size() != 0) {
        W3CDOMStreamWriter writer = null;
        try {
            writer = new W3CDOMStreamWriter();
            writer.writeStartElement("wst", "Claims", STSUtils.WST_NS_05_12);
            writer.writeNamespace("wst", STSUtils.WST_NS_05_12);
            writer.writeNamespace("ic", "http://schemas.xmlsoap.org/ws/2005/05/identity");
            writer.writeAttribute("Dialect", "http://schemas.xmlsoap.org/ws/2005/05/identity");
            for (String claim : claims) {
                LOGGER.trace("Claim: {}", claim);
                writer.writeStartElement("ic", "ClaimType", "http://schemas.xmlsoap.org/ws/2005/05/identity");
                writer.writeAttribute("Uri", claim);
                writer.writeAttribute("Optional", "true");
                writer.writeEndElement();
            }
            writer.writeEndElement();
            claimsElement = writer.getDocument().getDocumentElement();
        } catch (XMLStreamException e) {
            String msg = "Unable to create claims. Subjects will not have any attributes. Check STS Client configuration.";
            LOGGER.warn(msg, e);
            claimsElement = null;
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (XMLStreamException ignore) {
                //ignore
                }
            }
        }
        if (LOGGER.isDebugEnabled()) {
            if (claimsElement != null) {
                LOGGER.debug("Claims: {}", getFormattedXml(claimsElement));
            }
        }
    } else {
        LOGGER.debug("There are no claims to process.");
        claimsElement = null;
    }
    return claimsElement;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) XMLStreamException(javax.xml.stream.XMLStreamException) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) ContextPolicy(org.codice.ddf.security.policy.context.ContextPolicy)

Example 5 with W3CDOMStreamWriter

use of org.apache.cxf.staxutils.W3CDOMStreamWriter in project cas by apereo.

the class DefaultRelyingPartyTokenProducer method mapAttributesToRequestedClaims.

@SneakyThrows
private void mapAttributesToRequestedClaims(final WSFederationRegisteredService service, final SecurityTokenServiceClient sts, final Assertion assertion) {
    val writer = new W3CDOMStreamWriter();
    writer.writeStartElement("wst", "Claims", STSUtils.WST_NS_05_12);
    writer.writeNamespace("wst", STSUtils.WST_NS_05_12);
    writer.writeNamespace("ic", WSFederationConstants.HTTP_SCHEMAS_XMLSOAP_ORG_WS_2005_05_IDENTITY);
    writer.writeAttribute("Dialect", WSFederationConstants.HTTP_SCHEMAS_XMLSOAP_ORG_WS_2005_05_IDENTITY);
    val attributes = assertion.getPrincipal().getAttributes();
    LOGGER.debug("Mapping principal attributes [{}] to claims for service [{}]", attributes, service);
    attributes.forEach(Unchecked.biConsumer((k, v) -> {
        val claimName = ProtocolAttributeEncoder.decodeAttribute(k);
        if (WSFederationClaims.contains(claimName)) {
            val uri = WSFederationClaims.valueOf(k).getUri();
            LOGGER.debug("Requested claim [{}] mapped to [{}]", k, uri);
            writeAttributeValue(writer, uri, v, service);
        } else if (WSFederationClaims.containsUri(claimName)) {
            LOGGER.debug("Requested claim [{}] directly mapped to [{}]", k, claimName);
            writeAttributeValue(writer, claimName, v, service);
        } else if (customClaims.contains(claimName)) {
            LOGGER.debug("Requested custom claim [{}]", claimName);
            writeAttributeValue(writer, claimName, v, service);
        } else {
            LOGGER.debug("Requested claim [{}] is not defined/supported by CAS", claimName);
            writeAttributeValue(writer, WSFederationConstants.getClaimInCasNamespace(claimName), v, service);
        }
    }));
    writer.writeEndElement();
    val claims = writer.getDocument().getDocumentElement();
    sts.setClaims(claims);
}
Also used : lombok.val(lombok.val) DOMSource(javax.xml.transform.dom.DOMSource) SneakyThrows(lombok.SneakyThrows) CipherExecutor(org.apereo.cas.util.crypto.CipherExecutor) StreamResult(javax.xml.transform.stream.StreamResult) RequiredArgsConstructor(lombok.RequiredArgsConstructor) WSFederationRequest(org.apereo.cas.ws.idp.web.WSFederationRequest) BooleanUtils(org.apache.commons.lang3.BooleanUtils) Assertion(org.jasig.cas.client.validation.Assertion) ProtocolAttributeEncoder(org.apereo.cas.authentication.ProtocolAttributeEncoder) LoggingUtils(org.apereo.cas.util.LoggingUtils) HttpServletRequest(javax.servlet.http.HttpServletRequest) CollectionUtils(org.apereo.cas.util.CollectionUtils) XMLConstants(javax.xml.XMLConstants) ProcessingException(org.apache.cxf.fediz.core.exception.ProcessingException) Unchecked(org.jooq.lambda.Unchecked) WSFederationConstants(org.apereo.cas.ws.idp.WSFederationConstants) STSUtils(org.apache.cxf.ws.security.trust.STSUtils) StringWriter(java.io.StringWriter) lombok.val(lombok.val) Set(java.util.Set) SecurityConstants(org.apache.cxf.rt.security.SecurityConstants) OutputKeys(javax.xml.transform.OutputKeys) WSFederationClaims(org.apereo.cas.ws.idp.WSFederationClaims) W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) SecurityToken(org.apache.cxf.ws.security.tokenstore.SecurityToken) Slf4j(lombok.extern.slf4j.Slf4j) Element(org.w3c.dom.Element) SecurityTokenServiceClientBuilder(org.apereo.cas.authentication.SecurityTokenServiceClientBuilder) SecurityTokenServiceClient(org.apereo.cas.authentication.SecurityTokenServiceClient) TransformerFactory(javax.xml.transform.TransformerFactory) SoapFault(org.apache.cxf.binding.soap.SoapFault) W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) SneakyThrows(lombok.SneakyThrows)

Aggregations

W3CDOMStreamWriter (org.apache.cxf.staxutils.W3CDOMStreamWriter)60 Element (org.w3c.dom.Element)29 DOMSource (javax.xml.transform.dom.DOMSource)24 XMLStreamException (javax.xml.stream.XMLStreamException)15 Document (org.w3c.dom.Document)14 WebClient (org.apache.cxf.jaxrs.client.WebClient)9 Node (org.w3c.dom.Node)9 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)8 Fault (org.apache.cxf.interceptor.Fault)8 XMLStreamReader (javax.xml.stream.XMLStreamReader)7 BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)7 RequestSecurityTokenResponseType (org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenResponseType)7 SecurityToken (org.apache.cxf.ws.security.tokenstore.SecurityToken)7 JAXBElement (javax.xml.bind.JAXBElement)6 JAXBException (javax.xml.bind.JAXBException)5 SOAPMessage (javax.xml.soap.SOAPMessage)5 SoapFault (org.apache.cxf.binding.soap.SoapFault)5 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)5 InputStream (java.io.InputStream)4 HashMap (java.util.HashMap)4