Skip to content

Commit bdf99d2

Browse files
committed
feat(plugins): Add plugin support for auth challenge responses
Closes apacheGH-1212.
1 parent 9beca6d commit bdf99d2

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed

CordovaLib/Classes/Private/Plugins/CDVWebViewEngine/CDVWebViewEngine.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,21 @@ - (void) webView: (WKWebView *) webView decidePolicyForNavigationAction: (WKNavi
578578
return decisionHandler(NO);
579579
}
580580

581+
- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
582+
{
583+
CDVViewController* vc = (CDVViewController*)self.viewController;
584+
585+
for (CDVPlugin *plugin in vc.enumerablePlugins) {
586+
if ([plugin respondsToSelector:@selector(didReceiveAuthenticationChallenge:completionHandler:)]) {
587+
CDVPlugin <CDVPluginAuthenticationHandler> *challengePlugin = (CDVPlugin <CDVPluginAuthenticationHandler> *)plugin;
588+
[challengePlugin didReceiveAuthenticationChallenge:challenge completionHandler:completionHandler];
589+
return;
590+
}
591+
}
592+
593+
completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
594+
}
595+
581596
#pragma mark - Plugin interface
582597

583598
- (void)allowsBackForwardNavigationGestures:(CDVInvokedUrlCommand*)command;

CordovaLib/Classes/Public/CDVPlugin.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ - (void)handleOpenURL:(NSNotification*)notification
146146
/*
147147
NOTE: calls into JavaScript must not call or trigger any blocking UI, like alerts
148148
*/
149-
- (void)handleOpenURLWithApplicationSourceAndAnnotation: (NSNotification*)notification
149+
- (void)handleOpenURLWithApplicationSourceAndAnnotation:(NSNotification*)notification
150150
{
151151

152152
// override to handle urls sent to your app

CordovaLib/CordovaLib.docc/CordovaLib.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ For more information about Apache Cordova, visit [https://cordova.apache.org](ht
3737

3838
- ``CDVPlugin``
3939
- ``CDVPluginSchemeHandler``
40+
- ``CDVPluginAuthenticationHandler``
4041

4142
### Plugin communication
4243
- ``CDVPluginResult``

CordovaLib/CordovaLib.docc/upgrading-8.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,9 @@ The following headers are deprecated due to adding global category extensions to
259259
* The ``CDVPluginHandleOpenURLWithAppSourceAndAnnotationNotification`` notification is now deprecated.
260260
The existing ``CDVPluginHandleOpenURLNotification`` notification now includes the source and annotation in its `userInfo` dictionary.
261261

262+
* ``CDVPluginAuthenticationHandler``
263+
* Newly added protocol for plugins wishing to handle server authentication requests.
264+
262265
* ``CDVPluginSchemeHandler``
263266
* Newly added protocol for plugins wishing to override WebKit scheme handling for web requests.
264267

CordovaLib/include/Cordova/CDVPlugin.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
@end
4141
#endif
4242

43+
NS_ASSUME_NONNULL_BEGIN
44+
4345
extern const NSNotificationName CDVPageDidLoadNotification;
4446
extern const NSNotificationName CDVPluginHandleOpenURLNotification;
4547
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;
128130
*/
129131
- (void)stopSchemeTask:(id <WKURLSchemeTask>)task;
130132
@end
133+
134+
135+
/**
136+
A protocol for Cordova plugins to intercept and respond to server
137+
authentication challenges through WebKit.
138+
139+
Your plugin should implement this protocol and the
140+
``didReceiveAuthenticationChallenge:completionHandler:`` if it wants to
141+
support responses to server-side authentication challenges, otherwise the
142+
default NSURLSession handling for authentication challenges will be used.
143+
*/
144+
@protocol CDVPluginAuthenticationHandler <NSObject>
145+
146+
/**
147+
Asks your plugin to respond to an authentication challenge.
148+
149+
- Parameters:
150+
- challenge: The authentication challenge.
151+
- completionHandler: A completion handler block to execute with the response.
152+
This handler has no return value and takes the following parameters:
153+
- disposition: The option to use to handle the challenge. For a list of
154+
options, see `NSURLSessionAuthChallengeDisposition`.
155+
- credential: The credential to use for authentication when the
156+
`disposition` parameter contains the value
157+
`NSURLSessionAuthChallengeUseCredential`. Specify `nil` to continue
158+
without a credential.
159+
*/
160+
- (void)didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler;
161+
162+
@end
163+
164+
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)