Search in sources :

Example 6 with MockAlgorithmProvider

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

the class MessageVerifierTest method nullBodyDelMethodShouldNotThrowInvalidDataToVerifySignatureException.

@Test
public void nullBodyDelMethodShouldNotThrowInvalidDataToVerifySignatureException() throws IOException {
    Map<String, List<String>> headers = createMockHeaders();
    headers.put("Test", Collections.singletonList("value"));
    headers.put(HTTPSignatureConstants.REQUEST_TARGET, Collections.singletonList("12345"));
    createAndAddSignatureDeleteMethod(headers);
    MessageVerifier headerVerifier = new MessageVerifier(keyId -> keyPair.getPublic());
    headerVerifier.setAddDefaultRequiredHeaders(true);
    headerVerifier.setSecurityProvider(new MockSecurityProvider());
    headerVerifier.setAlgorithmProvider(new MockAlgorithmProvider());
    headerVerifier.verifyMessage(headers, DELETE_METHOD, URI, new MessageImpl(), null);
}
Also used : MockAlgorithmProvider(org.apache.cxf.rs.security.httpsignature.provider.MockAlgorithmProvider) ArrayList(java.util.ArrayList) List(java.util.List) MockSecurityProvider(org.apache.cxf.rs.security.httpsignature.provider.MockSecurityProvider) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 7 with MockAlgorithmProvider

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

the class SpecExamplesTest method basicTest.

@Test
public void basicTest() throws IOException {
    Map<String, List<String>> headers = createMockHeaders();
    MessageSigner messageSigner = new MessageSigner(keyProvider, "Test", Arrays.asList("(request-target)", "host", "Date"));
    messageSigner.sign(headers, "/foo?param=value&pet=dog", "POST");
    String signatureHeader = headers.get("Signature").get(0);
    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);
    MessageVerifier messageVerifier = new MessageVerifier(keyId -> publicKey);
    messageVerifier.setAddDefaultRequiredHeaders(false);
    messageVerifier.setSecurityProvider(new MockSecurityProvider());
    messageVerifier.setAlgorithmProvider(new MockAlgorithmProvider());
    messageVerifier.verifyMessage(headers, "POST", "/foo?param=value&pet=dog", new MessageImpl(), MESSAGE_BODY.getBytes());
}
Also used : MockAlgorithmProvider(org.apache.cxf.rs.security.httpsignature.provider.MockAlgorithmProvider) MockSecurityProvider(org.apache.cxf.rs.security.httpsignature.provider.MockSecurityProvider) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 8 with MockAlgorithmProvider

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

the class SpecExamplesTest method defaultTest.

@Test
public void defaultTest() throws IOException {
    Map<String, List<String>> headers = createMockHeaders();
    MessageSigner messageSigner = new MessageSigner(keyProvider, "Test", Collections.singletonList("Date"));
    messageSigner.sign(headers, "/foo?param=value&pet=dog", "POST");
    String signatureHeader = headers.get("Signature").get(0);
    String expectedHeader = "keyId=\"Test\",algorithm=\"rsa-sha256\"," + "signature=\"SjWJWbWN7i0wzBvtPl8rbASWz5xQW6mcJmn+ibttBqtifLN7Sazz" + "6m79cNfwwb8DMJ5cou1s7uEGKKCs+FLEEaDV5lp7q25WqS+lavg7T8hc0GppauB" + "6hbgEKTwblDHYGEtbGmtdHgVCk9SuS13F0hZ8FD0k/5OxEPXe5WozsbM=\"";
    // CXF adds all headers by default, so above we explicitly only add Date just to simulate the expected header
    assertEquals(signatureHeader.replaceAll("headers=\"date\",", ""), expectedHeader);
    // Now check we validate the Date header as expected on an empty header list
    headers.put("Signature", Collections.singletonList(expectedHeader));
    MessageVerifier messageVerifier = new MessageVerifier(keyId -> publicKey);
    messageVerifier.setAddDefaultRequiredHeaders(false);
    messageVerifier.setSecurityProvider(new MockSecurityProvider());
    messageVerifier.setAlgorithmProvider(new MockAlgorithmProvider());
    messageVerifier.verifyMessage(headers, "POST", "/foo?param=value&pet=dog", new MessageImpl(), MESSAGE_BODY.getBytes());
}
Also used : MockAlgorithmProvider(org.apache.cxf.rs.security.httpsignature.provider.MockAlgorithmProvider) MockSecurityProvider(org.apache.cxf.rs.security.httpsignature.provider.MockSecurityProvider) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 9 with MockAlgorithmProvider

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

