feat(core): implement certificate pinning for lobste.rs

This commit is contained in:
Harsh Shandilya 2023-04-18 13:35:35 +05:30
parent 4734ab4a22
commit ea0dd4cb78
No known key found for this signature in database

View file

@ -21,6 +21,7 @@ import dev.msfjarvis.claw.core.network.UserAgentInterceptor
import java.net.Socket import java.net.Socket
import javax.net.SocketFactory import javax.net.SocketFactory
import okhttp3.Cache import okhttp3.Cache
import okhttp3.CertificatePinner
import okhttp3.Interceptor import okhttp3.Interceptor
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor import okhttp3.logging.HttpLoggingInterceptor
@ -37,6 +38,15 @@ interface OkHttpModule {
private const val CACHE_SIZE_MB = 10L * 1024 * 1024 private const val CACHE_SIZE_MB = 10L * 1024 * 1024
private const val THREAD_STATS_TAG = 0x000090000 private const val THREAD_STATS_TAG = 0x000090000
@Provides
fun provideCertificatePinner(): CertificatePinner {
return CertificatePinner.Builder()
.add("lobste.rs", "sha256/Bla1TIdpGeHXQS0/CIrA5hhFhOTZd94IIJRS3G3AcIo=")
.add("lobste.rs", "sha256/jQJTbIh0grw0/1TkHSumWb+Fs0Ggogr621gT3PvPKG0=")
.add("lobste.rs", "sha256/C5+lpZ7tcVwmwQIMcRtPbsQtWLABXhQzejna0wHFr8M=")
.build()
}
@Provides @Provides
fun provideCache(@ForScope(ApplicationScope::class) context: Context): Cache { fun provideCache(@ForScope(ApplicationScope::class) context: Context): Cache {
return Cache(context.cacheDir, CACHE_SIZE_MB) return Cache(context.cacheDir, CACHE_SIZE_MB)
@ -57,12 +67,14 @@ interface OkHttpModule {
cache: Cache, cache: Cache,
socketFactory: SocketFactory, socketFactory: SocketFactory,
interceptors: Set<@JvmSuppressWildcards Interceptor>, interceptors: Set<@JvmSuppressWildcards Interceptor>,
certificatePinner: CertificatePinner,
): OkHttpClient { ): OkHttpClient {
return OkHttpClient.Builder() return OkHttpClient.Builder()
.apply { .apply {
cache(cache) cache(cache)
interceptors.forEach(::addNetworkInterceptor) interceptors.forEach(::addNetworkInterceptor)
socketFactory(socketFactory) socketFactory(socketFactory)
certificatePinner(certificatePinner)
} }
.build() .build()
} }