From c194fc018920be37fad090cd80864a1beb5374d5 Mon Sep 17 00:00:00 2001 From: "Christopher J. Brody" Date: Mon, 20 Dec 2021 22:34:31 -0500 Subject: [PATCH] support auth challenge callback via plugin --- .../CDVWebViewEngine/CDVWebViewEngine.m | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m b/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m index 8e1094e7c..9b52a4f7d 100644 --- a/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m +++ b/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m @@ -584,6 +584,30 @@ - (void) webView: (WKWebView *) webView decidePolicyForNavigationAction: (WKNavi return decisionHandler(NO); } +- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler +{ + // --- + NSLog(@"*** AUTH CHALLENGE ****"); + + CDVViewController* vc = (CDVViewController*)self.viewController; + + // first responder wins: + for (NSString* pluginName in vc.pluginObjects) { + CDVPlugin* plugin = [vc.pluginObjects objectForKey:pluginName]; + SEL selector = NSSelectorFromString(@"didReceiveAuthenticationChallenge:completionHandler:"); + if ([plugin respondsToSelector:selector]) { + // --- + NSLog(@"found plugin to handle auth challenge, handing over ..."); + (((void (*)(id, SEL, id, void(^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *)))objc_msgSend)(plugin, selector, challenge, completionHandler)); + return; + } + } + + // no plugin found, fallback to default behavior + NSLog(@"did not find any plugin to handle auth challenge, fallback to default behavior"); + completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil); +} + #pragma mark - Plugin interface - (void)allowsBackForwardNavigationGestures:(CDVInvokedUrlCommand*)command;