Skip to content

Commit

Permalink
Update signature authentication (#89)
Browse files Browse the repository at this point in the history
* Add nonce to payload

* Upgrade to Hashing to SHA-384

* Resolve * to single class imports

* Satisfy linter on testcase docstring => added those checks to local environment also.
  • Loading branch information
cdr-chakotay authored Jan 10, 2023
1 parent a516b00 commit 0b5275f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
33 changes: 28 additions & 5 deletions src/main/java/com/transloadit/sdk/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import org.joda.time.format.DateTimeFormatter;
import org.json.JSONObject;

import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.File;
import java.io.IOException;
Expand All @@ -26,6 +28,7 @@
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
Expand Down Expand Up @@ -303,6 +306,7 @@ private RequestBody getBody(Map<String, String> data, @Nullable Map<String, File
private Map<String, String> toPayload(Map<String, Object> data) throws LocalOperationException {
Map<String, Object> dataClone = new HashMap<String, Object>(data);
dataClone.put("auth", getAuthData());
dataClone.put("nonce", getNonce("AES", 256));

Map<String, String> payload = new HashMap<String, String>();
payload.put("params", jsonifyData(dataClone));
Expand Down Expand Up @@ -350,14 +354,14 @@ private Map<String, String> getAuthData() {
*/
private String getSignature(String message) throws LocalOperationException {
byte[] kSecret = transloadit.secret.getBytes(Charset.forName("UTF-8"));
byte[] rawHmac = hmacSHA1(kSecret, message);
byte[] rawHmac = hmacSHA384(kSecret, message);
byte[] hexBytes = new Hex().encode(rawHmac);

return new String(hexBytes, Charset.forName("UTF-8"));
String signature = "sha384:" + new String(hexBytes, Charset.forName("UTF-8"));
return signature;
}

private byte[] hmacSHA1(byte[] key, String data) throws LocalOperationException {
final String algorithm = "HmacSHA1";
private byte[] hmacSHA384(byte[] key, String data) throws LocalOperationException {
final String algorithm = "HmacSHA384";
Mac mac;

try {
Expand All @@ -371,6 +375,25 @@ private byte[] hmacSHA1(byte[] key, String data) throws LocalOperationException
return mac.doFinal(data.getBytes(Charset.forName("UTF-8")));
}

/**
* Generates a strong cryptographic nonce in order to make the request's signature unique.
* @param cipher Algorithm to derive key with
* @param lengthInBits Length of the generated key in bits
* @return A Key formatted as String
*/
protected String getNonce(String cipher, int lengthInBits) {
KeyGenerator keyGenerator = null;
try {
keyGenerator = KeyGenerator.getInstance(cipher);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
keyGenerator.init(lengthInBits);
SecretKey secKey = keyGenerator.generateKey();
String encodedKey = Base64.getEncoder().encodeToString(secKey.getEncoded());
return encodedKey;
}

/**
* Helper method, which performs a retryRateLimit action if a POST request has hit the servers rate limit.
* All parameters of the failed POST request should be provided to this method.
Expand Down
16 changes: 12 additions & 4 deletions src/test/java/com/transloadit/sdk/RequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,18 @@ public void retryAfterSpecificErrors() throws LocalOperationException, RequestEx
testRequest.delete("/foo", new HashMap<String, Object>());
}

/**
* Test secure nonce generation with.
*/
@Test
public void getNonce() {
String cipher = "Blowfish";
int keyLength = 256;

String nonce = request.getNonce(cipher, keyLength);
assertEquals(44, nonce.length());
}

/**
* Tests if {@link Request#delayBeforeRetry()} works.
* @throws LocalOperationException
Expand All @@ -194,8 +206,4 @@ public void delayBeforeRetry() throws LocalOperationException {
assertTrue(delta >= timeout);

}



}

0 comments on commit 0b5275f

Please sign in to comment.