Search in sources :

Example 61 with WebClient

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

the class CodeAuthSupplier method getAuthorization.

public String getAuthorization(AuthorizationPolicy authPolicy, URI currentURI, Message message, String fullHeader) {
    if (code != null) {
        synchronized (tokenSupplier) {
            if (tokenSupplier.getClientAccessToken().getTokenKey() == null) {
                WebClient wc = tokenSupplier.createAccessTokenServiceClient();
                ClientAccessToken at = OAuthClientUtils.getAccessToken(wc, tokenSupplier.getConsumer(), new AuthorizationCodeGrant(code));
                code = null;
                tokenSupplier.setClientAccessToken(at);
            }
        }
    }
    return tokenSupplier.getAuthorization(authPolicy, currentURI, message, fullHeader);
}
Also used : AuthorizationCodeGrant(org.apache.cxf.rs.security.oauth2.grants.code.AuthorizationCodeGrant) ClientAccessToken(org.apache.cxf.rs.security.oauth2.common.ClientAccessToken) WebClient(org.apache.cxf.jaxrs.client.WebClient)

Example 62 with WebClient

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

the class AccessTokenValidatorClient method validateAccessToken.

public AccessTokenValidation validateAccessToken(MessageContext mc, String authScheme, String authSchemeData, MultivaluedMap<String, String> extraProps) throws OAuthServiceException {
    WebClient client = WebClient.fromClient(tokenValidatorClient, true);
    MultivaluedMap<String, String> props = new MetadataMap<String, String>();
    props.putSingle(OAuthConstants.AUTHORIZATION_SCHEME_TYPE, authScheme);
    props.putSingle(OAuthConstants.AUTHORIZATION_SCHEME_DATA, authSchemeData);
    if (extraProps != null) {
        props.putAll(extraProps);
    }
    try {
        return client.post(props, AccessTokenValidation.class);
    } catch (WebApplicationException ex) {
        throw new OAuthServiceException(ex);
    }
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) WebApplicationException(javax.ws.rs.WebApplicationException) OAuthServiceException(org.apache.cxf.rs.security.oauth2.provider.OAuthServiceException) WebClient(org.apache.cxf.jaxrs.client.WebClient)

Example 63 with WebClient

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

the class CustomParameterTest method testCustomParameterToRESTInterface.

@org.junit.Test
public void testCustomParameterToRESTInterface() throws Exception {
    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = CustomParameterTest.class.getResource("cxf-client.xml");
    Bus bus = bf.createBus(busFile.toString());
    BusFactory.setDefaultBus(bus);
    BusFactory.setThreadDefaultBus(bus);
    String address = "https://localhost:" + STSPORT + "/SecurityTokenServiceREST/token";
    WebClient client = WebClient.create(address, busFile.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"));
    bus.shutdown(true);
}
Also used : Response(javax.ws.rs.core.Response) Bus(org.apache.cxf.Bus) W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) DOMSource(javax.xml.transform.dom.DOMSource) SpringBusFactory(org.apache.cxf.bus.spring.SpringBusFactory) JAXBElement(javax.xml.bind.JAXBElement) Element(org.w3c.dom.Element) RequestSecurityTokenResponseType(org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenResponseType) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL)

Example 64 with WebClient

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

the class JAASTest method doubleIt.

private static void doubleIt(String username, String password, String address, boolean authFailureExpected) {
    final String configLocation = "org/apache/cxf/systest/sts/jaas/cxf-client.xml";
    final int numToDouble = 25;
    WebClient client = null;
    if (username != null && password != null) {
        client = WebClient.create(address, username, password, configLocation);
    } else {
        client = WebClient.create(address, configLocation);
    }
    client.type("text/plain").accept("text/plain");
    try {
        int resp = client.post(numToDouble, Integer.class);
        if (authFailureExpected) {
            throw new RuntimeException("Exception expected");
        }
        org.junit.Assert.assertEquals(2 * numToDouble, resp);
    } catch (WebApplicationException ex) {
        if (!authFailureExpected) {
            throw new RuntimeException("Unexpected exception");
        }
        org.junit.Assert.assertEquals(500, ex.getResponse().getStatus());
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) WebClient(org.apache.cxf.jaxrs.client.WebClient)

Example 65 with WebClient

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

the class STSRESTTest method testExplicitlyIssueSAML2TokenViaPOST.

@org.junit.Test
public void testExplicitlyIssueSAML2TokenViaPOST() 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.type("application/xml").accept("application/xml");
    client.query("action", "issue");
    // 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.writeEndElement();
    Response response = client.post(new DOMSource(writer.getDocument().getDocumentElement()));
    RequestSecurityTokenResponseType securityResponse = response.readEntity(RequestSecurityTokenResponseType.class);
    validateSAMLSecurityTokenResponse(securityResponse, true);
    bus.shutdown(true);
}
Also used : Response(javax.ws.rs.core.Response) Bus(org.apache.cxf.Bus) W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) DOMSource(javax.xml.transform.dom.DOMSource) SpringBusFactory(org.apache.cxf.bus.spring.SpringBusFactory) RequestSecurityTokenResponseType(org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenResponseType) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL)

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