Skip to content

Commit 11f6cf7

Browse files
committed
Pass scope only where needed
1 parent b465b60 commit 11f6cf7

File tree

4 files changed

+18
-17
lines changed

4 files changed

+18
-17
lines changed

lib/src/main/java/at/bitfire/cert4android/CustomCertManager.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ import javax.net.ssl.X509TrustManager
2424
@SuppressLint("CustomX509TrustManager")
2525
class CustomCertManager @JvmOverloads constructor(
2626
context: Context,
27-
scope: CoroutineScope,
27+
private val scope: CoroutineScope,
2828
val trustSystemCerts: Boolean = true,
2929
private val getUserDecision: suspend (X509Certificate) -> Boolean
3030
): X509TrustManager {
3131

32-
val certStore = CustomCertStore.getInstance(context, scope)
32+
val certStore = CustomCertStore.getInstance(context)
3333

3434

3535
@Throws(CertificateException::class)
@@ -47,7 +47,7 @@ class CustomCertManager @JvmOverloads constructor(
4747
*/
4848
@Throws(CertificateException::class)
4949
override fun checkServerTrusted(chain: Array<X509Certificate>, authType: String) {
50-
if (!certStore.isTrusted(chain, authType, trustSystemCerts, getUserDecision))
50+
if (!certStore.isTrusted(chain, authType, trustSystemCerts, scope, getUserDecision))
5151
throw CertificateException("Certificate chain not trusted")
5252
}
5353

@@ -71,7 +71,7 @@ class CustomCertManager @JvmOverloads constructor(
7171
// Allow users to explicitly accept certificates that have a bad hostname here
7272
(session.peerCertificates.firstOrNull() as? X509Certificate)?.let { cert ->
7373
// Check without trusting system certificates so that the user will be asked even for system-trusted certificates
74-
if (certStore.isTrusted(arrayOf(cert), "RSA", false, getUserDecision))
74+
if (certStore.isTrusted(arrayOf(cert), "RSA", false, scope, getUserDecision))
7575
return true
7676
}
7777

lib/src/main/java/at/bitfire/cert4android/CustomCertStore.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import java.util.logging.Level
2222

2323
class CustomCertStore internal constructor(
2424
private val context: Context,
25-
private val scope: CoroutineScope,
2625
private val userTimeout: Long = 60000L
2726
) {
2827

@@ -35,12 +34,12 @@ class CustomCertStore internal constructor(
3534
private var instance: CustomCertStore? = null
3635

3736
@Synchronized
38-
fun getInstance(context: Context, scope: CoroutineScope): CustomCertStore {
37+
fun getInstance(context: Context): CustomCertStore {
3938
instance?.let {
4039
return it
4140
}
4241

43-
val newInstance = CustomCertStore(context.applicationContext, scope)
42+
val newInstance = CustomCertStore(context.applicationContext)
4443
instance = newInstance
4544
return newInstance
4645
}
@@ -82,6 +81,7 @@ class CustomCertStore internal constructor(
8281
chain: Array<X509Certificate>,
8382
authType: String,
8483
trustSystemCerts: Boolean,
84+
scope: CoroutineScope,
8585
getUserDecision: suspend (X509Certificate) -> Boolean
8686
): Boolean {
8787
if (chain.isEmpty())
@@ -110,11 +110,11 @@ class CustomCertStore internal constructor(
110110
}
111111

112112
return runBlocking {
113-
val ui = UserDecisionRegistry.getInstance(context, scope)
113+
val ui = UserDecisionRegistry.getInstance(context)
114114

115115
try {
116116
withTimeout(userTimeout) {
117-
ui.check(cert, getUserDecision)
117+
ui.check(cert, scope, getUserDecision)
118118
}
119119
} catch (_: TimeoutCancellationException) {
120120
Cert4Android.log.log(Level.WARNING, "User timeout while waiting for certificate decision, rejecting")

lib/src/main/java/at/bitfire/cert4android/UserDecisionRegistry.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import kotlin.coroutines.Continuation
1010
import kotlin.coroutines.resume
1111

1212
class UserDecisionRegistry private constructor(
13-
private val context: Context,
14-
private val scope: CoroutineScope
13+
private val context: Context
1514
) {
1615

1716
companion object {
@@ -20,12 +19,12 @@ class UserDecisionRegistry private constructor(
2019
private var instance: UserDecisionRegistry? = null
2120

2221
@Synchronized
23-
fun getInstance(context: Context, scope: CoroutineScope): UserDecisionRegistry {
22+
fun getInstance(context: Context): UserDecisionRegistry {
2423
instance?.let {
2524
return it
2625
}
2726

28-
val newInstance = UserDecisionRegistry(context.applicationContext, scope)
27+
val newInstance = UserDecisionRegistry(context.applicationContext)
2928
instance = newInstance
3029
return newInstance
3130
}
@@ -45,6 +44,7 @@ class UserDecisionRegistry private constructor(
4544
*/
4645
suspend fun check(
4746
cert: X509Certificate,
47+
scope: CoroutineScope,
4848
getUserDecision: suspend (X509Certificate) -> Boolean
4949
): Boolean = suspendCancellableCoroutine { cont ->
5050
cont.invokeOnCancellation {
@@ -67,16 +67,17 @@ class UserDecisionRegistry private constructor(
6767
}
6868
}
6969

70-
if (requestDecision)
70+
if (requestDecision) {
7171
scope.launch {
72-
val userDecision = getUserDecision(cert)
72+
val userDecision = getUserDecision(cert) // Suspends until user decision is made
7373
onUserDecision(cert, userDecision)
7474
}
75+
}
7576
}
7677

7778
fun onUserDecision(cert: X509Certificate, trusted: Boolean) {
7879
// save decision
79-
val customCertStore = CustomCertStore.getInstance(context, scope)
80+
val customCertStore = CustomCertStore.getInstance(context)
8081
if (trusted)
8182
customCertStore.setTrustedByUser(cert)
8283
else

sample-app/src/main/java/at/bitfire/cert4android/demo/MainActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class MainActivity : ComponentActivity() {
194194
}
195195

196196
fun reset() = viewModelScope.launch(Dispatchers.IO) {
197-
CustomCertStore.getInstance(getApplication(), viewModelScope).clearUserDecisions()
197+
CustomCertStore.getInstance(getApplication()).clearUserDecisions()
198198
}
199199

200200
fun testAccess(url: String, trustSystemCerts: Boolean = true) =

0 commit comments

Comments
 (0)