Skip to content

Commit

Permalink
Apply custom configuration statically
Browse files Browse the repository at this point in the history
  • Loading branch information
avazirna committed Sep 18, 2023
1 parent 279f859 commit 6a720db
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 34 deletions.
49 changes: 25 additions & 24 deletions app/src/org/commcare/network/CTInterceptorConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,40 @@ class CTInterceptorConfig {
private var interceptor: Interceptor? = null
private var previousRequestFailed = false
private var interceptorAttached = false
}

fun toggleCertificateTransparency(client: OkHttpClient.Builder) {
if (HiddenPreferences.isCertificateTransparencyEnabled()) {
if (!interceptorAttached) {
client.addNetworkInterceptor(getCTInterceptor())
interceptorAttached = true
@JvmStatic
fun toggleCertificateTransparency(client: OkHttpClient.Builder) {
if (HiddenPreferences.isCertificateTransparencyEnabled()) {
if (!interceptorAttached) {
client.addNetworkInterceptor(getCTInterceptor())
interceptorAttached = true
}
} else if (interceptorAttached) {
removeCTInterceptors(client)
interceptorAttached = false
}
} else if (interceptorAttached) {
removeCTInterceptors(client)
interceptorAttached = false
}
}

private fun getCTInterceptor(): Interceptor {
if (interceptor == null) {
interceptor = certificateTransparencyInterceptor {
logger = object : CTLogger {
override fun log(host: String, result: VerificationResult) {
if (result is VerificationResult.Failure && !previousRequestFailed) {
Logger.log(
LogTypes.TYPE_NETWORK,
"Certificate verification failed: $host -> $result")
private fun getCTInterceptor(): Interceptor {
if (interceptor == null) {
interceptor = certificateTransparencyInterceptor {
logger = object : CTLogger {
override fun log(host: String, result: VerificationResult) {
if (result is VerificationResult.Failure && !previousRequestFailed) {
Logger.log(
LogTypes.TYPE_NETWORK,
"Certificate verification failed: $host -> $result")
}
previousRequestFailed = result is VerificationResult.Failure
}
previousRequestFailed = result is VerificationResult.Failure
}
}
}
return interceptor!!
}
return interceptor!!
}

private fun removeCTInterceptors(client: OkHttpClient.Builder) {
client.networkInterceptors().removeAll { it === interceptor }
private fun removeCTInterceptors(client: OkHttpClient.Builder) {
client.networkInterceptors().removeAll { it === interceptor }
}
}
}
2 changes: 1 addition & 1 deletion app/src/org/commcare/network/ISRGCertConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
public class ISRGCertConfig {

public void attachISRGRootCertificate(OkHttpClient.Builder okHttpBuilder) {
public static void attachISRGRootCertificate(OkHttpClient.Builder okHttpBuilder) {
String isgCert =
"-----BEGIN CERTIFICATE-----\n" +
"MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw\n" +
Expand Down
11 changes: 2 additions & 9 deletions app/src/org/commcare/network/OkHttpBuilderCustomConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,15 @@
* Certificate Transparency
*/
public class OkHttpBuilderCustomConfig implements HttpBuilderConfig {
private CTInterceptorConfig ctInterceptorConfig;
private ISRGCertConfig isrgCertConfig;

public OkHttpBuilderCustomConfig(){
ctInterceptorConfig = new CTInterceptorConfig();
isrgCertConfig = new ISRGCertConfig();
}

@Override
public OkHttpClient.Builder performCustomConfig(OkHttpClient.Builder okHttpBuilder) {
// Enable or Disable CT, depending on the current value of the preference
ctInterceptorConfig.toggleCertificateTransparency(okHttpBuilder);
CTInterceptorConfig.toggleCertificateTransparency(okHttpBuilder);

// Attach ISRG Root Certificate when running Android 7.1 and below
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.N_MR1) {
isrgCertConfig.attachISRGRootCertificate(okHttpBuilder);
ISRGCertConfig.attachISRGRootCertificate(okHttpBuilder);
}

return okHttpBuilder;
Expand Down

0 comments on commit 6a720db

Please sign in to comment.