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

Config option for SSL Host verification #17

Open
wants to merge 1 commit 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 53 additions & 10 deletions src/main/java/com/icoderman/woocommerce/DefaultHttpClient.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package com.icoderman.woocommerce;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.net.URISyntaxException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.net.ssl.SSLContext;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
Expand All @@ -12,17 +19,17 @@
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.TrustStrategy;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class DefaultHttpClient implements HttpClient {

Expand All @@ -33,11 +40,47 @@ public class DefaultHttpClient implements HttpClient {
private ObjectMapper mapper;

public DefaultHttpClient() {
this.httpClient = HttpClientBuilder.create().build();
this.mapper = new ObjectMapper();
super();
createDefaultHttpClient(true);
}

@Override
public DefaultHttpClient(Boolean sslTrusted) {
super();
createDefaultHttpClient(true);
}


private void createDefaultHttpClient(Boolean sslTrusted) {

SSLContext sslContext = getSslContext();

if (sslTrusted)
this.httpClient = HttpClientBuilder.create().setSSLContext(sslContext)
.build();
else
this.httpClient = HttpClientBuilder.create().setSSLContext(sslContext)
.build();

this.mapper = new ObjectMapper();
}

private SSLContext getSslContext() {
TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
public boolean isTrusted(X509Certificate[] certificate, String authType) {
return true;
}
};

SSLContext sslContext = null;
try {
sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
} catch (Exception e) {
// Handle error
}
return sslContext;
}

@Override
public Map get(String url) {
HttpGet httpGet = new HttpGet(url);
return getEntityAndReleaseConnection(httpGet, Map.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class WooCommerceAPI implements WooCommerce {

public WooCommerceAPI(OAuthConfig config, ApiVersionType apiVersion) {
this.config = config;
this.client = new DefaultHttpClient();
this.client = new DefaultHttpClient(config.getSslTrusted());
this.apiVersion = apiVersion.getValue();
}

Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/icoderman/woocommerce/oauth/OAuthConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ public final class OAuthConfig {
private final String url;
private final String consumerKey;
private final String consumerSecret;

public OAuthConfig(String url, String consumerKey, String consumerSecret) {
private final Boolean sslTrusted;

public OAuthConfig(String url, String consumerKey, String consumerSecret, Boolean sslTrusted) {
if (url == null || url.isEmpty() ||
consumerKey == null || consumerKey.isEmpty() ||
consumerSecret == null || consumerSecret.isEmpty()) {
Expand All @@ -15,6 +16,7 @@ public OAuthConfig(String url, String consumerKey, String consumerSecret) {
this.url = url;
this.consumerKey = consumerKey;
this.consumerSecret = consumerSecret;
this.sslTrusted = sslTrusted;
}

public String getUrl() {
Expand All @@ -28,4 +30,8 @@ public String getConsumerKey() {
public String getConsumerSecret() {
return consumerSecret;
}

public Boolean getSslTrusted() {
return sslTrusted;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ public class WooCommerceClientTest {
private static final String CONSUMER_KEY = "ck_d35e7be7cc695d87f23490729dd80e173f88c8f5";
private static final String CONSUMER_SECRET = "cs_53a835760712ebf0c8bcf2a21197af4b2323a052";
private static final String WC_URL = "http://localhost/index.php";
private static final Boolean SSL_TRUSTED = true;

private WooCommerce wooCommerce;

@Before
public void setUp() {
wooCommerce = new WooCommerceAPI(new OAuthConfig(WC_URL, CONSUMER_KEY, CONSUMER_SECRET), ApiVersionType.V2);
wooCommerce = new WooCommerceAPI(new OAuthConfig(WC_URL, CONSUMER_KEY, CONSUMER_SECRET,SSL_TRUSTED), ApiVersionType.V2);
}

@Ignore
Expand Down