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);
}
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;
}
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);
}
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;
}
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);
}
Aggregations