Skip to content

Commit

Permalink
fix test on header signing (bunq#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
tubbynl committed Jun 17, 2018
1 parent 6486716 commit dece499
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 35 deletions.
19 changes: 18 additions & 1 deletion src/main/java/com/bunq/sdk/http/BunqBasicHeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

import okhttp3.Response;

import java.util.stream.Collectors;
import java.util.stream.Stream;

public class BunqBasicHeader {
private static final String DELIMITER_HEADER_NAME_AND_VALUE = ": ";
private static final String NEWLINE = "\n";

private final BunqHeader name;
private final String value;

Expand All @@ -26,4 +32,15 @@ public BunqHeader getName() {
public String getValue() {
return value;
}
}

private String forSigning() {
return getName().getHeader()+DELIMITER_HEADER_NAME_AND_VALUE+getValue();
}

public static String collectForSigning(Stream<BunqBasicHeader> headers) {
return headers
.map(BunqBasicHeader::forSigning)
.sorted()
.collect(Collectors.joining(NEWLINE));
}
}
8 changes: 1 addition & 7 deletions src/main/java/com/bunq/sdk/http/BunqRequestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,7 @@ public BunqRequestBody getBody() {

/**
*/
public List<BunqBasicHeader> getAllHeaderAsList() {
public List<BunqBasicHeader> getAllHeader() {
return this.allHeader;
}

/**
*/
public BunqBasicHeader[] getAllHeaderAsArray() {
return this.allHeader.toArray(new BunqBasicHeader[this.allHeader.size()]);
}
}
41 changes: 14 additions & 27 deletions src/main/java/com/bunq/sdk/security/SecurityUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* Static lib containing methods for handling encryption.
Expand Down Expand Up @@ -127,7 +125,6 @@ public final class SecurityUtils {
* Delimiter constants for building the data to sign.
*/
private static final String DELIMITER_METHOD_PATH = " ";
private static final String DELIMITER_HEADER_NAME_AND_VALUE = ": ";

/**
* The index of the first item in an array.
Expand Down Expand Up @@ -386,16 +383,14 @@ private static byte[] getEntityBodyBytes(BunqRequestBuilder requestBuilder) thro
}

private static String generateRequestHeadersSortedString(BunqRequestBuilder bunqRequestBuilder) {
return Arrays.stream(bunqRequestBuilder.getAllHeaderAsArray())
.filter(
return BunqBasicHeader.collectForSigning(bunqRequestBuilder.getAllHeader()
.stream()
.filter(
header ->
header.getName().isBunq() ||
header.getName().equals(BunqHeader.cacheControl) ||
header.getName().equals(BunqHeader.userAgent)
)
.map(header -> header.getName() + DELIMITER_HEADER_NAME_AND_VALUE + header.getValue())
.sorted()
.collect(Collectors.joining(NEWLINE));
));
}

/**
Expand Down Expand Up @@ -493,12 +488,7 @@ private static byte[] getResponseBytes(
}

try {
outputStream.write(
getResponseHeadBytes(
responseCode,
allResponseHeader.toArray(new BunqBasicHeader[allResponseHeader.size()])
)
);
outputStream.write(getResponseHeadBytes(responseCode,allResponseHeader));
outputStream.write(responseBodyBytes);
} catch (IOException exception) {
throw new UncaughtExceptionError(exception);
Expand All @@ -507,23 +497,20 @@ private static byte[] getResponseBytes(
return outputStream.toByteArray();
}

private static byte[] getResponseHeadBytes(int responseCode, BunqBasicHeader[] responseHeaders) {
private static byte[] getResponseHeadBytes(int responseCode, List<BunqBasicHeader> responseHeaders) {
String requestHeadString = responseCode + NEWLINE +
generateResponseHeadersSortedString(responseHeaders) + NEWLINE + NEWLINE;

return requestHeadString.getBytes();
}

private static String generateResponseHeadersSortedString(BunqBasicHeader[] responseHeaders) {
return Arrays.stream(responseHeaders)
.filter(
header ->
header.getName().isBunq() &&
!header.getName().equals(BunqHeader.serverSignature)
)
.map(header -> header.getName() + DELIMITER_HEADER_NAME_AND_VALUE + header.getValue())
.sorted()
.collect(Collectors.joining(NEWLINE));
private static String generateResponseHeadersSortedString(List<BunqBasicHeader> headers) {
return BunqBasicHeader.collectForSigning(headers
.stream()
.filter(
header ->
header.getName().isBunq() &&
!header.getName().equals(BunqHeader.serverSignature)
));
}

}

0 comments on commit dece499

Please sign in to comment.