Skip to content

Commit

Permalink
Fixes TLS Certs on Nougat
Browse files Browse the repository at this point in the history
  • Loading branch information
sachincool committed Feb 24, 2018
1 parent 23240a4 commit 9027fd2
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import android.content.Context;

import java.util.concurrent.TimeUnit;

import javax.inject.Singleton;

import dagger.Module;
Expand All @@ -15,6 +13,7 @@
import openfoodfacts.github.scrachx.openfood.category.mapper.CategoryMapper;
import openfoodfacts.github.scrachx.openfood.category.network.CategoryNetworkService;
import openfoodfacts.github.scrachx.openfood.dagger.Qualifiers;
import openfoodfacts.github.scrachx.openfood.utils.Utils;
import openfoodfacts.github.scrachx.openfood.views.OFFApplication;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
Expand All @@ -41,11 +40,7 @@ Context provideApplicationContext() {
return application;
}

private final static OkHttpClient httpClient = new OkHttpClient.Builder()
.connectTimeout(5000, TimeUnit.MILLISECONDS)
.readTimeout(30000, TimeUnit.MILLISECONDS)
.writeTimeout(30000, TimeUnit.MILLISECONDS)
.build();
private final static OkHttpClient httpClient = Utils.HttpClientBuilder();

@Provides
@Singleton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import me.dm7.barcodescanner.zxing.ZXingScannerView;
import okhttp3.MediaType;
Expand Down Expand Up @@ -59,19 +58,15 @@
import static openfoodfacts.github.scrachx.openfood.models.ProductImageField.NUTRITION;
import static openfoodfacts.github.scrachx.openfood.network.OpenFoodAPIService.PRODUCT_API_COMMENT;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;

