Last active
September 13, 2018 02:22
-
-
Save 0xced/9e16bebf82b37571fb6b3a24044523a3 to your computer and use it in GitHub Desktop.
Test for NSURLAuthenticationMethodClientCertificate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface Delegate : NSObject <NSURLConnectionDelegate, NSURLSessionDataDelegate> | |
@end | |
@implementation Delegate | |
#pragma mark - NSURLConnectionDelegate | |
- (void) connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge; | |
{ | |
NSLog(@"connection:%@", connection); | |
NSLog(@"willSendRequestForAuthenticationChallenge:%@ %@", challenge, challenge.protectionSpace); | |
[challenge.sender performDefaultHandlingForAuthenticationChallenge:challenge]; | |
} | |
- (void) connectionDidFinishLoading:(NSURLConnection *)connection | |
{ | |
NSLog(@"connectionDidFinishLoading:%@", connection); | |
exit(0); | |
} | |
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error | |
{ | |
NSLog(@"connection:%@", connection); | |
NSLog(@"didFailWithError:%@", error); | |
exit(0); | |
} | |
#pragma mark - NSURLSessionTaskDelegate | |
- (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * __nullable credential))completionHandler | |
{ | |
NSLog(@"URLSession:%@", session); | |
NSLog(@"task:%@", task); | |
NSLog(@"didReceiveChallenge:%@ %@", challenge, challenge.protectionSpace); | |
completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil); | |
} | |
@end | |
void AuthenticateWithNSURLSession(NSURLRequest *request, dispatch_block_t completionHandler) | |
{ | |
[[[NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration ephemeralSessionConfiguration] delegate:[Delegate new] delegateQueue:nil] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { | |
NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ?: error); | |
dispatch_async(dispatch_get_main_queue(), completionHandler); | |
}] resume]; | |
} | |
void AuthenticateWithNSURLConnection(NSURLRequest *request) | |
{ | |
#pragma clang diagnostic push | |
#pragma clang diagnostic ignored "-Wdeprecated-declarations" | |
(void)[[NSURLConnection alloc] initWithRequest:request delegate:[Delegate new] startImmediately:YES]; | |
#pragma clang diagnostic pop | |
} | |
int main(int argc, const char * argv[]) | |
{ | |
NSURL *url = [NSURL URLWithString:@"https://auth.startssl.com"]; | |
NSURLRequest *request = [NSURLRequest requestWithURL:url]; | |
AuthenticateWithNSURLSession(request, ^{ | |
AuthenticateWithNSURLConnection(request); | |
}); | |
[[NSRunLoop mainRunLoop] run]; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was expecting to get an authentication challenge with a
NSURLAuthenticationMethodClientCertificate
method but only gotNSURLAuthenticationMethodServerTrust
. Going to https://auth.startssl.com in Safari or Chrome asks for a client certificate.Output as of OS X 10.11.4: