Search in sources :

Example 1 with CreateSignatureInterceptor

use of org.apache.cxf.rs.security.httpsignature.filters.CreateSignatureInterceptor in project cxf by apache.

the class SpecExamplesTest method interceptorDefaultTest.

@Test
public void interceptorDefaultTest() {
    URI uri = URI.create("https://www.example.com/foo?param=value&pet=dog");
    String method = "POST";
    MessageSigner messageSigner = new MessageSigner(keyProvider, "Test", Collections.singletonList("Date"));
    CreateSignatureInterceptor interceptor = new CreateSignatureInterceptor();
    interceptor.setMessageSigner(messageSigner);
    Map<String, List<String>> headers = createMockHeaders();
    MultivaluedMap<String, Object> requestHeaders = new MultivaluedHashMap<>();
    MultivaluedMap<String, String> requestStringHeaders = new MultivaluedHashMap<>();
    headers.forEach((header, value) -> {
        requestHeaders.add(header, value.get(0));
        requestStringHeaders.add(header, value.get(0));
    });
    final Message message = new MessageImpl();
    doInPhaseInterceptor(message, () -> {
        ClientRequestContext requestContext = getClientRequestContextMock(uri, method, requestHeaders);
        interceptor.filter(requestContext);
        String signatureHeader = (String) requestHeaders.get("Signature").get(0);
        requestStringHeaders.add("Signature", signatureHeader);
        String expectedHeader = "keyId=\"Test\",algorithm=\"rsa-sha256\"," + "signature=\"SjWJWbWN7i0wzBvtPl8rbASWz5xQW6mcJmn+ibttBqtifLN7Sazz" + "6m79cNfwwb8DMJ5cou1s7uEGKKCs+FLEEaDV5lp7q25WqS+lavg7T8hc0GppauB" + "6hbgEKTwblDHYGEtbGmtdHgVCk9SuS13F0hZ8FD0k/5OxEPXe5WozsbM=\"";
        assertEquals(signatureHeader.replaceAll("headers=\"date\",", ""), expectedHeader);
        // Verify that the request signature can be verified by the filter
        MessageVerifier messageVerifier = new MessageVerifier(keyId -> publicKey);
        messageVerifier.setAddDefaultRequiredHeaders(false);
        messageVerifier.setSecurityProvider(new MockSecurityProvider());
        messageVerifier.setAlgorithmProvider(new MockAlgorithmProvider());
        VerifySignatureFilter verifySignatureFilter = new VerifySignatureFilter();
        verifySignatureFilter.setMessageVerifier(messageVerifier);
        ContainerRequestContext containerRequestContext = getContainerRequestContextMock(uri, method, requestStringHeaders);
        verifySignatureFilter.filter(containerRequestContext);
    });
}
Also used : ClientRequestContext(javax.ws.rs.client.ClientRequestContext) MockAlgorithmProvider(org.apache.cxf.rs.security.httpsignature.provider.MockAlgorithmProvider) Message(org.apache.cxf.message.Message) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) VerifySignatureFilter(org.apache.cxf.rs.security.httpsignature.filters.VerifySignatureFilter) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) CreateSignatureInterceptor(org.apache.cxf.rs.security.httpsignature.filters.CreateSignatureInterceptor) MockSecurityProvider(org.apache.cxf.rs.security.httpsignature.provider.MockSecurityProvider) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 2 with CreateSignatureInterceptor

use of org.apache.cxf.rs.security.httpsignature.filters.CreateSignatureInterceptor in project cxf by apache.

the class SpecExamplesTest method interceptorAllHeadersTest.