the class SpecExamplesTest method allHeadersTest.

@Test
public void allHeadersTest() throws IOException {
    Map<String, List<String>> headers = createMockHeaders();
    MessageSigner messageSigner = new MessageSigner(keyProvider, "Test", Arrays.asList("(request-target)", "host", "date", "content-type", "digest", "content-length"));
    messageSigner.sign(headers, "/foo?param=value&pet=dog", "POST");
    String signatureHeader = headers.get("Signature").get(0);
    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);
    MessageVerifier messageVerifier = new MessageVerifier(keyId -> publicKey);
    messageVerifier.setSecurityProvider(new MockSecurityProvider());
    messageVerifier.setAlgorithmProvider(new MockAlgorithmProvider());
    messageVerifier.verifyMessage(headers, "POST", "/foo?param=value&pet=dog", new MessageImpl(), MESSAGE_BODY.getBytes());
}
Also used : MockAlgorithmProvider(org.apache.cxf.rs.security.httpsignature.provider.MockAlgorithmProvider) MockSecurityProvider(org.apache.cxf.rs.security.httpsignature.provider.MockSecurityProvider) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 10 with MockAlgorithmProvider

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

the class MessageVerifierTest method defaultRequiredHeaderPresentTestNotRequired.

@Test
public void defaultRequiredHeaderPresentTestNotRequired() throws IOException {
    Map<String, List<String>> headers = createMockHeaders();
    headers.put("Test", Collections.singletonList("value"));
    headers.put(HTTPSignatureConstants.REQUEST_TARGET, Collections.singletonList("12345"));
    createAndAddSignature(headers);
    MessageVerifier headerVerifier = new MessageVerifier(keyId -> keyPair.getPublic());
    headerVerifier.setSecurityProvider(new MockSecurityProvider());
    headerVerifier.setAlgorithmProvider(new MockAlgorithmProvider());
    headerVerifier.verifyMessage(headers, METHOD, URI, new MessageImpl(), MESSAGE_BODY.getBytes());
}
Also used : MockAlgorithmProvider(org.apache.cxf.rs.security.httpsignature.provider.MockAlgorithmProvider) ArrayList(java.util.ArrayList) List(java.util.List) MockSecurityProvider(org.apache.cxf.rs.security.httpsignature.provider.MockSecurityProvider) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Aggregations

MessageImpl (org.apache.cxf.message.MessageImpl)15 MockAlgorithmProvider (org.apache.cxf.rs.security.httpsignature.provider.MockAlgorithmProvider)15 MockSecurityProvider (org.apache.cxf.rs.security.httpsignature.provider.MockSecurityProvider)15 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)9 List (java.util.List)9 ClientRequestContext (javax.ws.rs.client.ClientRequestContext)3 ContainerRequestContext (javax.ws.rs.container.ContainerRequestContext)3 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)3 Message (org.apache.cxf.message.Message)3 CreateSignatureInterceptor (org.apache.cxf.rs.security.httpsignature.filters.CreateSignatureInterceptor)3 VerifySignatureFilter (org.apache.cxf.rs.security.httpsignature.filters.VerifySignatureFilter)3 IOException (java.io.IOException)1 KeyPair (java.security.KeyPair)1 KeyPairGenerator (java.security.KeyPairGenerator)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ZoneOffset (java.time.ZoneOffset)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1