-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
extracted header-magic as enum (bunq/sdk_java#93) #102
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6486716
extracted header-magic as enum (bunq/sdk_java#93)
tubbynl ffb56ad
fix test on header signing (bunq/sdk_java#93)
tubbynl ad67ada
formatting feedback (bunq/sdk_java#93)
tubbynl 4cd229c
formatting feedback, java7 (bunq/sdk_java#93)
tubbynl 6b90696
formatting feedback, java7 (bunq/sdk_java#93)
tubbynl 30d43ee
formatting feedback, java7 (bunq/sdk_java#93)
tubbynl abb8cb6
formatting feedback, java7 (bunq/sdk_java#93)
tubbynl 0ebd723
formatting feedback (bunq/sdk_java#93)
tubbynl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,70 @@ | ||
package com.bunq.sdk.http; | ||
|
||
public class BunqBasicHeader { | ||
private String name; | ||
private String value; | ||
import okhttp3.Response; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class BunqBasicHeader { | ||
/** | ||
* String format constants for signing data | ||
*/ | ||
public BunqBasicHeader(String name, String value) { | ||
private static final String DELIMITER_HEADER_NAME_AND_VALUE = ": "; | ||
private static final String NEWLINE = "\n"; | ||
|
||
private final BunqHeader name; | ||
private final String value; | ||
|
||
public static BunqBasicHeader get(BunqHeader header, Response response) { | ||
return new BunqBasicHeader(header, response.header(header.getHeader())); | ||
} | ||
|
||
public BunqBasicHeader(BunqHeader name, String value) { | ||
this.name = name; | ||
this.value = value; | ||
} | ||
|
||
/** | ||
*/ | ||
public String getName() { | ||
public BunqHeader getName() { | ||
return name; | ||
} | ||
|
||
/** | ||
*/ | ||
public String getValue() { | ||
return value; | ||
} | ||
|
||
private String forSigning() { | ||
return getName().getHeader() + DELIMITER_HEADER_NAME_AND_VALUE + getValue(); | ||
} | ||
|
||
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package com.bunq.sdk.http; | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
|
||
public enum BunqHeader { | ||
attachmentDescription("X-Bunq-Attachment-Description"), | ||
cacheControl("Cache-Control", "no-cache"), | ||
contentType("Content-Type"), | ||
clientAuthentication("X-Bunq-Client-Authentication"), | ||
clientEncryptionHMAC("X-Bunq-Client-Encryption-Hmac"), | ||
clientEncryptionIV("X-Bunq-Client-Encryption-Iv"), | ||
clientEncryptionKey("X-Bunq-Client-Encryption-Key"), | ||
clientRequestId("X-Bunq-Client-Request-Id"), | ||
clientSignature("X-Bunq-Client-Signature"), | ||
clientResponseId("X-Bunq-Client-Response-Id", "Could not determine response id."), | ||
geolocation("X-Bunq-Geolocation", "0 0 0 0 000"), | ||
language("X-Bunq-Language", "en_US"), | ||
region("X-Bunq-Region", "nl_NL"), | ||
serverSignature("X-Bunq-Server-Signature"), | ||
userAgent("User-Agent", "bunq-sdk-java/0.13.1"); | ||
|
||
private static final String PREFIX = "X-Bunq-"; | ||
|
||
private final String header; | ||
private final String defaultValue; | ||
|
||
BunqHeader(String header) { | ||
this(header, null); | ||
} | ||
|
||
BunqHeader(String header, String defaultValue) { | ||
this.header = header; | ||
this.defaultValue = defaultValue; | ||
} | ||
|
||
public static BunqHeader parse(String value) { | ||
for (BunqHeader header:values()) { | ||
if (header.equals(value)) { | ||
return header; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public String getHeader() { | ||
return header; | ||
} | ||
|
||
public String getDefaultValue() { | ||
return defaultValue; | ||
} | ||
|
||
private String getOrDefault(String value) { | ||
if (value != null) { | ||
return value; | ||
} | ||
|
||
return getDefaultValue(); | ||
} | ||
|
||
public void addTo(Map<String, String> headers, String value) { | ||
headers.put(getHeader(), getOrDefault(value)); | ||
} | ||
|
||
public void addTo(BunqRequestBuilder requestBuilder) { | ||
addTo(requestBuilder, null); | ||
} | ||
|
||
public void addTo(BunqRequestBuilder requestBuilder, String value) { | ||
requestBuilder.addHeader(getHeader(), getOrDefault(value)); | ||
} | ||
|
||
public boolean equals(String header) { | ||
return getHeader().equalsIgnoreCase(header); | ||
} | ||
|
||
public boolean isBunq() { | ||
return getHeader().startsWith(PREFIX); | ||
} | ||
|
||
private String findKey(Collection<String> keys) { | ||
for (String key:keys) { | ||
if (this.equals(key)) { | ||
return key; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public String getOrDefault(Map<String, String> headers) { | ||
String key = findKey(headers.keySet()); | ||
if (key != null && headers.get(key) != null) { | ||
return headers.get(key); | ||
} | ||
return getDefaultValue(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is what bunq ? Please rename to
isBunqHeader