Skip to content

Commit

Permalink
Check if given keys are null or empty
Browse files Browse the repository at this point in the history
Check if given keys are null or empty, if it is the case a
BitsoApiException will be thrown.

Signed-off-by: Victor <victor.cruz.isc@gmail.com>
  • Loading branch information
nvcruzhe committed Oct 6, 2017
1 parent 9a058d3 commit e3bea1c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 13 deletions.
34 changes: 22 additions & 12 deletions src/main/java/com/bitso/Bitso.java
Original file line number Diff line number Diff line change
Expand Up @@ -576,27 +576,36 @@ private String quoteEliminator(String input) {
return input.substring(1, length - 1);
}

private String buildBitsoAuthHeader(String requestPath, String httpMethod, String apiKey, String secret) {
long nonce = System.currentTimeMillis() + System.currentTimeMillis();
private String buildBitsoAuthHeader(String requestPath, String httpMethod, String apiKey, String secret)
throws BitsoAPIException {
if (apiKey == null || secret == null) {
throw new BitsoAPIException("Bitso API key or secret is null");
}

byte[] secretBytes = secret.getBytes();
byte[] arrayOfByte = null;
String signature = null;
BigInteger bigInteger = null;
Mac mac = null;
if (secretBytes.length == 0) {
throw new BitsoAPIException("Bitso API key is empty");
}

long nonce = System.currentTimeMillis() + System.currentTimeMillis();
String message = nonce + httpMethod + requestPath;
SecretKeySpec secretKeySpec = new SecretKeySpec(secretBytes, "HmacSHA256");

try {
mac = Mac.getInstance("HmacSHA256");
SecretKeySpec secretKeySpec = new SecretKeySpec(secretBytes, "HmacSHA256");
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(secretKeySpec);
arrayOfByte = mac.doFinal(message.getBytes());
bigInteger = new BigInteger(1, arrayOfByte);
signature = String.format("%0" + (arrayOfByte.length << 1) + "x", new Object[] { bigInteger });
byte[] arrayOfByte = mac.doFinal(message.getBytes());
BigInteger bigInteger = new BigInteger(1, arrayOfByte);
String signature = String.format("%0" + (arrayOfByte.length << 1) + "x",
new Object[] { bigInteger });
return String.format("Bitso %s:%s:%s", apiKey, nonce, signature);
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
e.printStackTrace();
throw new BitsoAPIException(e);
} catch (IllegalArgumentException e) {
e.printStackTrace();
throw new BitsoAPIException(e);
}
return null;
}

private static Entry<String, String> buildBitsoAuthHeader(String secretKey, String publicKey, long nonce,
Expand All @@ -609,6 +618,7 @@ private static Entry<String, String> buildBitsoAuthHeader(String secretKey, Stri
try {
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(localMac);

// Compute the hmac on input data bytes
byte[] arrayOfByte = mac.doFinal(message.getBytes());
BigInteger localBigInteger = new BigInteger(1, arrayOfByte);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/bitso/exceptions/BitsoAPIException.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public BitsoAPIException(String message, Throwable cause) {

public BitsoAPIException(String message) {
super(message);
this.mErrorCode = 101;
}

public BitsoAPIException(int errorCode, String message, Throwable initialException) {
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/com/bitso/BitsoMockTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import com.bitso.exceptions.BitsoAPIException;
Expand Down Expand Up @@ -384,6 +385,22 @@ public void testUserTrades()
}
}

@Test
public void testOrderTrades() throws JSONException, BitsoNullException, IOException, BitsoAPIException,
BitsoPayloadException, InterruptedException {
int totalElements = 0;

// TODO:
// This should return a collection of 25 elements, not working limit default value
BitsoTrade[] trades = mBitso.getUserTrades(null);
assertEquals(trades != null, true);
totalElements = trades.length;
assertEquals((totalElements >= 0 && totalElements <= 25), true);
for (BitsoTrade current : trades) {
assertEquals(true, nullCheck(current, BitsoTrade.class));
}
}

@Override
public void testTrading() {
System.out.println("This test is overriden");
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/com/bitso/BitsoServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,14 @@ public void tearDown() {
e.printStackTrace();
}
}

@Override
public void testTrading() {
System.out.println("This test is overriden in BitsoServerTest");
}

@Override
public void testOrderTrades() {
System.out.println("This test is overriden in BitsoServerTest");
}
}
2 changes: 1 addition & 1 deletion src/test/java/com/bitso/BitsoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ public void testUserTrades() throws JSONException, BitsoNullException, IOExcepti
assertEquals((excedingLimit != null || excedingLimit == null), true);
}

// @Test
@Test
public void testOrderTrades() throws JSONException, BitsoNullException, IOException, BitsoAPIException,
BitsoPayloadException, InterruptedException {
int totalElements = 0;
Expand Down

0 comments on commit e3bea1c

Please sign in to comment.