Search in sources :

Example 1 with MessageVerifier

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

the class JAXRSHTTPSignatureTest method testHttpSignatureResponseServiceProperties.

@Test
public void testHttpSignatureResponseServiceProperties() 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(keyId -> privateKey, "alice-key-id");
    signatureFilter.setMessageSigner(messageSigner);
    VerifySignatureClientFilter signatureResponseFilter = new VerifySignatureClientFilter();
    MessageVerifier messageVerifier = new MessageVerifier(new CustomPublicKeyProvider());
    signatureResponseFilter.setMessageVerifier(messageVerifier);
    List<Object> providers = new ArrayList<>();
    providers.add(signatureFilter);
    providers.add(signatureResponseFilter);
    String address = "http://localhost:" + PORT + "/httpsigresponseprops/bookstore/books";
    WebClient client = WebClient.create(address, providers, 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 : MessageSigner(org.apache.cxf.rs.security.httpsignature.MessageSigner) PrivateKey(java.security.PrivateKey) ArrayList(java.util.ArrayList) MessageVerifier(org.apache.cxf.rs.security.httpsignature.MessageVerifier) KeyStore(java.security.KeyStore) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL) Response(javax.ws.rs.core.Response) CreateSignatureInterceptor(org.apache.cxf.rs.security.httpsignature.filters.CreateSignatureInterceptor) Book(org.apache.cxf.systest.jaxrs.security.Book) VerifySignatureClientFilter(org.apache.cxf.rs.security.httpsignature.filters.VerifySignatureClientFilter) Test(org.junit.Test)

Example 2 with MessageVerifier

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

the class JAXRSHTTPSignatureTest method testHttpSignatureEmptyResponse.

@Test
public void testHttpSignatureEmptyResponse() 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(keyId -> privateKey, "alice-key-id");
    signatureFilter.setMessageSigner(messageSigner);
    VerifySignatureClientFilter signatureResponseFilter = new VerifySignatureClientFilter();
    MessageVerifier messageVerifier = new MessageVerifier(new CustomPublicKeyProvider());
    signatureResponseFilter.setMessageVerifier(messageVerifier);
    String address = "http://localhost:" + PORT + "/httpsigresponse/bookstore/booksnoresp";
    WebClient client = WebClient.create(address, Arrays.asList(signatureFilter, signatureResponseFilter), busFile.toString());
    client.type("application/xml").accept("application/xml");
    Response response = client.post(new Book("CXF", 126L));
    assertEquals(204, response.getStatus());
}
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) MessageVerifier(org.apache.cxf.rs.security.httpsignature.MessageVerifier) VerifySignatureClientFilter(org.apache.cxf.rs.security.httpsignature.filters.VerifySignatureClientFilter) KeyStore(java.security.KeyStore) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL) Test(org.junit.Test)

Example 3 with MessageVerifier

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

the class JAXRSHTTPSignatureTest method testHttpSignatureDigestResponse.

@Test
public void testHttpSignatureDigestResponse() 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(keyId -> privateKey, "alice-key-id");
    signatureFilter.setMessageSigner(messageSigner);
    VerifySignatureClientFilter signatureResponseFilter = new VerifySignatureClientFilter();
    MessageVerifier messageVerifier = new MessageVerifier(new CustomPublicKeyProvider());
    signatureResponseFilter.setMessageVerifier(messageVerifier);
    String address = "http://localhost:" + PORT + "/httpsigrequired/bookstore/books";
    WebClient client = WebClient.create(address, Arrays.asList(signatureFilter, signatureResponseFilter), 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());
    String address2 = "http://localhost:" + PORT + "/httpsig/bookstore/books";
    client = WebClient.create(address2, Arrays.asList(signatureFilter, signatureResponseFilter), busFile.toString());
    client.type("application/xml").accept("application/xml");
    try {
        client.post(new Book("CXF", 126L));
        fail("Failure expected on no digest");
    } catch (Exception ex) {
    // expected
    }
}
Also used : MessageSigner(org.apache.cxf.rs.security.httpsignature.MessageSigner) PrivateKey(java.security.PrivateKey) MessageVerifier(org.apache.cxf.rs.security.httpsignature.MessageVerifier) KeyStore(java.security.KeyStore) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Response(javax.ws.rs.core.Response) CreateSignatureInterceptor(org.apache.cxf.rs.security.httpsignature.filters.CreateSignatureInterceptor) Book(org.apache.cxf.systest.jaxrs.security.Book) VerifySignatureClientFilter(org.apache.cxf.rs.security.httpsignature.filters.VerifySignatureClientFilter) Test(org.junit.Test)