@Test
public void interceptorAllHeadersTest() {
    URI uri = URI.create("https://www.example.com/foo?param=value&pet=dog");
    String method = "POST";
    MessageSigner messageSigner = new MessageSigner(keyProvider, "Test", Arrays.asList("(request-target)", "host", "date", "content-type", "digest", "content-length"));
    CreateSignatureInterceptor interceptor = new CreateSignatureInterceptor();
    interceptor.setMessageSigner(messageSigner);
    Map<String, List<String>> headers = createMockHeaders();
    MultivaluedMap<String, Object> requestHeaders = new MultivaluedHashMap<>();
    MultivaluedMap<String, String> requestStringHeaders = new MultivaluedHashMap<>();
    headers.forEach((header, value) -> {
        requestHeaders.add(header, value.get(0));
        requestStringHeaders.add(header, value.get(0));
    });
    final Message message = new MessageImpl();
    doInPhaseInterceptor(message, () -> {
        ClientRequestContext requestContext = getClientRequestContextMock(uri, method, requestHeaders);
        interceptor.filter(requestContext);
        String signatureHeader = (String) requestHeaders.get("Signature").get(0);
        requestStringHeaders.add("Signature", signatureHeader);
        String expectedHeader = "keyId=\"Test\",algorithm=\"rsa-sha256\"," + "headers=\"(request-target) host date content-type digest content-length\"," + "signature=\"vSdrb+dS3EceC9bcwHSo4MlyKS59iFIrhgYkz8+oVLEEzmYZZvRs" + "8rgOp+63LEM3v+MFHB32NfpB2bEKBIvB1q52LaEUHFv120V01IL+TAD48XaERZF" + "ukWgHoBTLMhYS2Gb51gWxpeIq8knRmPnYePbF5MOkR0Zkly4zKH7s1dE=\"";
        assertEquals(signatureHeader, expectedHeader);
        // Verify that the request signature can be verified by the filter
        MessageVerifier messageVerifier = new MessageVerifier(keyId -> publicKey);
        messageVerifier.setAddDefaultRequiredHeaders(false);
        messageVerifier.setSecurityProvider(new MockSecurityProvider());
        messageVerifier.setAlgorithmProvider(new MockAlgorithmProvider());
        VerifySignatureFilter verifySignatureFilter = new VerifySignatureFilter();
        verifySignatureFilter.setMessageVerifier(messageVerifier);
        ContainerRequestContext containerRequestContext = getContainerRequestContextMock(uri, method, requestStringHeaders);
        InputStream stream = new ByteArrayInputStream("{\"hello\": \"world\"}".getBytes(StandardCharsets.UTF_8));
        Mockito.when(containerRequestContext.getEntityStream()).thenReturn(stream);
        verifySignatureFilter.filter(containerRequestContext);
    });
}
Also used : ClientRequestContext(javax.ws.rs.client.ClientRequestContext) MockAlgorithmProvider(org.apache.cxf.rs.security.httpsignature.provider.MockAlgorithmProvider) Message(org.apache.cxf.message.Message) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) VerifySignatureFilter(org.apache.cxf.rs.security.httpsignature.filters.VerifySignatureFilter) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) CreateSignatureInterceptor(org.apache.cxf.rs.security.httpsignature.filters.CreateSignatureInterceptor) MockSecurityProvider(org.apache.cxf.rs.security.httpsignature.provider.MockSecurityProvider) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 3 with CreateSignatureInterceptor

use of org.apache.cxf.rs.security.httpsignature.filters.CreateSignatureInterceptor in project cxf by apache.

the class SpecExamplesTest method interceptorBasicTest.

