From 734653b79dbc7a1948113bb154beb264cdd7baa4 Mon Sep 17 00:00:00 2001 From: Jose Luis Parrilla Date: Wed, 22 May 2019 13:00:52 +0200 Subject: [PATCH] Config option for SSL Host verification --- .../woocommerce/DefaultHttpClient.java | 63 ++++++++++++++++--- .../icoderman/woocommerce/WooCommerceAPI.java | 2 +- .../woocommerce/oauth/OAuthConfig.java | 10 ++- .../integration/WooCommerceClientTest.java | 3 +- 4 files changed, 64 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/icoderman/woocommerce/DefaultHttpClient.java b/src/main/java/com/icoderman/woocommerce/DefaultHttpClient.java index e392865..8d5b2c4 100644 --- a/src/main/java/com/icoderman/woocommerce/DefaultHttpClient.java +++ b/src/main/java/com/icoderman/woocommerce/DefaultHttpClient.java @@ -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; @@ -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 { @@ -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); diff --git a/src/main/java/com/icoderman/woocommerce/WooCommerceAPI.java b/src/main/java/com/icoderman/woocommerce/WooCommerceAPI.java index 9958dc5..78bb650 100644 --- a/src/main/java/com/icoderman/woocommerce/WooCommerceAPI.java +++ b/src/main/java/com/icoderman/woocommerce/WooCommerceAPI.java @@ -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(); } diff --git a/src/main/java/com/icoderman/woocommerce/oauth/OAuthConfig.java b/src/main/java/com/icoderman/woocommerce/oauth/OAuthConfig.java index 5d9dd77..ee8a857 100644 --- a/src/main/java/com/icoderman/woocommerce/oauth/OAuthConfig.java +++ b/src/main/java/com/icoderman/woocommerce/oauth/OAuthConfig.java @@ -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()) { @@ -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() { @@ -28,4 +30,8 @@ public String getConsumerKey() { public String getConsumerSecret() { return consumerSecret; } + + public Boolean getSslTrusted() { + return sslTrusted; + } } diff --git a/src/test/java/com/icoderman/woocommerce/integration/WooCommerceClientTest.java b/src/test/java/com/icoderman/woocommerce/integration/WooCommerceClientTest.java index 0b19301..c8d4a0a 100644 --- a/src/test/java/com/icoderman/woocommerce/integration/WooCommerceClientTest.java +++ b/src/test/java/com/icoderman/woocommerce/integration/WooCommerceClientTest.java @@ -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