Skip to content

Commit

Permalink
When retry flag is set, the hmac is generated based on the previous n…
Browse files Browse the repository at this point in the history
…once, but the hmac has to be regenerated since the date changes
  • Loading branch information
Gabriel Rusu committed Aug 24, 2017
1 parent c5a8ca8 commit 1da452b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 97 deletions.
81 changes: 0 additions & 81 deletions ModulrAuthHeaderExample.java

This file was deleted.

17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
# Modulr Finance HMAC

### [ModulrAuthHeaderExample.java] (https://github.com/Modulr-finance/modulr-hmac/blob/master/ModulrAuthHeaderExample.java)
This class provides a Java example of generating HMAC Signature headers for use with Modulr API calls.
### Samples
Samples directory contain sample code for the following languages:
- Java

### [io.swagger.client.auth.ApiKeyAuth.java] (https://github.com/Modulr-finance/modulr-hmac/blob/master/io/swagger/client/auth/ApiKeyAuth.java)
This class provides a drop in replacement for the same file generated from Swagger.io Java client generation. This assumes you set ApiKey to your Modulr issued API Key and ApiKeyPrefix to your Modulr issued API secret in io.swagger.client.Configuration. For example;

```java
public static void main(String[] args) throws ApiException {
Configuration.getDefaultApiClient().setApiKey("<Your API Key>");
Configuration.getDefaultApiClient().setApiKeyPrefix("<You API Secret>");
TransactionsApi api = new TransactionsApi();
System.out.println(api.getTransactionsUsingGET("A02017NU",0,25));
class ModulrApiAuth {
public static void main(String[] args) throws ApiException {
Configuration.getDefaultApiClient().setApiKey("<Your API Key>");
Configuration.getDefaultApiClient().setApiKeyPrefix("<You API Secret>");
TransactionsApi api = new TransactionsApi();
System.out.println(api.getTransactionsUsingGET("A02017NU",0,25));
}
}
```
16 changes: 7 additions & 9 deletions samples/java/src/main/java/com/modulr/api/ModulrApiAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ModulrApiAuth {
private Date date;
private Boolean retry = false;

private String lastGeneratedHmac;
private String lastUsedNonce;

public ModulrApiAuth(String token, String secret, String nonce) {
this.token = token.trim();
Expand Down Expand Up @@ -48,15 +48,14 @@ public Map<String, String> getApiAuthHeaders() {
public String generateHmac() throws SignatureException {
final String hmac;
if (this.retry) {
hmac = this.lastGeneratedHmac;
this.nonce = this.lastUsedNonce;
} else {
validateFields();
String data = String.format("date: %s nx-mod-nonce: %s", getFormattedDate(this.date), this.nonce);
hmac = calculateHmac(data);
this.lastGeneratedHmac = hmac;
this.lastUsedNonce = this.nonce;
}

return hmac;
validateFields();
String data = String.format("date: %s nx-mod-nonce: %s", getFormattedDate(this.date), this.nonce);
return calculateHmac(data);
}

public String getNonce() {
Expand Down Expand Up @@ -88,8 +87,7 @@ public Boolean getRetry() {
}

public void setRetry(Boolean retry) {
if (this.lastGeneratedHmac != null)
this.retry = retry;
this.retry = retry;
}

private String formatAuthHeader(String token, String signature) {
Expand Down

0 comments on commit 1da452b

Please sign in to comment.