@Test
public void interceptorBasicTest() {
    URI uri = URI.create("https://www.example.com/foo?param=value&pet=dog");
    String method = "POST";
    MessageSigner messageSigner = new MessageSigner(keyProvider, "Test", Arrays.asList("(request-target)", "host", "Date"));
    CreateSignatureInterceptor interceptor = new CreateSignatureInterceptor();
    interceptor.setMessageSigner(messageSigner);
    Map<String, List<String>> headers = createMockHeaders();
    MultivaluedMap<String, Object> requestHeaders = new MultivaluedHashMap<>();
    MultivaluedMap<String, String> requestStringHeaders = new MultivaluedHashMap<>();
    headers.forEach((header, value) -> {
        requestHeaders.add(header, value.get(0));
        requestStringHeaders.add(header, value.get(0));
    });
    final Message message = new MessageImpl();
    doInPhaseInterceptor(message, () -> {
        ClientRequestContext requestContext = getClientRequestContextMock(uri, method, requestHeaders);
        interceptor.filter(requestContext);
        String signatureHeader = (String) requestHeaders.get("Signature").get(0);
        requestStringHeaders.add("Signature", signatureHeader);
        String expectedHeader = "keyId=\"Test\",algorithm=\"rsa-sha256\"," + "headers=\"(request-target) host date\",signature=\"qdx+H7PHHDZgy4" + "y/Ahn9Tny9V3GP6YgBPyUXMmoxWtLbHpUnXS2mg2+SbrQDMCJypxBLSPQR2aAjn" + "7ndmw2iicw3HMbe8VfEdKFYRqzic+efkb3nndiv/x1xSHDJWeSWkx3ButlYSuBs" + "kLu6kd9Fswtemr3lgdDEmn04swr2Os0=\"";
        assertEquals(signatureHeader, expectedHeader);
        // Verify that the request signature can be verified by the filter
        MessageVerifier messageVerifier = new MessageVerifier(keyId -> publicKey);
        messageVerifier.setAddDefaultRequiredHeaders(false);
        messageVerifier.setSecurityProvider(new MockSecurityProvider());
        messageVerifier.setAlgorithmProvider(new MockAlgorithmProvider());
        VerifySignatureFilter verifySignatureFilter = new VerifySignatureFilter();
        verifySignatureFilter.setMessageVerifier(messageVerifier);
        ContainerRequestContext containerRequestContext = getContainerRequestContextMock(uri, method, requestStringHeaders);
        verifySignatureFilter.filter(containerRequestContext);
    });
}
Also used : ClientRequestContext(javax.ws.rs.client.ClientRequestContext) MockAlgorithmProvider(org.apache.cxf.rs.security.httpsignature.provider.MockAlgorithmProvider) Message(org.apache.cxf.message.Message) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) VerifySignatureFilter(org.apache.cxf.rs.security.httpsignature.filters.VerifySignatureFilter) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) CreateSignatureInterceptor(org.apache.cxf.rs.security.httpsignature.filters.CreateSignatureInterceptor) MockSecurityProvider(org.apache.cxf.rs.security.httpsignature.provider.MockSecurityProvider) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 4 with CreateSignatureInterceptor

use of org.apache.cxf.rs.security.httpsignature.filters.CreateSignatureInterceptor in project cxf by apache.

the class JAXRSHTTPSignatureTest method testHttpSignatureNoRequestTarget.

@Test
public void testHttpSignatureNoRequestTarget() throws Exception {
    URL busFile = JAXRSHTTPSignatureTest.class.getResource("client.xml");
    CreateSignatureInterceptor signatureFilter = new CreateSignatureInterceptor();
    KeyStore keyStore = KeyStore.getInstance("JKS");
    keyStore.load(ClassLoaderUtils.getResourceAsStream("keys/alice.jks", this.getClass()), "password".toCharArray());
    PrivateKey privateKey = (PrivateKey) keyStore.getKey("alice", "password".toCharArray());
    assertNotNull(privateKey);
    List<String> headerList = Arrays.asList("accept");
    MessageSigner messageSigner = new MessageSigner("rsa-sha512", keyId -> privateKey, "alice-key-id", headerList);
    signatureFilter.setMessageSigner(messageSigner);
    String address = "http://localhost:" + PORT + "/httpsigrsasha512/bookstore/books";
    WebClient client = WebClient.create(address, Collections.singletonList(signatureFilter), busFile.toString());
    client.type("application/xml").accept("application/xml");
    Response response = client.post(new Book("CXF", 126L));
    assertEquals(200, response.getStatus());
    Book returnedBook = response.readEntity(Book.class);
    assertEquals(126L, returnedBook.getId());
}
Also used : Response(javax.ws.rs.core.Response) MessageSigner(org.apache.cxf.rs.security.httpsignature.MessageSigner) PrivateKey(java.security.PrivateKey) CreateSignatureInterceptor(org.apache.cxf.rs.security.httpsignature.filters.CreateSignatureInterceptor) Book(org.apache.cxf.systest.jaxrs.security.Book) KeyStore(java.security.KeyStore) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL) Test(org.junit.Test)

