Skip to content

Commit

Permalink
feat(plugins): Add plugin support for auth challenge responses
Browse files Browse the repository at this point in the history
  • Loading branch information
dpogue committed Dec 14, 2024
1 parent 9beca6d commit bdf99d2
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <CDVPluginAuthenticationHandler> *challengePlugin = (CDVPlugin <CDVPluginAuthenticationHandler> *)plugin;
[challengePlugin didReceiveAuthenticationChallenge:challenge completionHandler:completionHandler];
return;
}
}

completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
}

#pragma mark - Plugin interface

- (void)allowsBackForwardNavigationGestures:(CDVInvokedUrlCommand*)command;
Expand Down
2 changes: 1 addition & 1 deletion CordovaLib/Classes/Public/CDVPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions CordovaLib/CordovaLib.docc/CordovaLib.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ For more information about Apache Cordova, visit [https://cordova.apache.org](ht

- ``CDVPlugin``
- ``CDVPluginSchemeHandler``
- ``CDVPluginAuthenticationHandler``

### Plugin communication
- ``CDVPluginResult``
Expand Down
3 changes: 3 additions & 0 deletions CordovaLib/CordovaLib.docc/upgrading-8.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
34 changes: 34 additions & 0 deletions CordovaLib/include/Cordova/CDVPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down Expand Up @@ -128,3 +130,35 @@ extern const NSNotificationName CDVViewWillTransitionToSizeNotification;
*/
- (void)stopSchemeTask:(id <WKURLSchemeTask>)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 <NSObject>

/**
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

0 comments on commit bdf99d2

Please sign in to comment.