-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from fileme/master
Added a signer for Spring HTTP requests
- Loading branch information
Showing
3 changed files
with
210 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/java/com/mastercard/developer/signers/SpringHttpRequestSigner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
162 changes: 162 additions & 0 deletions
162
src/test/java/com/mastercard/developer/signers/SpringHttpRequestSignerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |