From 6744ab167be210c91c54a58e6d86a4bffbcf961e Mon Sep 17 00:00:00 2001 From: "David J. Kordsmeier" Date: Thu, 8 Nov 2018 18:43:43 -0800 Subject: [PATCH] Implement a workable solution for issue #345 Also tackle legacy device support, downgrade minSdkVersion to 18 from 19 unless someone can tell me that there is a reason the code base must be on API 19. Note: I've been working at this to be able to continue to maintain at least one Github client working with BB10 (Blackberry OS10), which has ForkHub as its sole Github client in the app store BlackberryWorld (also in Amazon store), however this app stopped working about a year or so back, and this should provide a fix. Work with upstream maintainers to sort out cleanest fix as this only takes into account a build specific to the legacy devices. --- app/build.gradle | 3 +- .../java/com/github/mobile/DefaultClient.java | 39 +++++++++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 757a96b2f..eb0abd69a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -11,7 +11,7 @@ android { defaultConfig { applicationId 'jp.forkhub' - minSdkVersion 19 + minSdkVersion 18 targetSdkVersion 27 versionCode 1020900 versionName '1.2.9' @@ -55,4 +55,5 @@ dependencies { //Self compiled .aar version of wishlist compile(name: 'lib', ext: 'aar') + implementation 'com.android.support:appcompat-v7:27.1.0' } diff --git a/app/src/main/java/com/github/mobile/DefaultClient.java b/app/src/main/java/com/github/mobile/DefaultClient.java index 9ab4b1456..aaed4c3b8 100644 --- a/app/src/main/java/com/github/mobile/DefaultClient.java +++ b/app/src/main/java/com/github/mobile/DefaultClient.java @@ -20,6 +20,13 @@ import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; import okhttp3.OkHttpClient; import okhttp3.OkUrlFactory; @@ -50,8 +57,34 @@ public DefaultClient() { */ @Override protected HttpURLConnection createConnection(String uri) throws IOException { - OkUrlFactory factory = new OkUrlFactory(new OkHttpClient()); - URL url = new URL(createUri(uri)); - return factory.open(url); + // OkUrlFactory factory = new OkUrlFactory(new OkHttpClient()); + // + // TODO: Look through code base to explore how project handles exceptions + // generally, and what to do about the fact this connection has several + // new ways it can throw an exception, instead of burrying the fault of KeyManagementException or + // NoSuchAlgorithmException + try { + // NOTE: The deprecated signature below for sslSocketFactory() + // final OkHttpClient client = new OkHttpClient.Builder().sslSocketFactory(new TLSSocketFactory()).build(); + + // public OkHttpClient.Builder sslSocketFactory(SSLSocketFactory sslSocketFactory, + // X509TrustManager trustManager) + TrustManager[] trustManagers = new TrustManager[] { new TrustManagerManipulator() }; + + SSLContext context = SSLContext.getInstance("TLS"); + context.init(null, trustManagers, new SecureRandom()); + final OkHttpClient client2 = new OkHttpClient.Builder().sslSocketFactory(new TLSSocketFactory(context.getSocketFactory()), (X509TrustManager) trustManagers[0]).build(); + URL url = new URL(createUri(uri)); + OkUrlFactory factory = new OkUrlFactory(client2); + return factory.open(url); + } catch (KeyManagementException e) { + e.printStackTrace(); + throw new IOException(e.getMessage()); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + throw new IOException(e.getMessage()); + } + // URL url = new URL(createUri(uri)); + // return factory.open(url); } }