Skip to content

Instantly share code, notes, and snippets.

@kalyaganov
Created May 25, 2021 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kalyaganov/25708e03de6a0f52afd22b76bb6d9acf to your computer and use it in GitHub Desktop.
Save kalyaganov/25708e03de6a0f52afd22b76bb6d9acf to your computer and use it in GitHub Desktop.
Ktor self signed certs
let skipSessionDelegate = SkipSSLSessionDelegate()
SDK(
// ....
challengeHandler: { (session, task, authenticationChallenge, completionHandler) in
skipSessionDelegate.urlSession?(session, didReceive: authenticationChallenge) {
(authChallengeDisposition, urlCredentials) in
completionHandler(KotlinLong(integerLiteral: authChallengeDisposition.rawValue), urlCredentials)
}
}
// ....
)
private fun createHttpEngine(challengeHandler: ChallengeHandler? = null): HttpClientEngine {
return Ios.create {
challengeHandler?.let { handleChallenge(it) }
}
}
// create SDK
import Foundation
open class SkipSSLSessionDelegate: NSObject, URLSessionDelegate {
public func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> ()) {
NSLog("URLAuthenticationChallenge auth method %@", challenge.protectionSpace.authenticationMethod)
if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
guard let serverTrust = challenge.protectionSpace.serverTrust else {
completionHandler(.cancelAuthenticationChallenge, nil)
return
}
completionHandler(.useCredential, URLCredential(trust: serverTrust))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment