From dece49971c540ed4117264edc252122e0eb0ae57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michae=CC=88l=20van=20de=20Giessen?= Date: Sun, 17 Jun 2018 21:01:46 +0200 Subject: [PATCH] fix test on header signing (bunq/sdk_java#93) --- .../com/bunq/sdk/http/BunqBasicHeader.java | 19 ++++++++- .../com/bunq/sdk/http/BunqRequestBuilder.java | 8 +--- .../com/bunq/sdk/security/SecurityUtils.java | 41 +++++++------------ 3 files changed, 33 insertions(+), 35 deletions(-) diff --git a/src/main/java/com/bunq/sdk/http/BunqBasicHeader.java b/src/main/java/com/bunq/sdk/http/BunqBasicHeader.java index c3206eb4..61e838a4 100644 --- a/src/main/java/com/bunq/sdk/http/BunqBasicHeader.java +++ b/src/main/java/com/bunq/sdk/http/BunqBasicHeader.java @@ -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; @@ -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 headers) { + return headers + .map(BunqBasicHeader::forSigning) + .sorted() + .collect(Collectors.joining(NEWLINE)); + } +} \ No newline at end of file diff --git a/src/main/java/com/bunq/sdk/http/BunqRequestBuilder.java b/src/main/java/com/bunq/sdk/http/BunqRequestBuilder.java index 5aa2c3b1..49f3fcc6 100644 --- a/src/main/java/com/bunq/sdk/http/BunqRequestBuilder.java +++ b/src/main/java/com/bunq/sdk/http/BunqRequestBuilder.java @@ -205,13 +205,7 @@ public BunqRequestBody getBody() { /** */ - public List getAllHeaderAsList() { + public List getAllHeader() { return this.allHeader; } - - /** - */ - public BunqBasicHeader[] getAllHeaderAsArray() { - return this.allHeader.toArray(new BunqBasicHeader[this.allHeader.size()]); - } } diff --git a/src/main/java/com/bunq/sdk/security/SecurityUtils.java b/src/main/java/com/bunq/sdk/security/SecurityUtils.java index ed42e634..6e56c6ce 100644 --- a/src/main/java/com/bunq/sdk/security/SecurityUtils.java +++ b/src/main/java/com/bunq/sdk/security/SecurityUtils.java @@ -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. @@ -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. @@ -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)); + )); } /** @@ -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); @@ -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 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 headers) { + return BunqBasicHeader.collectForSigning(headers + .stream() + .filter( + header -> + header.getName().isBunq() && + !header.getName().equals(BunqHeader.serverSignature) + )); } - }