Skip to content

Commit

Permalink
Updated algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Rusu committed Aug 29, 2017
1 parent 1da452b commit 89c2780
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 51 deletions.
1 change: 1 addition & 0 deletions samples/java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ repositories {

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile 'org.mockito:mockito-core:2.8.47'
}
55 changes: 19 additions & 36 deletions samples/java/src/main/java/com/modulr/api/ModulrApiAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,56 +16,47 @@ public class ModulrApiAuth {
private static final String DATE_PATTERN = "EEE, dd MMM yyyy HH:mm:ss z";
private final String secret;
private final String token;
private String nonce;
private Date date;
private Boolean retry = false;

private String lastUsedNonce;

public ModulrApiAuth(String token, String secret, String nonce) {
public ModulrApiAuth(String token, String secret) {
this.token = token.trim();
this.secret = secret.trim();
this.nonce = nonce.trim();
this.date = new Date();
}

public Map<String, String> getApiAuthHeaders() {
public Map<String, String> generateApiAuthHeaders(String nonce) {
return buildHeaders(nonce, false);
}

public Map<String, String> generateRetryApiAuthHeaders() {
return buildHeaders(this.lastUsedNonce, true);
}

private Map<String, String> buildHeaders(String nonce, Boolean retry) {
final Map<String, String> headerParams = new HashMap<>();
try {
String hmac = generateHmac();
String hmac = generateHmac(nonce);

headerParams.put("Authorization", formatAuthHeader(this.token, hmac));
headerParams.put("Date", getFormattedDate(this.date));
headerParams.put("x-mod-nonce", this.nonce);
headerParams.put("x-mod-retry", String.valueOf(this.retry));
headerParams.put("Date", getFormattedDate(this.getDate()));
headerParams.put("x-mod-nonce", nonce);
headerParams.put("x-mod-retry", String.valueOf(retry));

this.lastUsedNonce = nonce;
} catch (SignatureException e) {
e.printStackTrace();
}

return headerParams;
}

public String generateHmac() throws SignatureException {
final String hmac;
if (this.retry) {
this.nonce = this.lastUsedNonce;
} else {
this.lastUsedNonce = this.nonce;
}

private String generateHmac(String nonce) throws SignatureException {
validateFields();
String data = String.format("date: %s nx-mod-nonce: %s", getFormattedDate(this.date), this.nonce);
this.date = new Date();
String data = String.format("date: %s nx-mod-nonce: %s", getFormattedDate(this.getDate()), nonce);
return calculateHmac(data);
}

public String getNonce() {
return nonce;
}

public void setNonce(String nonce) {
this.nonce = nonce;
}

public Date getDate() {
return date;
}
Expand All @@ -82,14 +73,6 @@ public void setDate(Date date) {
this.date = date;
}

public Boolean getRetry() {
return retry;
}

public void setRetry(Boolean retry) {
this.retry = retry;
}

private String formatAuthHeader(String token, String signature) {
return String.format("Signature keyId=\"%s\",algorithm=\"%s\",headers=\"date x-mod-nonce\",signature=\"%s\"", token, "hmac-sha1", signature);
}
Expand Down
4 changes: 2 additions & 2 deletions samples/java/src/main/java/com/modulr/hmac/Hmac.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

public class Hmac {
public static void main(String... args) {
ModulrApiAuth modulrAuth = new ModulrApiAuth("KNOWN-TOKEN", "SECRET-TOKEN", "NONCE");
Map<String, String> headers = modulrAuth.getApiAuthHeaders();
ModulrApiAuth modulrAuth = new ModulrApiAuth("KNOWN-TOKEN", "SECRET-TOKEN");
Map<String, String> headers = modulrAuth.generateApiAuthHeaders("NONCE");

headers.forEach((key, value) -> System.out.println(key + ": " + value));
}
Expand Down
29 changes: 16 additions & 13 deletions samples/java/src/test/java/com/modulr/hmac/HmacTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,47 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.junit.MockitoJUnitRunner;

import java.security.SignatureException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.Map;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

@RunWith(JUnit4.class)
@RunWith(MockitoJUnitRunner.class)
public class HmacTest {
private ModulrApiAuth modulrAuth;

@Before
public void setUp() {
modulrAuth = new ModulrApiAuth("KNOWN-TOKEN", "SECRET-TOKEN", "NONCE");
}

@Test
public void testHmacGenerator() throws SignatureException {
String dateStr = "2017-08-04T10:10:20";
LocalDateTime dateTime = LocalDateTime.parse(dateStr);
Date date = Date.from(dateTime.toInstant(ZoneOffset.UTC));

modulrAuth.setDate(date);
assertEquals("G9zfk3yPn861TKddM6wIxu4u0YU%3D", modulrAuth.generateHmac());
modulrAuth = spy(new ModulrApiAuth("KNOWN-TOKEN", "SECRET-TOKEN"));
when(modulrAuth.getDate()).thenReturn(date);
}

Map<String, String> headers = modulrAuth.getApiAuthHeaders();
@Test
public void testHmacGenerator() throws SignatureException {
Map<String, String> headers = modulrAuth.generateApiAuthHeaders("NONCE");
assertEquals(headers.size(), 4);
assertEquals("Signature keyId=\"KNOWN-TOKEN\",algorithm=\"hmac-sha1\",headers=\"date x-mod-nonce\",signature=\"G9zfk3yPn861TKddM6wIxu4u0YU%3D\"", headers.get("Authorization"));
assertEquals("NONCE", headers.get("x-mod-nonce"));
assertEquals("Fri, 04 Aug 2017 10:10:20 GMT", headers.get("Date"));
assertEquals("false", headers.get("x-mod-retry"));

modulrAuth.setRetry(true);
Map<String, String> headersWithRetryOn = modulrAuth.getApiAuthHeaders();

when(modulrAuth.getDate()).thenReturn(new Date());
Map<String, String> headersWithRetryOn = modulrAuth.generateRetryApiAuthHeaders();
assertEquals("true", headersWithRetryOn.get("x-mod-retry"));
assertEquals(headersWithRetryOn.get("x-mod-nonce"), headers.get("x-mod-nonce"));
assertNotEquals(headersWithRetryOn.get("Date"), headers.get("Date"));
}
}

0 comments on commit 89c2780

Please sign in to comment.