Example 4 with MessageVerifier

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

the class AbstractSignatureInFilter method createMessageVerifier.

protected MessageVerifier createMessageVerifier() {
    Properties props = KeyManagementUtils.loadSignatureInProperties();
    if (props == null) {
        throw new SignatureException("Signature properties are not configured correctly");
    }
    Message m = PhaseInterceptorChain.getCurrentMessage();
    PublicKey publicKey = KeyManagementUtils.loadPublicKey(m, props);
    String signatureAlgorithm = (String) m.getContextualProperty(HTTPSignatureConstants.RSSEC_SIGNATURE_ALGORITHM);
    if (signatureAlgorithm == null) {
        signatureAlgorithm = DefaultSignatureConstants.SIGNING_ALGORITHM;
    }
    final String finalSignatureAlgorithm = signatureAlgorithm;
    final Provider provider = Security.getProvider(DefaultSignatureConstants.SECURITY_PROVIDER);
    return new MessageVerifier(keyId -> publicKey, keyId -> provider, keyId -> finalSignatureAlgorithm);
}
Also used : Message(org.apache.cxf.message.Message) PublicKey(java.security.PublicKey) MessageVerifier(org.apache.cxf.rs.security.httpsignature.MessageVerifier) SignatureException(org.apache.cxf.rs.security.httpsignature.exception.SignatureException) InvalidDataToVerifySignatureException(org.apache.cxf.rs.security.httpsignature.exception.InvalidDataToVerifySignatureException) InvalidSignatureException(org.apache.cxf.rs.security.httpsignature.exception.InvalidSignatureException) Properties(java.util.Properties) Provider(java.security.Provider)

Example 5 with MessageVerifier

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

the class JAXRSHTTPSignatureTest method testHttpSignatureResponse.

@Test
public void testHttpSignatureResponse() 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(keyId -> privateKey, "alice-key-id");
    signatureFilter.setMessageSigner(messageSigner);
    VerifySignatureClientFilter signatureResponseFilter = new VerifySignatureClientFilter();
    MessageVerifier messageVerifier = new MessageVerifier(new CustomPublicKeyProvider());
    signatureResponseFilter.setMessageVerifier(messageVerifier);
    List<Object> providers = new ArrayList<>();
    providers.add(signatureFilter);
    providers.add(signatureResponseFilter);
    String address = "http://localhost:" + PORT + "/httpsigresponse/bookstore/books";
    WebClient client = WebClient.create(address, providers, 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 : MessageSigner(org.apache.cxf.rs.security.httpsignature.MessageSigner) PrivateKey(java.security.PrivateKey) ArrayList(java.util.ArrayList) MessageVerifier(org.apache.cxf.rs.security.httpsignature.MessageVerifier) KeyStore(java.security.KeyStore) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL) Response(javax.ws.rs.core.Response) CreateSignatureInterceptor(org.apache.cxf.rs.security.httpsignature.filters.CreateSignatureInterceptor) Book(org.apache.cxf.systest.jaxrs.security.Book) VerifySignatureClientFilter(org.apache.cxf.rs.security.httpsignature.filters.VerifySignatureClientFilter) Test(org.junit.Test)

Aggregations

MessageVerifier (org.apache.cxf.rs.security.httpsignature.MessageVerifier)5 URL (java.net.URL)4 KeyStore (java.security.KeyStore)4 PrivateKey (java.security.PrivateKey)4 Response (javax.ws.rs.core.Response)4 WebClient (org.apache.cxf.jaxrs.client.WebClient)4 MessageSigner (org.apache.cxf.rs.security.httpsignature.MessageSigner)4 CreateSignatureInterceptor (org.apache.cxf.rs.security.httpsignature.filters.CreateSignatureInterceptor)4 VerifySignatureClientFilter (org.apache.cxf.rs.security.httpsignature.filters.VerifySignatureClientFilter)4 Book (org.apache.cxf.systest.jaxrs.security.Book)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)2 IOException (java.io.IOException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Provider (java.security.Provider)1 PublicKey (java.security.PublicKey)1 Properties (java.util.Properties)1 Message (org.apache.cxf.message.Message)1 InvalidDataToVerifySignatureException (org.apache.cxf.rs.security.httpsignature.exception.InvalidDataToVerifySignatureException)1 InvalidSignatureException (org.apache.cxf.rs.security.httpsignature.exception.InvalidSignatureException)1