Search in sources :

Example 76 with WebClient

use of org.apache.cxf.jaxrs.client.WebClient in project cxf by apache.

the class STSRESTTest method testIssueSAML2TokenAppliesTo.

@org.junit.Test
public void testIssueSAML2TokenAppliesTo() throws Exception {
    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = STSRESTTest.class.getResource("cxf-client.xml");
    Bus bus = bf.createBus(busFile.toString());
    BusFactory.setDefaultBus(bus);
    BusFactory.setThreadDefaultBus(bus);
    String address = "https://localhost:" + STSPORT + "/SecurityTokenService/token";
    WebClient client = WebClient.create(address, busFile.toString());
    client.accept("application/xml");
    client.path("saml2.0");
    client.query("appliesTo", DEFAULT_ADDRESS);
    Response response = client.get();
    Document assertionDoc = response.readEntity(Document.class);
    assertNotNull(assertionDoc);
    // Process the token
    List<WSSecurityEngineResult> results = processToken(assertionDoc.getDocumentElement());
    assertTrue(results != null && results.size() == 1);
    SamlAssertionWrapper assertion = (SamlAssertionWrapper) results.get(0).get(WSSecurityEngineResult.TAG_SAML_ASSERTION);
    assertTrue(assertion != null);
    assertTrue(assertion.getSaml2() != null && assertion.getSaml1() == null);
    assertTrue(assertion.isSigned());
    bus.shutdown(true);
}
Also used : Response(javax.ws.rs.core.Response) Bus(org.apache.cxf.Bus) SpringBusFactory(org.apache.cxf.bus.spring.SpringBusFactory) SamlAssertionWrapper(org.apache.wss4j.common.saml.SamlAssertionWrapper) Document(org.w3c.dom.Document) WebClient(org.apache.cxf.jaxrs.client.WebClient) WSSecurityEngineResult(org.apache.wss4j.dom.engine.WSSecurityEngineResult) URL(java.net.URL)

Example 77 with WebClient

use of org.apache.cxf.jaxrs.client.WebClient in project cxf by apache.

the class STSRESTTest method testValidateSAMLAndIssueJWT.

@org.junit.Test
public void testValidateSAMLAndIssueJWT() throws Exception {
    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = STSRESTTest.class.getResource("cxf-client.xml");
    Bus bus = bf.createBus(busFile.toString());
    BusFactory.setDefaultBus(bus);
    BusFactory.setThreadDefaultBus(bus);
    String address = "https://localhost:" + STSPORT + "/SecurityTokenService/token";
    WebClient client = WebClient.create(address, busFile.toString());
    client.accept("application/xml");
    client.path("saml2.0");
    // 1. Get a token via GET
    Response response = client.get();
    Document assertionDoc = response.readEntity(Document.class);
    assertNotNull(assertionDoc);
    // 2. Now validate it in the STS using POST
    client = WebClient.create(address, busFile.toString());
    client.type("application/xml").accept("application/xml");
    client.query("action", "validate");
    // 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 + "/Validate");
    writer.writeEndElement();
    writer.writeStartElement("wst", "TokenType", namespace);
    writer.writeCharacters(JWT_TOKEN_TYPE);
    writer.writeEndElement();
    writer.writeStartElement("wst", "ValidateTarget", namespace);
    StaxUtils.copy(assertionDoc.getDocumentElement(), writer);
    writer.writeEndElement();
    writer.writeEndElement();
    response = client.post(new DOMSource(writer.getDocument().getDocumentElement()));
    RequestSecurityTokenResponseType securityResponse = response.readEntity(RequestSecurityTokenResponseType.class);
    StatusType status = null;
    for (Object obj : securityResponse.getAny()) {
        if (obj instanceof JAXBElement<?>) {
            JAXBElement<?> jaxbElement = (JAXBElement<?>) obj;
            if ("Status".equals(jaxbElement.getName().getLocalPart())) {
                status = (StatusType) jaxbElement.getValue();
                break;
            }
        }
    }
    assertNotNull(status);
    // Check the token was valid
    String validCode = "http://docs.oasis-open.org/ws-sx/ws-trust/200512/status/valid";
    assertEquals(validCode, status.getCode());
    // Check the token
    RequestedSecurityTokenType requestedSecurityToken = getRequestedSecurityToken(securityResponse);
    assertNotNull(requestedSecurityToken);
    String token = ((Element) requestedSecurityToken.getAny()).getTextContent();
    assertNotNull(token);
    validateJWTToken(token, null);
    bus.shutdown(true);
}
Also used : Bus(org.apache.cxf.Bus) W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) DOMSource(javax.xml.transform.dom.DOMSource) JAXBElement(javax.xml.bind.JAXBElement) Element(org.w3c.dom.Element) RequestSecurityTokenResponseType(org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenResponseType) JAXBElement(javax.xml.bind.JAXBElement) RequestedSecurityTokenType(org.apache.cxf.ws.security.sts.provider.model.RequestedSecurityTokenType) Document(org.w3c.dom.Document) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL) Response(javax.ws.rs.core.Response) SpringBusFactory(org.apache.cxf.bus.spring.SpringBusFactory) StatusType(org.apache.cxf.ws.security.sts.provider.model.StatusType)

