From 1018f19bdb00b335292c8379af05c74890ee64be Mon Sep 17 00:00:00 2001 From: Darryl Pogue Date: Fri, 13 Dec 2024 22:30:01 -0800 Subject: [PATCH] feat(plugins): Add plugin support for auth challenge responses Closes GH-1212. Co-Authored-By: Christopher J. Brody --- .../CDVWebViewEngine/CDVWebViewEngine.m | 15 ++++++++ CordovaLib/Classes/Public/CDVPlugin.m | 2 +- CordovaLib/CordovaLib.docc/CordovaLib.md | 1 + CordovaLib/CordovaLib.docc/upgrading-8.md | 3 ++ CordovaLib/include/Cordova/CDVPlugin.h | 34 +++++++++++++++++++ 5 files changed, 54 insertions(+), 1 deletion(-) diff --git a/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m b/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m index 88f2d9a9d..4b376ae3d 100644 --- a/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m +++ b/CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m @@ -578,6 +578,21 @@ - (void) webView: (WKWebView *) webView decidePolicyForNavigationAction: (WKNavi return decisionHandler(NO); } +- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler +{ + CDVViewController* vc = (CDVViewController*)self.viewController; + + for (CDVPlugin *plugin in vc.enumerablePlugins) { + if ([plugin respondsToSelector:@selector(didReceiveAuthenticationChallenge:completionHandler:)]) { + CDVPlugin *challengePlugin = (CDVPlugin *)plugin; + [challengePlugin didReceiveAuthenticationChallenge:challenge completionHandler:completionHandler]; + return; + } + } + + completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil); +} + #pragma mark - Plugin interface - (void)allowsBackForwardNavigationGestures:(CDVInvokedUrlCommand*)command; diff --git a/CordovaLib/Classes/Public/CDVPlugin.m b/CordovaLib/Classes/Public/CDVPlugin.m index a2da3b86d..f86ffc9b7 100644 --- a/CordovaLib/Classes/Public/CDVPlugin.m +++ b/CordovaLib/Classes/Public/CDVPlugin.m @@ -146,7 +146,7 @@ - (void)handleOpenURL:(NSNotification*)notification /* NOTE: calls into JavaScript must not call or trigger any blocking UI, like alerts */ -- (void)handleOpenURLWithApplicationSourceAndAnnotation: (NSNotification*)notification +- (void)handleOpenURLWithApplicationSourceAndAnnotation:(NSNotification*)notification { // override to handle urls sent to your app diff --git a/CordovaLib/CordovaLib.docc/CordovaLib.md b/CordovaLib/CordovaLib.docc/CordovaLib.md index f06ad8f2a..8ee6f753b 100644 --- a/CordovaLib/CordovaLib.docc/CordovaLib.md +++ b/CordovaLib/CordovaLib.docc/CordovaLib.md @@ -37,6 +37,7 @@ For more information about Apache Cordova, visit [https://cordova.apache.org](ht - ``CDVPlugin`` - ``CDVPluginSchemeHandler`` +- ``CDVPluginAuthenticationHandler`` ### Plugin communication - ``CDVPluginResult`` diff --git a/CordovaLib/CordovaLib.docc/upgrading-8.md b/CordovaLib/CordovaLib.docc/upgrading-8.md index 471a73295..67c8ca358 100644 --- a/CordovaLib/CordovaLib.docc/upgrading-8.md +++ b/CordovaLib/CordovaLib.docc/upgrading-8.md @@ -259,6 +259,9 @@ The following headers are deprecated due to adding global category extensions to * The ``CDVPluginHandleOpenURLWithAppSourceAndAnnotationNotification`` notification is now deprecated. The existing ``CDVPluginHandleOpenURLNotification`` notification now includes the source and annotation in its `userInfo` dictionary. +* ``CDVPluginAuthenticationHandler`` + * Newly added protocol for plugins wishing to handle server authentication requests. + * ``CDVPluginSchemeHandler`` * Newly added protocol for plugins wishing to override WebKit scheme handling for web requests. diff --git a/CordovaLib/include/Cordova/CDVPlugin.h b/CordovaLib/include/Cordova/CDVPlugin.h index 981675fc5..e423c6d31 100644 --- a/CordovaLib/include/Cordova/CDVPlugin.h +++ b/CordovaLib/include/Cordova/CDVPlugin.h @@ -40,6 +40,8 @@ @end #endif +NS_ASSUME_NONNULL_BEGIN + extern const NSNotificationName CDVPageDidLoadNotification; extern const NSNotificationName CDVPluginHandleOpenURLNotification; extern const NSNotificationName CDVPluginHandleOpenURLWithAppSourceAndAnnotationNotification CDV_DEPRECATED(8, "Find sourceApplication and annotations in the userInfo of the CDVPluginHandleOpenURLNotification notification."); @@ -128,3 +130,35 @@ extern const NSNotificationName CDVViewWillTransitionToSizeNotification; */ - (void)stopSchemeTask:(id )task; @end + + +/** + A protocol for Cordova plugins to intercept and respond to server + authentication challenges through WebKit. + + Your plugin should implement this protocol and the + ``didReceiveAuthenticationChallenge:completionHandler:`` if it wants to + support responses to server-side authentication challenges, otherwise the + default NSURLSession handling for authentication challenges will be used. + */ +@protocol CDVPluginAuthenticationHandler + +/** + Asks your plugin to respond to an authentication challenge. + + - Parameters: + - challenge: The authentication challenge. + - completionHandler: A completion handler block to execute with the response. + This handler has no return value and takes the following parameters: + - disposition: The option to use to handle the challenge. For a list of + options, see `NSURLSessionAuthChallengeDisposition`. + - credential: The credential to use for authentication when the + `disposition` parameter contains the value + `NSURLSessionAuthChallengeUseCredential`. Specify `nil` to continue + without a credential. + */ +- (void)didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler; + +@end + +NS_ASSUME_NONNULL_END