Example 5 with CreateSignatureInterceptor

use of org.apache.cxf.rs.security.httpsignature.filters.CreateSignatureInterceptor in project cxf by apache.

the class JAXRSHTTPSignatureTest method testHttpSignatureRsaSha512ServiceProperties.

@Test
public void testHttpSignatureRsaSha512ServiceProperties() throws Exception {
    URL busFile = JAXRSHTTPSignatureTest.class.getResource("client.xml");
    CreateSignatureInterceptor signatureFilter = new CreateSignatureInterceptor();
    KeyStore keyStore = KeyStore.getInstance("JKS");
    keyStore.load(ClassLoaderUtils.getResourceAsStream("keys/alice.jks", this.getClass()), "password".toCharArray());
    PrivateKey privateKey = (PrivateKey) keyStore.getKey("alice", "password".toCharArray());
    assertNotNull(privateKey);
    MessageSigner messageSigner = new MessageSigner("rsa-sha512", keyId -> privateKey, "alice-key-id");
    signatureFilter.setMessageSigner(messageSigner);
    String address = "http://localhost:" + PORT + "/httpsigrsasha512props/bookstore/books";
    WebClient client = WebClient.create(address, Collections.singletonList(signatureFilter), busFile.toString());
    client.type("application/xml").accept("application/xml");
    Response response = client.post(new Book("CXF", 126L));
    assertEquals(200, response.getStatus());
    Book returnedBook = response.readEntity(Book.class);
    assertEquals(126L, returnedBook.getId());
}
Also used : Response(javax.ws.rs.core.Response) MessageSigner(org.apache.cxf.rs.security.httpsignature.MessageSigner) PrivateKey(java.security.PrivateKey) CreateSignatureInterceptor(org.apache.cxf.rs.security.httpsignature.filters.CreateSignatureInterceptor) Book(org.apache.cxf.systest.jaxrs.security.Book) KeyStore(java.security.KeyStore) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL) Test(org.junit.Test)

Aggregations

CreateSignatureInterceptor (org.apache.cxf.rs.security.httpsignature.filters.CreateSignatureInterceptor)30 Test (org.junit.Test)30 URL (java.net.URL)27 WebClient (org.apache.cxf.jaxrs.client.WebClient)27 Book (org.apache.cxf.systest.jaxrs.security.Book)27 Response (javax.ws.rs.core.Response)26 KeyStore (java.security.KeyStore)22 PrivateKey (java.security.PrivateKey)22 MessageSigner (org.apache.cxf.rs.security.httpsignature.MessageSigner)21 HashMap (java.util.HashMap)9 VerifySignatureClientFilter (org.apache.cxf.rs.security.httpsignature.filters.VerifySignatureClientFilter)8 ArrayList (java.util.ArrayList)5 MessageVerifier (org.apache.cxf.rs.security.httpsignature.MessageVerifier)5 ClientRequestContext (javax.ws.rs.client.ClientRequestContext)4 IOException (java.io.IOException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 ContainerRequestContext (javax.ws.rs.container.ContainerRequestContext)3 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)3 Message (org.apache.cxf.message.Message)3 MessageImpl (org.apache.cxf.message.MessageImpl)3