use of org.apache.cxf.staxutils.W3CDOMStreamWriter in project cxf by apache.
the class CustomParameterTest method testCustomParameterToRESTInterface.
@org.junit.Test
public void testCustomParameterToRESTInterface() throws Exception {
String address = "https://localhost:" + STSPORT + "/SecurityTokenServiceREST/token";
WebClient client = WebClient.create(address, getClass().getResource("cxf-client.xml").toString());
client.type("application/xml").accept("application/xml");
// Create RequestSecurityToken
W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
String namespace = STSUtils.WST_NS_05_12;
writer.writeStartElement("wst", "RequestSecurityToken", namespace);
writer.writeNamespace("wst", namespace);
writer.writeStartElement("wst", "RequestType", namespace);
writer.writeCharacters(namespace + "/Issue");
writer.writeEndElement();
writer.writeStartElement("wst", "TokenType", namespace);
writer.writeCharacters(SAML2_TOKEN_TYPE);
writer.writeEndElement();
writer.writeStartElement("wst", "Claims", namespace);
writer.writeAttribute("Dialect", "http://schemas.xmlsoap.org/ws/2005/05/identity");
writer.writeStartElement("ic", "ClaimType", "http://schemas.xmlsoap.org/ws/2005/05/identity");
writer.writeAttribute("Uri", "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/role");
writer.writeEndElement();
writer.writeEndElement();
// Add custom content to the RST
writer.writeStartElement("", "realm", "http://cxf.apache.org/custom");
writer.writeCharacters("custom-realm");
writer.writeEndElement();
writer.writeEndElement();
Response response = client.post(new DOMSource(writer.getDocument().getDocumentElement()));
RequestSecurityTokenResponseType securityResponse = response.readEntity(RequestSecurityTokenResponseType.class);
Element assertion = validateSAMLSecurityTokenResponse(securityResponse, true);
assertTrue(DOM2Writer.nodeToString(assertion).contains("admin-user"));
}
use of org.apache.cxf.staxutils.W3CDOMStreamWriter in project cxf by apache.
the class AbstractXmlSecOutInterceptor method getDomDocument.
@SuppressWarnings("unchecked")
private Document getDomDocument(Message m) throws Exception {
Object body = getRequestBody(m);
if (body == null) {
return null;
}
if (body instanceof Document) {
return (Document) body;
}
if (body instanceof DOMSource) {
return (Document) ((DOMSource) body).getNode();
}
ProviderFactory pf = ProviderFactory.getInstance(m);
Object providerObject = pf.createMessageBodyWriter(body.getClass(), body.getClass(), new Annotation[] {}, MediaType.APPLICATION_XML_TYPE, m);
if (!(providerObject instanceof JAXBElementProvider)) {
return null;
}
JAXBElementProvider<Object> provider = (JAXBElementProvider<Object>) providerObject;
W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
m.setContent(XMLStreamWriter.class, writer);
provider.writeTo(body, body.getClass(), new Annotation[] {}, MediaType.APPLICATION_XML_TYPE, (MultivaluedMap<String, Object>) m.get(Message.PROTOCOL_HEADERS), null);
return writer.getDocument();
}
use of org.apache.cxf.staxutils.W3CDOMStreamWriter in project cxf by apache.
the class SecurityTokenTest method testParseLifetimeElement.
@org.junit.Test
public void testParseLifetimeElement() throws Exception {
String key = "key";
Element tokenElement = DOMUtils.createDocument().createElement("token");
// Create Lifetime
W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
Instant created = Instant.now().truncatedTo(ChronoUnit.MILLIS);
Instant expires = created.plusSeconds(20L);
writer.writeStartElement("wst", "Lifetime", WST_NS_05_12);
writer.writeStartElement("wsu", "Created", WSU_NS);
writer.writeCharacters(created.atZone(ZoneOffset.UTC).format(DateUtil.getDateTimeFormatter(true)));
writer.writeEndElement();
writer.writeStartElement("wsu", "Expires", WSU_NS);
writer.writeCharacters(expires.atZone(ZoneOffset.UTC).format(DateUtil.getDateTimeFormatter(true)));
writer.writeEndElement();
writer.writeEndElement();
SecurityToken token = new SecurityToken(key, tokenElement, writer.getDocument().getDocumentElement());
assertEquals(key, token.getId());
assertEquals(created, token.getCreated());
assertEquals(expires, token.getExpires());
}
use of org.apache.cxf.staxutils.W3CDOMStreamWriter in project cxf by apache.
the class SecurityTokenTest method testLifetimeNoCreated.
@org.junit.Test
public void testLifetimeNoCreated() throws Exception {
String key = "key";
Element tokenElement = DOMUtils.createDocument().createElement("token");
// Create Lifetime
W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
Instant created = Instant.now().truncatedTo(ChronoUnit.MILLIS);
Instant expires = created.plusSeconds(20L);
writer.writeStartElement("wst", "Lifetime", WST_NS_05_12);
writer.writeStartElement("wsu", "Expires", WSU_NS);
writer.writeCharacters(expires.atZone(ZoneOffset.UTC).format(DateUtil.getDateTimeFormatter(true)));
writer.writeEndElement();
writer.writeEndElement();
SecurityToken token = new SecurityToken(key, tokenElement, writer.getDocument().getDocumentElement());
assertEquals(key, token.getId());
// It should default to the current time
assertNotNull(token.getCreated());
assertEquals(expires, token.getExpires());
}
use of org.apache.cxf.staxutils.W3CDOMStreamWriter in project cxf by apache.
the class SecurityTokenTest method testLifetimeNoExpires.
@org.junit.Test
public void testLifetimeNoExpires() throws Exception {
String key = "key";
Element tokenElement = DOMUtils.createDocument().createElement("token");
// Create Lifetime
W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
Instant created = Instant.now().truncatedTo(ChronoUnit.MILLIS);
writer.writeStartElement("wst", "Lifetime", WST_NS_05_12);
writer.writeStartElement("wsu", "Created", WSU_NS);
writer.writeCharacters(created.atZone(ZoneOffset.UTC).format(DateUtil.getDateTimeFormatter(true)));
writer.writeEndElement();
writer.writeEndElement();
SecurityToken token = new SecurityToken(key, tokenElement, writer.getDocument().getDocumentElement());
assertEquals(key, token.getId());
assertEquals(created, token.getCreated());
assertNull(token.getExpires());
}
Aggregations