Example 78 with WebClient

use of org.apache.cxf.jaxrs.client.WebClient in project cxf by apache.

the class JAXRSSoapBookTest method testServiceListingsAndWadl.

@Test
public void testServiceListingsAndWadl() throws Exception {
    String listings = getStringFromInputStream(getHttpInputStream("http://localhost:" + PORT + "/test/services"));
    assertNotNull(listings);
    assertTrue(listings.contains("http://localhost:" + PORT + "/test/services/soap/bookservice?wsdl"));
    assertFalse(listings.contains("http://localhost:" + PORT + "/test/services/soap/bookservice2?wsdl"));
    assertTrue(listings.contains("http://localhost:" + PORT + "/test/services/rest?_wadl"));
    assertEquals(200, WebClient.create("http://localhost:" + PORT + "/test/services/rest?_wadl&type=xml").get().getStatus());
    assertTrue(listings.contains("http://localhost:" + PORT + "/test/services/rest2?_wadl"));
    assertEquals(200, WebClient.create("http://localhost:" + PORT + "/test/services/rest2?_wadl&type=xml").get().getStatus());
    assertFalse(listings.contains("http://localhost:" + PORT + "/test/services/rest3?_wadl"));
    assertFalse(listings.contains("Atom Log Feed"));
    WebClient webClient = WebClient.create("http://localhost:" + PORT + "/test/services/rest3?_wadl");
    assertEquals(404, webClient.get().getStatus());
}
Also used : WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.junit.Test)

Example 79 with WebClient

use of org.apache.cxf.jaxrs.client.WebClient in project cxf by apache.

the class JAXRSSoapBookTest method testGetBookWebClientForm2.

@Test
public void testGetBookWebClientForm2() throws Exception {
    String baseAddress = "http://localhost:" + PORT + "/test/services/rest/bookstore/books/679/subresource3";
    WebClient wc = WebClient.create(baseAddress);
    Form f = new Form(new MetadataMap<String, String>());
    f.param("id", "679").param("name", "CXF in Action - ").param("name", "679");
    Book b = readBook((InputStream) wc.accept("application/xml").form(f).getEntity());
    assertEquals(679, b.getId());
    assertEquals("CXF in Action - 679", b.getName());
}
Also used : Form(javax.ws.rs.core.Form) WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.junit.Test)

Example 80 with WebClient

use of org.apache.cxf.jaxrs.client.WebClient in project cxf by apache.

the class JAXRSSoapBookTest method testPostBookTransformV2.

@Test
public void testPostBookTransformV2() throws Exception {
    String address = "http://localhost:" + PORT + "/test/v2/rest-transform/bookstore/books";
    WebClient client = WebClient.create(address);
    Book book = client.type("application/xml").accept("text/xml").post(new Book(), Book.class);
    assertEquals(124L, book.getId());
}
Also used : WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.junit.Test)

Aggregations

WebClient (org.apache.cxf.jaxrs.client.WebClient)723 Test (org.junit.Test)400 Response (javax.ws.rs.core.Response)351 URL (java.net.URL)198 HashMap (java.util.HashMap)100 Book (org.apache.cxf.systest.jaxrs.security.Book)94 ArrayList (java.util.ArrayList)88 JacksonJsonProvider (com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider)87 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)77 ClientAccessToken (org.apache.cxf.rs.security.oauth2.common.ClientAccessToken)60 Bus (org.apache.cxf.Bus)48 SpringBusFactory (org.apache.cxf.bus.spring.SpringBusFactory)46 Test (org.testng.annotations.Test)46 Form (javax.ws.rs.core.Form)44 JwtToken (org.apache.cxf.rs.security.jose.jwt.JwtToken)42 JwtClaims (org.apache.cxf.rs.security.jose.jwt.JwtClaims)40 JwtAuthenticationClientFilter (org.apache.cxf.rs.security.jose.jaxrs.JwtAuthenticationClientFilter)32 InputStream (java.io.InputStream)28 ResponseProcessingException (javax.ws.rs.client.ResponseProcessingException)28 Document (org.w3c.dom.Document)27