I’m using cloudflare WAF for mutual tls in react native. Have implemented the required delegate authentication method for urlsession. The setup works fine initially. I get ServerTrust challenge and ClientCertificate challenge in succession and it works completely fine.
But randomly sometimes, after a restart or app resume. The request fails. In the logs I can only see Server Trust challenge and NOT client certificate. Is the tls session timing out in some cases?
@objc func handleSessionChallenge(didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
guard challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodClientCertificate
else {
completionHandler(.performDefaultHandling, nil)
return
}
guard let credential = Credentials.urlCredential(for: Bundle.main.userCertificateForWebsite) else {
completionHandler(.performDefaultHandling, nil)
return
}
challenge.sender?.use(credential, for: challenge)
completionHandler(.useCredential, credential)
}
@implementation RCTHTTPRequestHandler(Custom)
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
{
[[CustomSession shared] handleSessionChallengeWithDidReceive:challenge completionHandler:completionHandler];
}
@end
Have tried default NSURLSessionConfiguration using RCTSetCustomNSURLSessionConfigurationProvider, also tried using challenge.protectionSpace.serverTrust for URLCredential for NSURLAuthenticationMethodServerTrust
drz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.