diff --git a/app/src/debug/java/org/mifos/mobile/api/SelfServiceInterceptor.kt b/app/src/debug/java/org/mifos/mobile/api/SelfServiceInterceptor.kt index 1164e0d18..739003e09 100644 --- a/app/src/debug/java/org/mifos/mobile/api/SelfServiceInterceptor.kt +++ b/app/src/debug/java/org/mifos/mobile/api/SelfServiceInterceptor.kt @@ -7,22 +7,23 @@ package org.mifos.mobile.api import android.text.TextUtils import okhttp3.Interceptor import okhttp3.Response +import org.mifos.mobile.api.local.PreferencesHelper import java.io.IOException /** * @author Vishwajeet * @since 21/06/16 */ -class SelfServiceInterceptor(private val tenant: String?, private val authToken: String?) : +class SelfServiceInterceptor(private val preferencesHelper: PreferencesHelper) : Interceptor { @Throws(IOException::class) override fun intercept(chain: Interceptor.Chain): Response { val chainRequest = chain.request() val builder = chainRequest.newBuilder() - .header(HEADER_TENANT, tenant) + .header(HEADER_TENANT, preferencesHelper.tenant) .header(CONTENT_TYPE, "application/json") - if (!TextUtils.isEmpty(authToken)) { - builder.header(HEADER_AUTH, authToken) + if (!TextUtils.isEmpty(preferencesHelper.token)) { + builder.header(HEADER_AUTH, preferencesHelper.token) } val request = builder.build() return chain.proceed(request) diff --git a/app/src/main/java/org/mifos/mobile/api/SelfServiceOkHttpClient.kt b/app/src/main/java/org/mifos/mobile/api/SelfServiceOkHttpClient.kt index 9c69ef61d..bae9ee2ca 100644 --- a/app/src/main/java/org/mifos/mobile/api/SelfServiceOkHttpClient.kt +++ b/app/src/main/java/org/mifos/mobile/api/SelfServiceOkHttpClient.kt @@ -2,13 +2,62 @@ package org.mifos.mobile.api import okhttp3.OkHttpClient import okhttp3.logging.HttpLoggingInterceptor +import org.mifos.mobile.api.local.PreferencesHelper +import java.security.SecureRandom +import java.security.cert.CertificateException +import java.security.cert.X509Certificate import java.util.concurrent.TimeUnit +import javax.net.ssl.SSLContext +import javax.net.ssl.TrustManager +import javax.net.ssl.X509TrustManager -class SelfServiceOkHttpClient(private val tenant: String?, private val authToken: String?) { +class SelfServiceOkHttpClient(private val preferences: PreferencesHelper) { // Create a trust manager that does not validate certificate chains val mifosOkHttpClient: OkHttpClient + //Interceptor :> Full Body Logger and ApiRequest Header get() { val builder = OkHttpClient.Builder() + try { + // Create a trust manager that does not validate certificate chains + val trustAllCerts = arrayOf( + object : X509TrustManager { + @Throws(CertificateException::class) + override fun checkClientTrusted( + chain: Array, + authType: String + ) { + } + + @Throws(CertificateException::class) + override fun checkServerTrusted( + chain: Array, + authType: String + ) { + } + + override fun getAcceptedIssuers(): Array { + return emptyArray() + } + } + ) + + // Install the all-trusting trust manager + val sslContext = SSLContext.getInstance("SSL") + sslContext.init(null, trustAllCerts, SecureRandom()) + // Create an ssl socket factory with our all-trusting manager + val sslSocketFactory = sslContext.socketFactory + + //Enable Full Body Logging + val logger = HttpLoggingInterceptor() + logger.level = HttpLoggingInterceptor.Level.BODY + + //Set SSL certificate to OkHttpClient Builder +// builder.sslSocketFactory(sslSocketFactory) + builder.sslSocketFactory(sslSocketFactory, trustAllCerts[0] as X509TrustManager) + builder.hostnameVerifier { hostname, session -> true } + } catch (e: Exception) { + throw RuntimeException(e) + } // Enable Full Body Logging val logger = HttpLoggingInterceptor() @@ -20,7 +69,7 @@ class SelfServiceOkHttpClient(private val tenant: String?, private val authToken // Interceptor :> Full Body Logger and ApiRequest Header builder.addInterceptor(logger) - builder.addInterceptor(SelfServiceInterceptor(tenant, authToken)) + builder.addInterceptor(SelfServiceInterceptor(preferences)) return builder.build() } } diff --git a/app/src/main/java/org/mifos/mobile/injection/module/NetworkModule.kt b/app/src/main/java/org/mifos/mobile/injection/module/NetworkModule.kt index ee79336e9..49d569ef7 100644 --- a/app/src/main/java/org/mifos/mobile/injection/module/NetworkModule.kt +++ b/app/src/main/java/org/mifos/mobile/injection/module/NetworkModule.kt @@ -29,12 +29,7 @@ class NetworkModule { .baseUrl(BaseURL().getUrl(preferencesHelper.baseUrl!!)) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) - .client( - SelfServiceOkHttpClient( - preferencesHelper.tenant, - preferencesHelper.token - ).mifosOkHttpClient - ) + .client(SelfServiceOkHttpClient(preferencesHelper).mifosOkHttpClient) .build() } diff --git a/app/src/main/java/org/mifos/mobile/repositories/ClientRepositoryImp.kt b/app/src/main/java/org/mifos/mobile/repositories/ClientRepositoryImp.kt index 71ce877be..3604f4be0 100644 --- a/app/src/main/java/org/mifos/mobile/repositories/ClientRepositoryImp.kt +++ b/app/src/main/java/org/mifos/mobile/repositories/ClientRepositoryImp.kt @@ -50,8 +50,7 @@ class ClientRepositoryImp @Inject constructor( override fun reInitializeService() { retrofit.newBuilder().client( SelfServiceOkHttpClient( - preferencesHelper.tenant, - preferencesHelper.token + preferencesHelper ).mifosOkHttpClient ) }