Search in sources :

Example 11 with RequestHeader

use of com.ingenico.connect.gateway.sdk.java.RequestHeader in project connect-sdk-java by Ingenico-ePayments.

the class WebhooksHelperTest method testUnmarshalDuplicateHeaders.

@Test(expected = SignatureValidationException.class)
public void testUnmarshalDuplicateHeaders() throws IOException {
    WebhooksHelper helper = createHelper();
    InMemorySecretKeyStore.INSTANCE.storeSecretKey(KEY_ID, SECRET_KEY);
    String body = new String(readResource("valid-body"), CHARSET);
    List<RequestHeader> requestHeaders = Arrays.asList(new RequestHeader(SIGNATURE_HEADER, SIGNATURE), new RequestHeader(KEY_ID_HEADER, KEY_ID), new RequestHeader(SIGNATURE_HEADER, SIGNATURE + "1"));
    helper.unmarshal(body, requestHeaders);
}
Also used : RequestHeader(com.ingenico.connect.gateway.sdk.java.RequestHeader) Test(org.junit.Test)

Example 12 with RequestHeader

use of com.ingenico.connect.gateway.sdk.java.RequestHeader in project connect-sdk-java by Ingenico-ePayments.

the class WebhooksHelperTest method testUnmarshalBytesInvalidBody.

@Test(expected = SignatureValidationException.class)
public void testUnmarshalBytesInvalidBody() throws IOException {
    WebhooksHelper helper = createHelper();
    InMemorySecretKeyStore.INSTANCE.storeSecretKey(KEY_ID, SECRET_KEY);
    ByteArrayInputStream bodyStream = new ByteArrayInputStream(readResource("invalid-body"));
    List<RequestHeader> requestHeaders = Arrays.asList(new RequestHeader(SIGNATURE_HEADER, SIGNATURE), new RequestHeader(KEY_ID_HEADER, KEY_ID));
    helper.unmarshal(bodyStream, requestHeaders);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) RequestHeader(com.ingenico.connect.gateway.sdk.java.RequestHeader) Test(org.junit.Test)

Example 13 with RequestHeader

use of com.ingenico.connect.gateway.sdk.java.RequestHeader in project connect-sdk-java by Ingenico-ePayments.

the class WebhooksHelperTest method testUnmarshalApiVersionMismatch.

@Test(expected = ApiVersionMismatchException.class)
public void testUnmarshalApiVersionMismatch() throws IOException {
    Marshaller marshaller = Mockito.mock(Marshaller.class);
    Mockito.when(marshaller.unmarshal(Matchers.anyString(), Matchers.<Class<?>>any())).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            String responseJson = invocation.getArgumentAt(0, String.class);
            Class<?> type = invocation.getArgumentAt(1, Class.class);
            WebhooksEvent event = (WebhooksEvent) DefaultMarshaller.INSTANCE.unmarshal(responseJson, type);
            event.setApiVersion("v0");
            return event;
        }
    });
    WebhooksHelper helper = createHelper(marshaller);
    InMemorySecretKeyStore.INSTANCE.storeSecretKey(KEY_ID, SECRET_KEY);
    String body = new String(readResource("valid-body"), CHARSET);
    List<RequestHeader> requestHeaders = Arrays.asList(new RequestHeader(SIGNATURE_HEADER, SIGNATURE), new RequestHeader(KEY_ID_HEADER, KEY_ID));
    helper.unmarshal(body, requestHeaders);
}
Also used : Marshaller(com.ingenico.connect.gateway.sdk.java.Marshaller) DefaultMarshaller(com.ingenico.connect.gateway.sdk.java.defaultimpl.DefaultMarshaller) WebhooksEvent(com.ingenico.connect.gateway.sdk.java.domain.webhooks.WebhooksEvent) InvocationOnMock(org.mockito.invocation.InvocationOnMock) RequestHeader(com.ingenico.connect.gateway.sdk.java.RequestHeader) Test(org.junit.Test)

Example 14 with RequestHeader

use of com.ingenico.connect.gateway.sdk.java.RequestHeader in project connect-sdk-java by Ingenico-ePayments.

the class DefaultAuthenticator method toDataToSign.

String toDataToSign(String httpMethod, URI resourceUri, List<RequestHeader> httpHeaders) {
    String contentType = null;
    String date = null;
    String canonicalizedResource = toCanonicalizedResource(resourceUri);
    List<RequestHeader> xgcsHttpHeaders = new ArrayList<RequestHeader>();
    if (httpHeaders != null) {
        for (RequestHeader httpHeader : httpHeaders) {
            if ("Content-Type".equalsIgnoreCase(httpHeader.getName())) {
                contentType = httpHeader.getValue();
            } else if ("Date".equalsIgnoreCase(httpHeader.getName())) {
                date = httpHeader.getValue();
            } else {
                String name = toCanonicalizeHeaderName(httpHeader.getName());
                if (name.startsWith("x-gcs")) {
                    String value = toCanonicalizeHeaderValue(httpHeader.getValue());
                    RequestHeader xgcsHttpHeader = new RequestHeader(name, value);
                    xgcsHttpHeaders.add(xgcsHttpHeader);
                }
            }
        }
    }
    Collections.sort(xgcsHttpHeaders, REQUEST_HEADER_COMPARATOR);
    StringBuilder sb = new StringBuilder(100);
    sb.append(httpMethod.toUpperCase()).append('\n');
    if (contentType != null) {
        sb.append(contentType).append('\n');
    } else {
        sb.append('\n');
    }
    sb.append(date).append('\n');
    for (RequestHeader xgcsHttpHeader : xgcsHttpHeaders) {
        sb.append(xgcsHttpHeader.getName()).append(':').append(xgcsHttpHeader.getValue()).append('\n');
    }
    sb.append(canonicalizedResource).append('\n');
    return sb.toString();
}
Also used : ArrayList(java.util.ArrayList) RequestHeader(com.ingenico.connect.gateway.sdk.java.RequestHeader)

Aggregations

RequestHeader (com.ingenico.connect.gateway.sdk.java.RequestHeader)14 Test (org.junit.Test)13 ByteArrayInputStream (java.io.ByteArrayInputStream)4 WebhooksEvent (com.ingenico.connect.gateway.sdk.java.domain.webhooks.WebhooksEvent)3 ArrayList (java.util.ArrayList)2 Client (com.ingenico.connect.gateway.sdk.java.Client)1 CommunicatorConfiguration (com.ingenico.connect.gateway.sdk.java.CommunicatorConfiguration)1 Marshaller (com.ingenico.connect.gateway.sdk.java.Marshaller)1 MetaDataProvider (com.ingenico.connect.gateway.sdk.java.MetaDataProvider)1 MetaDataProviderBuilder (com.ingenico.connect.gateway.sdk.java.MetaDataProviderBuilder)1 Session (com.ingenico.connect.gateway.sdk.java.Session)1 DefaultMarshaller (com.ingenico.connect.gateway.sdk.java.defaultimpl.DefaultMarshaller)1 Directory (com.ingenico.connect.gateway.sdk.java.domain.product.Directory)1 DirectoryParams (com.ingenico.connect.gateway.sdk.java.merchant.products.DirectoryParams)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1