Skip to content
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

code refactoring #121

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/main/java/com/braintreegateway/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,10 @@ public int getConnectTimeout() {
public void setConnectTimeout(Integer timeout) {
this.connectTimeout = timeout;
}

public String connectUrl(OAuthConnectUrlRequest request) {
request.clientId(getClientId());
String queryString = request.toQueryString();
return getBaseURL() + "/oauth/connect?" + queryString;
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/braintreegateway/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public class Customer {
private Map<String, String> customFields;

public Customer(NodeWrapper node) {
setCustomerNodeWrapperStrings(node);
setCustomerNodeWrapperResponses(node);
}

private void setCustomerNodeWrapperStrings(NodeWrapper node) {
company = node.findString("company");
createdAt = node.findDateTime("created-at");
customFields = node.findMap("custom-fields/*");
Expand All @@ -44,6 +49,9 @@ public Customer(NodeWrapper node) {
phone = node.findString("phone");
updatedAt = node.findDateTime("updated-at");
website = node.findString("website");
}

private void setCustomerNodeWrapperResponses(NodeWrapper node) {
creditCards = new ArrayList<CreditCard>();
for (NodeWrapper creditCardResponse : node.findAll("credit-cards/credit-card")) {
creditCards.add(new CreditCard(creditCardResponse));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public MerchantAccount find(String id) {
}

public Result<MerchantAccount> update(String id, MerchantAccountRequest request) {
final NodeWrapper response = http.put(configuration.getMerchantPath() + "/merchant_accounts/" + id + "/update_via_api", request);
String updateUrl = "/merchant_accounts/" + id + "/update_via_api";
final NodeWrapper response = http.put(configuration.getMerchantPath() + updateUrl, request);
return new Result<MerchantAccount>(response, MerchantAccount.class);
}

Expand Down
5 changes: 0 additions & 5 deletions src/main/java/com/braintreegateway/OAuthGateway.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,4 @@ public Result<OAuthResult> revokeAccessToken(String accessToken) {
return new Result<OAuthResult>(response, OAuthResult.class);
}

public String connectUrl(OAuthConnectUrlRequest request) {
request.clientId(configuration.getClientId());
String queryString = request.toQueryString();
return configuration.getBaseURL() + "/oauth/connect?" + queryString;
}
}
7 changes: 0 additions & 7 deletions src/main/java/com/braintreegateway/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,4 @@ public String getKind() {
return null;
}

protected String buildXMLElement(Object element) {
return RequestBuilder.buildXMLElement(element);
}

protected String buildXMLElement(String name, Object element) {
return RequestBuilder.buildXMLElement(name, element);
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/braintreegateway/SearchCriteria.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
public class SearchCriteria extends Request {
private String xml;

protected String buildXMLElement(String name, Object element) {
return RequestBuilder.buildXMLElement(name, element);
}

public SearchCriteria(String type, Object value) {
this.xml = buildXMLElement(type, value);
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/braintreegateway/util/Crypto.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public class Crypto {
public Boolean secureCompare(String left, String right) {
if (left == null || right == null || (left.length() != right.length())) {
if (checkSecureCompareDirections(left, right)) {
return false;
}

Expand All @@ -15,4 +15,8 @@ public Boolean secureCompare(String left, String right) {
}
return result == 0;
}

public Boolean checkSecureCompareDirections(String left, String right) {
return left == null || right == null || (left.length() != right.length());
}
}
4 changes: 3 additions & 1 deletion src/main/java/com/braintreegateway/util/Hasher.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

public interface Hasher {

public String hmacHash(String privateKey, String content);
public String hmacHash(String privateKey, String content, String shaAlgorithm);

public byte[] shaTypeBytes(String string, String shaAlgorithm);

}
26 changes: 2 additions & 24 deletions src/main/java/com/braintreegateway/util/Sha1Hasher.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,8 @@
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class Sha1Hasher implements Hasher {
public class Sha1Hasher extends ShaTypeHasher {
public String hmacHash(String privateKey, String content) {
String hash = "";
try {
SecretKeySpec signingKey = new SecretKeySpec(sha1Bytes(privateKey), "SHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(signingKey);

byte[] rawMac = mac.doFinal(content.getBytes("UTF-8"));
byte[] hexBytes = new Hex().encode(rawMac);
hash = new String(hexBytes, "ISO-8859-1");
} catch (Exception e) {
throw new RuntimeException(e);
}
return hash;
}

private byte[] sha1Bytes(String string) {
try {
MessageDigest md = MessageDigest.getInstance("SHA1");
md.update(string.getBytes("UTF-8"));
return md.digest();
} catch (Exception e) {
throw new RuntimeException(e);
}
return super.hmacHash(privateKey, content, "SHA1");
}
}
26 changes: 2 additions & 24 deletions src/main/java/com/braintreegateway/util/Sha256Hasher.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,9 @@
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class Sha256Hasher implements Hasher {
public class Sha256Hasher extends ShaTypeHasher {

public String hmacHash(String privateKey, String content) {
String hash = "";
try {
SecretKeySpec signingKey = new SecretKeySpec(sha256Bytes(privateKey), "SHA-256");
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(signingKey);

byte[] rawMac = mac.doFinal(content.getBytes("UTF-8"));
byte[] hexBytes = new Hex().encode(rawMac);
hash = new String(hexBytes, "ISO-8859-1");
} catch (Exception e) {
throw new RuntimeException(e);
}
return hash;
}

private byte[] sha256Bytes(String string) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(string.getBytes("UTF-8"));
return md.digest();
} catch (Exception e) {
throw new RuntimeException(e);
}
return super.hmacHash(privateKey, content, "SHA-256");
}
}
41 changes: 41 additions & 0 deletions src/main/java/com/braintreegateway/util/ShaTypeHasher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.braintreegateway.util;

import com.braintreegateway.org.apache.commons.codec.binary.Hex;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;

public class ShaTypeHasher implements Hasher{
public String hmacHash(String privateKey, String content, String shaAlgorithm) {
String hash = "";
try {
SecretKeySpec signingKey = new SecretKeySpec(shaTypeBytes(privateKey, shaAlgorithm), shaAlgorithm);
String macShaInstance = "";
if(shaAlgorithm.equals("SHA1")) {
macShaInstance = "HmacSHA1";
} else {
macShaInstance = "HmacSHA256";
}
Mac mac = Mac.getInstance(macShaInstance);
mac.init(signingKey);

byte[] rawMac = mac.doFinal(content.getBytes("UTF-8"));
byte[] hexBytes = new Hex().encode(rawMac);
hash = new String(hexBytes, "ISO-8859-1");
} catch (Exception e) {
throw new RuntimeException(e);
}
return hash;
}

public byte[] shaTypeBytes(String string, String shaAlgorithm) {
try {
MessageDigest md = MessageDigest.getInstance(shaAlgorithm);
md.update(string.getBytes("UTF-8"));
return md.digest();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@ public class SignatureService {
private final String key;
private final Hasher hasher;

public SignatureService(String key, Hasher hasher) {
private final String shaAlgorithm;

public SignatureService(String key, Hasher hasher, String shaAlgorithm) {
this.key = key;
this.hasher = hasher;
this.shaAlgorithm = shaAlgorithm;
}

public String sign(String query) {
return hash(query) + "|" + query;
}

private String hash(String string) {
return hasher.hmacHash(key, string);
return hasher.hmacHash(key, string, shaAlgorithm);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void connectUrlReturnsCorrectUrl() {
establishedOn("1988-10").
done();

String urlString = gateway.oauth().connectUrl(request);
String urlString = gateway.getConfiguration().connectUrl(request);

URL url;

Expand Down Expand Up @@ -203,7 +203,7 @@ public void connectUrlReturnsCorrectUrl() {
public void connectUrlReturnsCorrectUrlWithoutOptionalParams() {
OAuthConnectUrlRequest request = new OAuthConnectUrlRequest();

String urlString = gateway.oauth().connectUrl(request);
String urlString = gateway.getConfiguration().connectUrl(request);

URL url;

Expand All @@ -225,7 +225,7 @@ public void connectUrlReturnsCorrectPaymentMethods() {
OAuthConnectUrlRequest request = new OAuthConnectUrlRequest().
paymentMethods(new String[] {"credit_card", "paypal"});

String urlString = gateway.oauth().connectUrl(request);
String urlString = gateway.getConfiguration().connectUrl(request);

URL url;

Expand All @@ -248,7 +248,7 @@ public void connectUrlCanIncludeSignupOnly() {
OAuthConnectUrlRequest request = new OAuthConnectUrlRequest()
.signupOnly(true);

String urlString = gateway.oauth().connectUrl(request);
String urlString = gateway.getConfiguration().connectUrl(request);

try {
URL url = new URL(urlString);
Expand All @@ -269,7 +269,7 @@ public void connectUrlOnlyIncludesLoginOnlyIfBothLoginOnlyAndSignupOnlyAreSpecif
.loginOnly(true)
.signupOnly(true);

String urlString = gateway.oauth().connectUrl(request);
String urlString = gateway.getConfiguration().connectUrl(request);

try {
URL url = new URL(urlString);
Expand Down