diff --git a/pom.xml b/pom.xml index 86cb1ac..e9a428f 100755 --- a/pom.xml +++ b/pom.xml @@ -13,6 +13,7 @@ oauth1-signer + 5.1.9.RELEASE 2.7.5 3.12.0 1.23.0 @@ -53,6 +54,13 @@ + + org.springframework + spring-web + ${spring-version} + provided + + org.apache.httpcomponents httpclient diff --git a/src/main/java/com/mastercard/developer/signers/SpringHttpRequestSigner.java b/src/main/java/com/mastercard/developer/signers/SpringHttpRequestSigner.java new file mode 100644 index 0000000..af7e71e --- /dev/null +++ b/src/main/java/com/mastercard/developer/signers/SpringHttpRequestSigner.java @@ -0,0 +1,40 @@ +package com.mastercard.developer.signers; + +import com.mastercard.developer.oauth.OAuth; + +import java.nio.charset.Charset; +import java.security.PrivateKey; + +import org.springframework.http.HttpRequest; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; + +/** + * Utility class for signing Spring RestTemplate requests. + */ +public class SpringHttpRequestSigner extends AbstractSigner { + + public SpringHttpRequestSigner(String consumerKey, PrivateKey signingKey) { + super(consumerKey, signingKey); + } + + public void sign(HttpRequest request, byte[] bytes) { + HttpHeaders headers = request.getHeaders(); + Charset charset = getCharset(headers); + String payload = (null==bytes ? null : new String(bytes, charset)); + String authHeader = OAuth.getAuthorizationHeader(request.getURI(), request.getMethod().toString(), payload, charset, consumerKey, signingKey); + headers.add(OAuth.AUTHORIZATION_HEADER_NAME, authHeader); + } + + private static Charset getCharset(HttpHeaders headers){ + Charset defaultCharset = Charset.defaultCharset(); + MediaType contentType = headers.getContentType(); + if(contentType != null){ + Charset charset = contentType.getCharset(); + if(charset != null){ + return charset; + } + } + return defaultCharset; + } +} diff --git a/src/test/java/com/mastercard/developer/signers/SpringHttpRequestSignerTest.java b/src/test/java/com/mastercard/developer/signers/SpringHttpRequestSignerTest.java new file mode 100644 index 0000000..47b5751 --- /dev/null +++ b/src/test/java/com/mastercard/developer/signers/SpringHttpRequestSignerTest.java @@ -0,0 +1,162 @@ +package com.mastercard.developer.signers; + +import com.mastercard.developer.test.TestUtils; + +import org.springframework.http.HttpRequest; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; + +import java.net.URI; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.security.PrivateKey; + +public class SpringHttpRequestSignerTest { + + private static final HttpMethod POST_METHOD = HttpMethod.POST; + private static final HttpMethod GET_METHOD = HttpMethod.GET; + private static final String DEFAULT_BODY = "{\"foo\":\"bar\"}"; + private static final String DEFAULT_CONSUMER_KEY = "Some key"; + + private PrivateKey signingKey; + private URI uri; + private HttpHeaders headers; + private HttpRequest request; + + @Before + public void initialize() throws Exception { + + signingKey = TestUtils.getTestSigningKey(); + uri = new URI("https://api.mastercard.com/service"); + headers = new HttpHeaders(); + request = new HttpRequest() { + @Override + public HttpMethod getMethod(){ + return POST_METHOD; + } + @Override + public String getMethodValue(){ + return getMethod().toString(); + } + @Override + public URI getURI(){ + return uri; + } + @Override + public HttpHeaders getHeaders(){ + return headers; + } + }; + } + + @Test + public void testSignShouldAddOAuth1HeaderToPostRequest() { + + // WHEN + SpringHttpRequestSigner instanceUnderTest = new SpringHttpRequestSigner(DEFAULT_CONSUMER_KEY, signingKey); + instanceUnderTest.sign(request, DEFAULT_BODY.getBytes()); + + // THEN + String authorizationHeaderValue = headers.getFirst(HttpHeaders.AUTHORIZATION); + Assert.assertNotNull(authorizationHeaderValue); + } + + @Test + public void testSignShouldAddOAuth1HeaderToPostRequestWithCharset() { + + // GIVEN + headers.setContentType(MediaType.APPLICATION_JSON_UTF8); + + // WHEN + SpringHttpRequestSigner instanceUnderTest = new SpringHttpRequestSigner(DEFAULT_CONSUMER_KEY, signingKey); + instanceUnderTest.sign(request, DEFAULT_BODY.getBytes()); + + // THEN + String authorizationHeaderValue = headers.getFirst(HttpHeaders.AUTHORIZATION); + Assert.assertNotNull(authorizationHeaderValue); + } + + @Test + public void testSignShouldAddOAuth1HeaderToPostRequestWithInvalidCharset() { + + // GIVEN + headers.setContentType(MediaType.APPLICATION_PDF); + + // WHEN + SpringHttpRequestSigner instanceUnderTest = new SpringHttpRequestSigner(DEFAULT_CONSUMER_KEY, signingKey); + instanceUnderTest.sign(request, DEFAULT_BODY.getBytes()); + + // THEN + String authorizationHeaderValue = headers.getFirst(HttpHeaders.AUTHORIZATION); + Assert.assertNotNull(authorizationHeaderValue); + } + + @Test + public void testSignShouldAddOAuth1HeaderToGetRequestNullBody() { + + // GIVEN + request = new HttpRequest() { + @Override + public HttpMethod getMethod(){ + return GET_METHOD; + } + @Override + public String getMethodValue(){ + return getMethod().toString(); + } + @Override + public URI getURI(){ + return uri; + } + @Override + public HttpHeaders getHeaders(){ + return headers; + } + }; + + // WHEN + SpringHttpRequestSigner instanceUnderTest = new SpringHttpRequestSigner(DEFAULT_CONSUMER_KEY, signingKey); + instanceUnderTest.sign(request, null); + + // THEN + String authorizationHeaderValue = headers.getFirst(HttpHeaders.AUTHORIZATION); + Assert.assertNotNull(authorizationHeaderValue); + } + + @Test + public void testSignShouldAddOAuth1HeaderToGetRequestEmptyBody() { + + // GIVEN + request = new HttpRequest() { + @Override + public HttpMethod getMethod(){ + return GET_METHOD; + } + @Override + public String getMethodValue(){ + return getMethod().toString(); + } + @Override + public URI getURI(){ + return uri; + } + @Override + public HttpHeaders getHeaders(){ + return headers; + } + }; + + // WHEN + SpringHttpRequestSigner instanceUnderTest = new SpringHttpRequestSigner(DEFAULT_CONSUMER_KEY, signingKey); + instanceUnderTest.sign(request, "".getBytes()); + + // THEN + String authorizationHeaderValue = headers.getFirst(HttpHeaders.AUTHORIZATION); + Assert.assertNotNull(authorizationHeaderValue); + } + +}