Skip to content

Commit

Permalink
Use the right scope
Browse files Browse the repository at this point in the history
  • Loading branch information
sunkup committed Feb 20, 2025
1 parent f3d1cc0 commit 6bd6422
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 380 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package at.bitfire.cert4android

import android.annotation.SuppressLint
import android.content.Context
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.CoroutineScope
import java.security.cert.CertificateException
import java.security.cert.X509Certificate
import javax.net.ssl.SSLSession
Expand All @@ -24,11 +24,12 @@ import javax.net.ssl.X509TrustManager
@SuppressLint("CustomX509TrustManager")
class CustomCertManager @JvmOverloads constructor(
context: Context,
scope: CoroutineScope,
val trustSystemCerts: Boolean = true,
private val getUserDecision: suspend (X509Certificate) -> Boolean
): X509TrustManager {

val certStore = CustomCertStore.getInstance(context)
val certStore = CustomCertStore.getInstance(context, scope)


@Throws(CertificateException::class)
Expand Down
8 changes: 5 additions & 3 deletions lib/src/main/java/at/bitfire/cert4android/CustomCertStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package at.bitfire.cert4android
import android.annotation.SuppressLint
import android.content.Context
import androidx.annotation.VisibleForTesting
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout
Expand All @@ -21,6 +22,7 @@ import java.util.logging.Level

class CustomCertStore internal constructor(
private val context: Context,
private val scope: CoroutineScope,
private val userTimeout: Long = 60000L
) {

Expand All @@ -33,12 +35,12 @@ class CustomCertStore internal constructor(
private var instance: CustomCertStore? = null

@Synchronized
fun getInstance(context: Context): CustomCertStore {
fun getInstance(context: Context, scope: CoroutineScope): CustomCertStore {
instance?.let {
return it
}

val newInstance = CustomCertStore(context.applicationContext)
val newInstance = CustomCertStore(context.applicationContext, scope)
instance = newInstance
return newInstance
}
Expand Down Expand Up @@ -108,7 +110,7 @@ class CustomCertStore internal constructor(
}

return runBlocking {
val ui = UserDecisionRegistry.getInstance(context)
val ui = UserDecisionRegistry.getInstance(context, scope)

try {
withTimeout(userTimeout) {
Expand Down
Loading

0 comments on commit 6bd6422

Please sign in to comment.