Skip to content

Commit bd43301

Browse files
committed
Added security policy.
1 parent 3728c81 commit bd43301

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

.DS_Store

0 Bytes
Binary file not shown.

AXWebViewController/AXWebViewController/AXWebViewController.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#import <NJKWebViewProgress/NJKWebViewProgressView.h>
4444
#if AX_WEB_VIEW_CONTROLLER_USING_WEBKIT
4545
#import <WebKit/WebKit.h>
46+
#import "AFSecurityPolicy.h"
4647
#endif
4748
#ifndef AX_REQUIRES_SUPER
4849
#if __has_attribute(objc_requires_super)
@@ -101,6 +102,8 @@ typedef NS_ENUM(NSInteger, AXWebViewControllerNavigationType) {
101102
- (void)webViewController:(AXWebViewController *)webViewController didFailLoadWithError:(NSError *)error;
102103
@end
103104
#if AX_WEB_VIEW_CONTROLLER_USING_WEBKIT
105+
typedef NSURLSessionAuthChallengeDisposition (^WKWebViewDidReceiveAuthenticationChallengeHandler)(WKWebView *webView, NSURLAuthenticationChallenge *challenge, NSURLCredential * _Nullable __autoreleasing * _Nullable credential);
106+
104107
@interface AXWebViewController : UIViewController <WKUIDelegate, WKNavigationDelegate>
105108
{
106109
@protected
@@ -238,4 +241,14 @@ typedef NS_ENUM(NSInteger, AXWebViewControllerNavigationType) {
238241
///
239242
@property(readonly, nonatomic) UILabel *descriptionLabel;
240243
@end
244+
245+
#if AX_WEB_VIEW_CONTROLLER_USING_WEBKIT
246+
@interface AXWebViewController (Security)
247+
/// Challenge handler for the credential.
248+
@property(copy, nonatomic, nullable) WKWebViewDidReceiveAuthenticationChallengeHandler challengeHandler;
249+
/// The security policy used by created session to evaluate server trust for secure connections.
250+
/// `AXWebViewController` uses the `defaultPolicy` unless otherwise specified.
251+
@property (strong, nonatomic, nullable) AFSecurityPolicy *securityPolicy;
252+
@end
253+
#endif
241254
NS_ASSUME_NONNULL_END

AXWebViewController/AXWebViewController/AXWebViewController.m

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ @interface AXWebViewController ()<NJKWebViewProgressDelegate>
4949
NSURL *_baseURL;
5050
#if AX_WEB_VIEW_CONTROLLER_USING_WEBKIT
5151
WKWebViewConfiguration *_configuration;
52+
53+
WKWebViewDidReceiveAuthenticationChallengeHandler _challengeHandler;
54+
AFSecurityPolicy *_securityPolicy;
5255
#endif
5356

5457
NSURLRequest *_request;
@@ -1096,6 +1099,32 @@ - (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavig
10961099
[self didFailLoadWithError:error];
10971100
}
10981101
- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *__nullable credential))completionHandler {
1102+
// !!!: Do add the security policy if using a custom credential.
1103+
NSURLSessionAuthChallengeDisposition disposition = NSURLSessionAuthChallengePerformDefaultHandling;
1104+
__block NSURLCredential *credential = nil;
1105+
1106+
if (self.challengeHandler) {
1107+
disposition = self.challengeHandler(webView, challenge, &credential);
1108+
} else {
1109+
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
1110+
if ([self.securityPolicy evaluateServerTrust:challenge.protectionSpace.serverTrust forDomain:challenge.protectionSpace.host]) {
1111+
credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
1112+
if (credential) {
1113+
disposition = NSURLSessionAuthChallengeUseCredential;
1114+
} else {
1115+
disposition = NSURLSessionAuthChallengePerformDefaultHandling;
1116+
}
1117+
} else {
1118+
disposition = NSURLSessionAuthChallengePerformDefaultHandling;
1119+
}
1120+
} else {
1121+
disposition = NSURLSessionAuthChallengePerformDefaultHandling;
1122+
}
1123+
}
1124+
1125+
if (completionHandler) {
1126+
completionHandler(disposition, credential);
1127+
}
10991128
completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
11001129
}
11011130
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_9_0
@@ -1523,6 +1552,24 @@ - (void)orientationChanged:(NSNotification *)note {
15231552
}
15241553
@end
15251554

1555+
@implementation AXWebViewController (Security)
1556+
- (WKWebViewDidReceiveAuthenticationChallengeHandler)challengeHandler {
1557+
return _challengeHandler;
1558+
}
1559+
1560+
- (AFSecurityPolicy *)securityPolicy {
1561+
return _securityPolicy;
1562+
}
1563+
1564+
- (void)setChallengeHandler:(WKWebViewDidReceiveAuthenticationChallengeHandler)challengeHandler {
1565+
_challengeHandler = [challengeHandler copy];
1566+
}
1567+
1568+
- (void)setSecurityPolicy:(AFSecurityPolicy *)securityPolicy {
1569+
_securityPolicy = securityPolicy;
1570+
}
1571+
@end
1572+
15261573
#if AX_WEB_VIEW_CONTROLLER_USING_WEBKIT
15271574
@implementation UIProgressView (WebKit)
15281575
+ (void)load {

0 commit comments

Comments
 (0)