Hi guys! I’m working on setting up a certificate p...
# ktor
v
Hi guys! I’m working on setting up a certificate pinning in my client app and struggle with catching a pinning error. I want to show a full-screen blocking banner if pinning error occurs. Error handling is not a part of
CertificatePinner
api, but also pinning error doesn’t trigger
HttpResponseValidator
. How can I configure
HttpClient
to catch this error?
a
Do you mean the
CertificatePinner
for the Darwin engine? If so, have you tried catching the
TlsPeerUnverifiedException
introduced in Ktor 3.1.0?
v
Yes, I have Darwin and OkHttp implementations, but I’m struggling with finding a place where to catch the error, how to configure catching.
That’s what I have for Darwin, for example:
Copy code
if (engineConfig is DarwinClientEngineConfig) {
        val builder = CertificatePinner.Builder()
        listOf("host" to "pinSHA").forEach { (host, pinSHA) ->
            builder.add(host, pinSHA)
        }
        engineConfig.handleChallenge(builder.build())
    }
for OkHttp is pretty similar:
Copy code
if (engineConfig is OkHttpConfig) {
        engineConfig.config {
            certificatePinner(
                CertificatePinner.Builder().apply {
                    listOf("host" to "pinSHA").forEach { (host, pinSHA) ->
                        add(host, pinSHA)
                    }

                }.build()
            )
        }
    }
a
Have you tried catching the exception in the place where you initiate the request?
v
I wanted it to be generic, so if any request fails with this error, I want to handle it in generic way (show blocking screen). Similar to how we can manage http errors with
HttpResponseValidator
https://ktor.io/docs/client-response-validation.html#non-2xx
I’m probably looking for something not existing yet 😞
a
Unfortunately, you have to catch the exceptions explicitly for each platform.
👌 1