Skip to content

Commit

Permalink
allow setting a custom HostNameVerifier for issue #322
Browse files Browse the repository at this point in the history
  • Loading branch information
ryber committed Dec 6, 2019
1 parent 493b173 commit 41f9132
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
16 changes: 16 additions & 0 deletions unirest/src/main/java/kong/unirest/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.apache.http.client.HttpClient;
import org.apache.http.nio.client.HttpAsyncClient;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -84,6 +85,7 @@ public class Config {
private long ttl = -1;
private SSLContext sslContext;
private Interceptor interceptor = new DefaultInterceptor();
private HostnameVerifier hostnameVerifier;

public Config() {
setDefaults();
Expand Down Expand Up @@ -243,6 +245,16 @@ public Config sslContext(SSLContext ssl) {
return this;
}

/**
* Set a custom HostnameVerifier
* @param value the verifier
* @return this config object
*/
public Config hostnameVerifier(HostnameVerifier value) {
this.hostnameVerifier = value;
return this;
}

private void verifySecurityConfig(Object thing) {
if(thing != null){
throw new UnirestConfigException("You may only configure a SSLContext OR a Keystore, but not both");
Expand Down Expand Up @@ -813,4 +825,8 @@ public SSLContext getSslContext() {
private Optional<DefaultInterceptor> getDefaultInterceptor() {
return tryCast(getUniInterceptor(), DefaultInterceptor.class);
}

public HostnameVerifier getHostnameVerifier() {
return hostnameVerifier;
}
}
11 changes: 10 additions & 1 deletion unirest/src/main/java/kong/unirest/apache/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.SSLContexts;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -104,10 +105,18 @@ private Registry<ConnectionSocketFactory> createDisabledSSLContext() throws Exce

private SSLConnectionSocketFactory getSocketFactory() {
if(sslSocketFactory == null) {
sslSocketFactory = new SSLConnectionSocketFactory(createSslContext(), new NoopHostnameVerifier());
sslSocketFactory = new SSLConnectionSocketFactory(createSslContext(), getHostnameVerifier());
}
return sslSocketFactory;
}

private HostnameVerifier getHostnameVerifier() {
if(config.getHostnameVerifier() != null){
return config.getHostnameVerifier();
}
return NoopHostnameVerifier.INSTANCE;
}

private SSLContext createSslContext() {
if(sslContext == null) {
if(config.getSslContext() != null){
Expand Down
8 changes: 8 additions & 0 deletions unirest/src/test/java/BehaviorTests/CertificateTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ public void loadWithSSLContext() throws Exception {
assertEquals(200, response);
}

@Test
public void canSetHoestNameVerifyer() throws Exception {
Unirest.config().hostnameVerifier(new NoopHostnameVerifier());

int response = Unirest.get("https://badssl.com/").asEmpty().getStatus();
assertEquals(200, response);
}

@Test
public void rawApacheClientCert() throws Exception {
SSLContext sslContext = SSLContexts.custom()
Expand Down

0 comments on commit 41f9132

Please sign in to comment.