public class OpenFoodAPIClient {

private AllergenDao mAllergenDao;
private HistoryProductDao mHistoryProductDao;

private static final JacksonConverterFactory jacksonConverterFactory = JacksonConverterFactory.create();

private final static OkHttpClient httpClient = new OkHttpClient.Builder()
.connectTimeout(5000, TimeUnit.MILLISECONDS)
.readTimeout(30000, TimeUnit.MILLISECONDS)
.writeTimeout(30000, TimeUnit.MILLISECONDS)
.build();

private static OkHttpClient httpClient = Utils.HttpClientBuilder();

private final OpenFoodAPIService apiService;

Expand All @@ -82,6 +77,7 @@ public OpenFoodAPIClient(Activity activity) {
}

private OpenFoodAPIClient(String apiUrl) {

apiService = new Retrofit.Builder()
.baseUrl(apiUrl)
.client(httpClient)
Expand Down Expand Up @@ -304,7 +300,7 @@ public void onResponse(Call<AllergenRestResponse> call, Response<AllergenRestRes

@Override
public void onFailure(Call<AllergenRestResponse> call, Throwable t) {

onAllergensCallback.onAllergensResponse(false);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Field;
import retrofit2.http.FieldMap;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.Part;
import retrofit2.http.PartMap;
import retrofit2.http.Path;
import retrofit2.http.Query;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collections;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

import okhttp3.CipherSuite;
import okhttp3.ConnectionSpec;
import okhttp3.OkHttpClient;
import okhttp3.TlsVersion;
import openfoodfacts.github.scrachx.openfood.BuildConfig;
import openfoodfacts.github.scrachx.openfood.R;
import openfoodfacts.github.scrachx.openfood.models.DaoSession;
Expand Down Expand Up @@ -270,4 +276,29 @@ public static boolean isHardwareCameraInstalled(Context context) {
}
return false;
}


public static OkHttpClient HttpClientBuilder() {
OkHttpClient httpClient;
if (Build.VERSION.SDK_INT == 24) {
ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
.tlsVersions(TlsVersion.TLS_1_2)
.cipherSuites(CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256)
.build();

httpClient = new OkHttpClient.Builder()
.connectTimeout(5000, TimeUnit.MILLISECONDS)
.readTimeout(30000, TimeUnit.MILLISECONDS)
.writeTimeout(30000, TimeUnit.MILLISECONDS)
.connectionSpecs(Collections.singletonList(spec))
.build();
} else {
httpClient = new OkHttpClient.Builder()
.connectTimeout(5000, TimeUnit.MILLISECONDS)
.readTimeout(30000, TimeUnit.MILLISECONDS)
.writeTimeout(30000, TimeUnit.MILLISECONDS)
.build();
}
return httpClient;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ protected void onCreate(Bundle savedInstanceState) {

apiClient = new Retrofit.Builder()
.baseUrl(BuildConfig.HOST)
.client(Utils.HttpClientBuilder())
.build()
.create(OpenFoodAPIService.class);
}
Expand Down Expand Up @@ -173,6 +174,7 @@ public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(context, context.getString(R.string.errorWeb), Toast.LENGTH_LONG).show();
lt.error();
Utils.hideKeyboard(context);
t.printStackTrace();
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
Expand All @@ -41,8 +42,6 @@
import com.mikepenz.materialdrawer.model.SectionDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IProfile;
import android.support.v7.app.AlertDialog;
import android.widget.Toast;

import butterknife.BindView;
import openfoodfacts.github.scrachx.openfood.BuildConfig;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
package openfoodfacts.github.scrachx.openfood.network;

import android.os.Build;

import java.util.Collections;
import java.util.concurrent.TimeUnit;

import okhttp3.CipherSuite;
import okhttp3.ConnectionSpec;
import okhttp3.OkHttpClient;
import okhttp3.TlsVersion;
import okhttp3.logging.HttpLoggingInterceptor;

public interface APIUtils {

String GET_API = "https://world.openfoodfacts.org";
Expand All @@ -9,4 +20,36 @@ public interface APIUtils {
String POST_LOGIN = "off";

String POST_PASSWORD = "off";

HttpLoggingInterceptor logging = new HttpLoggingInterceptor()
.setLevel(HttpLoggingInterceptor.Level.BODY);

default OkHttpClient HttpClientBuilder() {
OkHttpClient httpClient;
if (Build.VERSION.SDK_INT == 24) {
ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
.tlsVersions(TlsVersion.TLS_1_2)
.cipherSuites(CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256)

.build();

httpClient = new OkHttpClient.Builder()
.connectTimeout(5000, TimeUnit.MILLISECONDS)
.readTimeout(30000, TimeUnit.MILLISECONDS)
.writeTimeout(30000, TimeUnit.MILLISECONDS)
.connectionSpecs(Collections.singletonList(spec))
.addInterceptor(logging)
.build();
} else {
httpClient = new OkHttpClient.Builder()
.connectTimeout(5000, TimeUnit.MILLISECONDS)
.readTimeout(30000, TimeUnit.MILLISECONDS)
.writeTimeout(30000, TimeUnit.MILLISECONDS)
.addInterceptor(logging)
.build();
}
return httpClient;

}
}

Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
package openfoodfacts.github.scrachx.openfood.network;

import com.fasterxml.jackson.databind.JsonNode;

import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.logging.HttpLoggingInterceptor;
import openfoodfacts.github.scrachx.openfood.models.Product;
import openfoodfacts.github.scrachx.openfood.models.ProductImage;
import openfoodfacts.github.scrachx.openfood.models.ProductImageField;
import openfoodfacts.github.scrachx.openfood.models.Search;
import openfoodfacts.github.scrachx.openfood.models.SendProduct;
import openfoodfacts.github.scrachx.openfood.models.State;
Expand All @@ -29,11 +21,10 @@
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.TestCase.assertNotNull;
import static net.javacrumbs.jsonunit.fluent.JsonFluentAssert.assertThatJson;
import static openfoodfacts.github.scrachx.openfood.network.OpenFoodAPIService.PRODUCT_API_COMMENT;
import static org.junit.Assert.assertTrue;

public class OpenFoodAPIServiceTest {
public class OpenFoodAPIServiceTest implements APIUtils {

private OpenFoodAPIService serviceRead;
private OpenFoodAPIService serviceWrite;
Expand All @@ -43,9 +34,7 @@ public void setUp() throws Exception {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor()
.setLevel(HttpLoggingInterceptor.Level.BODY);

OkHttpClient httpClient = new OkHttpClient.Builder()
.addInterceptor(logging)
.build();
OkHttpClient httpClient = HttpClientBuilder();

serviceRead = new Retrofit.Builder()
.baseUrl(APIUtils.GET_API)
Expand Down

0 comments on commit 9027fd2

Please sign in to comment.