Skip to content

Commit e8507cf

Browse files
authored
feat(iOS): Allow plugins to hook into handling WebView URL authentication challenges (#8216)
1 parent 23303d4 commit e8507cf

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

cli/src/tasks/copy.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ export async function copy(config: Config, platformName: string, inline = false)
6565
}
6666

6767
let usesSSLPinning = false;
68-
if (allPlugins.filter((plugin) => plugin.id === '@ionic-enterprise/ssl-pinning').length > 0) {
68+
if (
69+
allPlugins.filter(
70+
(plugin) => plugin.id === '@ionic-enterprise/ssl-pinning' || plugin.id === '@capacitor/ssl-pinning',
71+
).length > 0
72+
) {
6973
usesSSLPinning = true;
7074
}
7175

ios/Capacitor/Capacitor/CAPPlugin.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
* Returning nil will defer to the default Capacitor policy
3939
*/
4040
- (NSNumber* _Nullable)shouldOverrideLoad:(WKNavigationAction* _Nonnull)navigationAction;
41+
/**
42+
* Allows plugins to hook into and respond to the WebView's URL authentication challenge.
43+
* Returning false will defer to the default response of [.rejectProtectionSpace](https://developer.apple.com/documentation/Foundation/URLSession/AuthChallengeDisposition/rejectProtectionSpace).
44+
*/
45+
- (BOOL)handleWKWebViewURLAuthenticationChallenge:(NSURLAuthenticationChallenge* _Nonnull)challenge completionHandler:(void (^_Nonnull)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler;
4146

4247
// Called after init if the plugin wants to do
4348
// some loading so the plugin author doesn't

ios/Capacitor/Capacitor/CAPPlugin.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,5 +171,10 @@ - (NSNumber*)shouldOverrideLoad:(WKNavigationAction*)navigationAction {
171171
return nil;
172172
}
173173

174+
- (BOOL)handleWKWebViewURLAuthenticationChallenge:(NSURLAuthenticationChallenge* _Nonnull)challenge completionHandler:(void (^_Nonnull)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler {
175+
return NO;
176+
}
177+
178+
174179
@end
175180

ios/Capacitor/Capacitor/WebViewDelegationHandler.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,26 @@ open class WebViewDelegationHandler: NSObject, WKNavigationDelegate, WKUIDelegat
161161
webView.reload()
162162
}
163163

164+
open func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping @MainActor (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
165+
guard let bridge = bridge else {
166+
completionHandler(.rejectProtectionSpace, nil)
167+
return
168+
}
169+
170+
for pluginObject in bridge.plugins {
171+
let plugin = pluginObject.value
172+
let selector = NSSelectorFromString("handleWKWebViewURLAuthenticationChallenge:completionHandler:")
173+
if plugin.responds(to: selector) {
174+
if plugin.handleWKWebViewURLAuthenticationChallenge(challenge, completionHandler: completionHandler) {
175+
return
176+
}
177+
}
178+
}
179+
180+
completionHandler(.rejectProtectionSpace, nil)
181+
return
182+
}
183+
164184
// MARK: - WKScriptMessageHandler
165185

166186
open func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {

0 commit comments

Comments
 (0)