Skip to content

Commit

Permalink
formatting feedback, java7 (bunq#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
tubbynl committed Jun 19, 2018
1 parent 4cd229c commit 6b90696
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 32 deletions.
40 changes: 32 additions & 8 deletions src/main/java/com/bunq/sdk/http/BunqBasicHeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import okhttp3.Response;

import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class BunqBasicHeader {
/**
Expand Down Expand Up @@ -37,10 +38,33 @@ 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));
public static String collectForSigning(
Collection<BunqBasicHeader> headers,
BunqHeader exclude,
Collection<BunqHeader> includes) {
List<String> headersForSigning = new ArrayList<String>();

for (BunqBasicHeader header:headers) {
if (!header.getName().isBunq() && !includes.contains(header.getName())) {
continue;
}

if (exclude != null && exclude.equals(header.getName())) {
continue;
}

headersForSigning.add(header.forSigning());
}

Collections.sort(headersForSigning);

StringBuffer stringBuffer = new StringBuffer();

for (String header:headersForSigning) {
stringBuffer.append(header);
stringBuffer.append(NEWLINE);
}

return stringBuffer.toString();
}
}
2 changes: 0 additions & 2 deletions src/main/java/com/bunq/sdk/http/BunqHeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

public enum BunqHeader {
attachmentDescription("X-Bunq-Attachment-Description"),
Expand Down
32 changes: 14 additions & 18 deletions src/main/java/com/bunq/sdk/security/SecurityUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -380,14 +380,11 @@ private static byte[] getEntityBodyBytes(BunqRequestBuilder requestBuilder) thro
}

private static String generateRequestHeadersSortedString(BunqRequestBuilder bunqRequestBuilder) {
return BunqBasicHeader.collectForSigning(bunqRequestBuilder.getAllHeader()
.stream()
.filter(
header ->
header.getName().isBunq() ||
header.getName().equals(BunqHeader.cacheControl) ||
header.getName().equals(BunqHeader.userAgent)
));
return BunqBasicHeader.collectForSigning(
bunqRequestBuilder.getAllHeader(),
null,
Arrays.asList(BunqHeader.cacheControl,BunqHeader.userAgent)
);
}

/**
Expand Down Expand Up @@ -478,9 +475,10 @@ private static byte[] getResponseBytes(
List<BunqBasicHeader> allResponseHeader = new ArrayList<>();

for (int i = INDEX_FIRST; i < allHeader.names().size(); i++) {
Optional<BunqBasicHeader> header = BunqBasicHeader.get(allHeader.name(i),allHeader.get(allHeader.name(i)));
if(header.isPresent() && !BunqHeader.serverSignature.equals(header.get().getName())) {
allResponseHeader.add(header.get());
BunqHeader header = BunqHeader.parse(allHeader.name(i));

if (header != null && !BunqHeader.serverSignature.equals(header)) {
allResponseHeader.add(new BunqBasicHeader(header,allHeader.get(allHeader.name(i))));
}
}

Expand All @@ -502,12 +500,10 @@ private static byte[] getResponseHeadBytes(int responseCode, List<BunqBasicHeade
}

private static String generateResponseHeadersSortedString(List<BunqBasicHeader> headers) {
return BunqBasicHeader.collectForSigning(headers
.stream()
.filter(
header ->
header.getName().isBunq() &&
!header.getName().equals(BunqHeader.serverSignature)
));
return BunqBasicHeader.collectForSigning(
headers,
BunqHeader.serverSignature,
Collections.emptyList()
);
}
}
23 changes: 19 additions & 4 deletions src/test/java/com/bunq/sdk/http/BunqHeaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,30 @@ public class BunqHeaderTest {
@Test
public void parse() {
// parse works case-insensitive
assertEquals(BunqHeader.clientResponseId,BunqHeader.parse("X-Bunq-Client-Response-Id").get());
assertEquals(BunqHeader.clientResponseId,BunqHeader.parse("x-bunq-client-response-id").get());
assertEquals(BunqHeader.clientResponseId,BunqHeader.parse("X-Bunq-Client-Response-Id"));
assertEquals(BunqHeader.clientResponseId,BunqHeader.parse("x-bunq-client-response-id"));
}

@Test
public void isBunq() {
List<BunqHeader> nonBunqHeaders = Arrays.asList(BunqHeader.cacheControl,BunqHeader.contentType,BunqHeader.userAgent);
assertFalse(BunqHeader.cacheControl.isBunq());
assertFalse(BunqHeader.contentType.isBunq());
assertFalse(BunqHeader.userAgent.isBunq());

assertEquals(nonBunqHeaders, Stream.of(BunqHeader.values()).filter(h->!h.isBunq()).collect(Collectors.toList()));
assertTrue(BunqHeader.attachmentDescription.isBunq());
assertTrue(BunqHeader.clientAuthentication.isBunq());
assertTrue(BunqHeader.clientEncryptionHMAC.isBunq());
assertTrue(BunqHeader.clientEncryptionIV.isBunq());
assertTrue(BunqHeader.clientEncryptionIV.isBunq());
assertTrue(BunqHeader.clientEncryptionKey.isBunq());
assertTrue(BunqHeader.clientRequestId.isBunq());
assertTrue(BunqHeader.clientSignature.isBunq());
assertTrue(BunqHeader.clientResponseId.isBunq());
assertTrue(BunqHeader.geolocation.isBunq());
assertTrue(BunqHeader.region.isBunq());
assertTrue(BunqHeader.serverSignature.isBunq());
assertTrue(BunqHeader.clientEncryptionKey.isBunq());
assertTrue(BunqHeader.clientRequestId.isBunq());
}

@Test
Expand Down

0 comments on commit 6b90696

Please sign in to comment.