Skip to content

Commit

Permalink
fix: login issue
Browse files Browse the repository at this point in the history
  • Loading branch information
PratyushSingh07 authored and therajanmaurya committed Oct 7, 2023
1 parent a2971e4 commit 7267667
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
53 changes: 51 additions & 2 deletions app/src/main/java/org/mifos/mobile/api/SelfServiceOkHttpClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<TrustManager>(
object : X509TrustManager {
@Throws(CertificateException::class)
override fun checkClientTrusted(
chain: Array<X509Certificate>,
authType: String
) {
}

@Throws(CertificateException::class)
override fun checkServerTrusted(
chain: Array<X509Certificate>,
authType: String
) {
}

override fun getAcceptedIssuers(): Array<X509Certificate> {
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()
Expand All @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ class ClientRepositoryImp @Inject constructor(
override fun reInitializeService() {
retrofit.newBuilder().client(
SelfServiceOkHttpClient(
preferencesHelper.tenant,
preferencesHelper.token
preferencesHelper
).mifosOkHttpClient
)
}
Expand Down

0 comments on commit 7267667

Please sign in to comment.