Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose authentication challenge callback to support iOS WKWebView #12

Merged
merged 5 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/ios/ClientCertificate.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
- (void)registerAuthenticationCertificate:(CDVInvokedUrlCommand*)command;
- (void)validateSslChain:(CDVInvokedUrlCommand*)command;

+ (void) didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler withOptionsNullable:(NSObject *) options;

+ (void)registerCertificateFromPath:(NSString*)path withPassword:(NSString*)password;

@end
20 changes: 18 additions & 2 deletions src/ios/ClientCertificate.m
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,23 @@ - (BOOL)customHTTPProtocol:(CustomHTTPProtocol *)protocol canAuthenticateAgainst

- (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
// support cordova-ios pre-6.x with UIWebView
[ClientCertificate didReceiveAuthenticationChallenge:challenge completionHandler:^(NSURLSessionAuthChallengeDisposition _, NSURLCredential * credential){
[protocol resolveAuthenticationChallenge:challenge withCredential:credential];
} withOptionsNullable:nil];
}

- (void) didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
{
// works with cordova-ios & WKWebView with update proposed in:
// - https://github.com/apache/cordova-ios/pull/1212
// - https://github.com/brodybits/cordova-ios/tree/auth-challenge-callback-support
[ClientCertificate didReceiveAuthenticationChallenge:challenge completionHandler:completionHandler withOptionsNullable:nil];
}

+ (void) didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler withOptionsNullable:(NSObject *) _optionsIgnored
{
// now exposed as a static method usable by @brodybits/cordova-plugin-ios-xhr
if([challenge previousFailureCount] == 0) {

NSURLCredential *credential = nil;
Expand Down Expand Up @@ -160,8 +177,7 @@ - (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol didReceiveAuthenticati

}

[protocol resolveAuthenticationChallenge:challenge withCredential:credential];

completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
}
}

Expand Down