From 769af56052f4678dfada2f707d24fdd5b5922fbf Mon Sep 17 00:00:00 2001 From: Alexander Dodatko Date: Mon, 30 Jul 2018 11:47:51 +0300 Subject: [PATCH 1/8] [refactor] removed a strange `YES &&` code pattern --- .../JAHPAuthenticatingHTTPProtocol.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/JiveAuthenticatingHTTPProtocol/JAHPAuthenticatingHTTPProtocol.m b/Source/JiveAuthenticatingHTTPProtocol/JAHPAuthenticatingHTTPProtocol.m index bae72b6..601f209 100644 --- a/Source/JiveAuthenticatingHTTPProtocol/JAHPAuthenticatingHTTPProtocol.m +++ b/Source/JiveAuthenticatingHTTPProtocol/JAHPAuthenticatingHTTPProtocol.m @@ -253,9 +253,9 @@ + (BOOL)canInitWithRequest:(NSURLRequest *)request // NSURLProtocol subclass. if (shouldAccept) { - shouldAccept = YES && [scheme isEqual:@"http"]; + shouldAccept = [scheme isEqual:@"http"]; if ( ! shouldAccept ) { - shouldAccept = YES && [scheme isEqual:@"https"]; + shouldAccept = [scheme isEqual:@"https"]; } if ( ! shouldAccept ) { From 445b351ee1542f69fc62c9fcde93ca6d1aa01dcb Mon Sep 17 00:00:00 2001 From: Alexander Dodatko Date: Mon, 30 Jul 2018 11:53:01 +0300 Subject: [PATCH 2/8] [refactor] used _cmd where appropriate in JAHPQNSURLSessionDemux.m --- .../JAHPQNSURLSessionDemux.m | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/Source/JiveAuthenticatingHTTPProtocol/JAHPQNSURLSessionDemux.m b/Source/JiveAuthenticatingHTTPProtocol/JAHPQNSURLSessionDemux.m index ce49371..3d6f389 100644 --- a/Source/JiveAuthenticatingHTTPProtocol/JAHPQNSURLSessionDemux.m +++ b/Source/JiveAuthenticatingHTTPProtocol/JAHPQNSURLSessionDemux.m @@ -73,9 +73,9 @@ @implementation JAHPQNSURLSessionDemuxTaskInfo - (instancetype)initWithTask:(NSURLSessionDataTask *)task delegate:(id)delegate modes:(NSArray *)modes { - assert(task != nil); - assert(delegate != nil); - assert(modes != nil); + NSParameterAssert(task != nil); + NSParameterAssert(delegate != nil); + NSParameterAssert(modes != nil); self = [super init]; if (self != nil) { @@ -89,14 +89,14 @@ - (instancetype)initWithTask:(NSURLSessionDataTask *)task delegate:(id Date: Mon, 30 Jul 2018 11:54:08 +0300 Subject: [PATCH 3/8] [refactor] replaced assert() ==> NSParameterAssert() for obj-c functions --- .../JAHPAuthenticatingHTTPProtocol.m | 114 +++++++++--------- 1 file changed, 57 insertions(+), 57 deletions(-) diff --git a/Source/JiveAuthenticatingHTTPProtocol/JAHPAuthenticatingHTTPProtocol.m b/Source/JiveAuthenticatingHTTPProtocol/JAHPAuthenticatingHTTPProtocol.m index 601f209..729a896 100644 --- a/Source/JiveAuthenticatingHTTPProtocol/JAHPAuthenticatingHTTPProtocol.m +++ b/Source/JiveAuthenticatingHTTPProtocol/JAHPAuthenticatingHTTPProtocol.m @@ -272,7 +272,7 @@ + (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request { NSURLRequest * result; - assert(request != nil); + NSParameterAssert(request != nil); // can be called on any thread // Canonicalising a request is quite complex, so all the heavy lifting has @@ -287,9 +287,9 @@ + (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request - (id)initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)cachedResponse client:(id )client { - assert(request != nil); + NSParameterAssert(request != nil); // cachedResponse may be nil - assert(client != nil); + NSParameterAssert(client != nil); // can be called on any thread NSMutableURLRequest *mutableRequest = [request mutableCopy]; @@ -327,9 +327,9 @@ - (void)dealloc { // can be called on any thread [[self class] authenticatingHTTPProtocol:self logWithFormat:@"dealloc"]; - assert(self->_task == nil); // we should have cleared it by now - assert(self->_pendingChallenge == nil); // we should have cancelled it by now - assert(self->_pendingChallengeCompletionHandler == nil); // we should have cancelled it by now + NSParameterAssert(self->_task == nil); // we should have cleared it by now + NSParameterAssert(self->_pendingChallenge == nil); // we should have cancelled it by now + NSParameterAssert(self->_pendingChallengeCompletionHandler == nil); // we should have cancelled it by now } - (void)startLoading @@ -341,8 +341,8 @@ - (void)startLoading // At this point we kick off the process of loading the URL via NSURLSession. // The thread that calls this method becomes the client thread. - assert(self.clientThread == nil); // you can't call -startLoading twice - assert(self.task == nil); + NSParameterAssert(self.clientThread == nil); // you can't call -startLoading twice + NSParameterAssert(self.task == nil); // Calculate our effective run loop modes. In some circumstances (yes I'm looking at // you UIWebView!) we can be called from a non-standard thread which then runs a @@ -353,7 +353,7 @@ - (void)startLoading // For debugging purposes the non-standard mode is "WebCoreSynchronousLoaderRunLoopMode" // but it's better not to hard-code that here. - assert(self.modes == nil); + NSParameterAssert(self.modes == nil); calculatedModes = [NSMutableArray array]; [calculatedModes addObject:NSDefaultRunLoopMode]; currentMode = [[NSRunLoop currentRunLoop] currentMode]; @@ -361,13 +361,13 @@ - (void)startLoading [calculatedModes addObject:currentMode]; } self.modes = calculatedModes; - assert([self.modes count] > 0); + NSParameterAssert([self.modes count] > 0); // Create new request that's a clone of the request we were initialised with, // except that it has our 'recursive request flag' property set on it. recursiveRequest = [[self request] mutableCopy]; - assert(recursiveRequest != nil); + NSParameterAssert(recursiveRequest != nil); [[self class] setProperty:@YES forKey:kJAHPRecursiveRequestFlagProperty inRequest:recursiveRequest]; @@ -385,7 +385,7 @@ - (void)startLoading // Once everything is ready to go, create a data task with the new request. self.task = [[[self class] sharedDemux] dataTaskWithRequest:recursiveRequest delegate:self modes:self.modes]; - assert(self.task != nil); + NSParameterAssert(self.task != nil); [self.task resume]; } @@ -396,7 +396,7 @@ - (void)stopLoading [[self class] authenticatingHTTPProtocol:self logWithFormat:@"stop (elapsed %.1f)", [NSDate timeIntervalSinceReferenceDate] - self.startTime]; - assert(self.clientThread != nil); // someone must have called -startLoading + NSParameterAssert(self.clientThread != nil); // someone must have called -startLoading // Check that we're being stopped on the same thread that we were started // on. Without this invariant things are going to go badly (for example, @@ -408,7 +408,7 @@ - (void)stopLoading // Rather, I rely on our client calling us on the right thread, which is what // the following assert is about. - assert([NSThread currentThread] == self.clientThread); + NSParameterAssert([NSThread currentThread] == self.clientThread); [self cancelPendingChallenge]; if (self.task != nil) { @@ -432,7 +432,7 @@ - (void)performOnThread:(NSThread *)thread modes:(NSArray *)modes block:(dispatc { // thread may be nil // modes may be nil - assert(block != nil); + NSParameterAssert(block != nil); if (thread == nil) { thread = [NSThread mainThread]; @@ -450,7 +450,7 @@ - (void)performOnThread:(NSThread *)thread modes:(NSArray *)modes block:(dispatc - (void)onThreadPerformBlock:(dispatch_block_t)block { - assert(block != nil); + NSParameterAssert(block != nil); block(); } @@ -475,9 +475,9 @@ - (void)onThreadPerformBlock:(dispatch_block_t)block - (void)didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(JAHPChallengeCompletionHandler)completionHandler { - assert(challenge != nil); - assert(completionHandler != nil); - assert([NSThread currentThread] == self.clientThread); + NSParameterAssert(challenge != nil); + NSParameterAssert(completionHandler != nil); + NSParameterAssert([NSThread currentThread] == self.clientThread); [[self class] authenticatingHTTPProtocol:self logWithFormat:@"challenge %@ received", [[challenge protectionSpace] authenticationMethod]]; @@ -498,9 +498,9 @@ - (void)didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challe - (void)mainThreadDidReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(JAHPChallengeCompletionHandler)completionHandler { - assert(challenge != nil); - assert(completionHandler != nil); - assert([NSThread isMainThread]); + NSParameterAssert(challenge != nil); + NSParameterAssert(completionHandler != nil); + NSParameterAssert([NSThread isMainThread]); if (self.pendingChallenge != nil) { @@ -512,7 +512,7 @@ - (void)mainThreadDidReceiveAuthenticationChallenge:(NSURLAuthenticationChalleng // namely, the client thread. [[self class] authenticatingHTTPProtocol:self logWithFormat:@"challenge %@ cancelled; other challenge pending", [[challenge protectionSpace] authenticationMethod]]; - assert(NO); + NSParameterAssert(NO); [self clientThreadCancelAuthenticationChallenge:challenge completionHandler:completionHandler]; } else { id strongDelegate; @@ -526,7 +526,7 @@ - (void)mainThreadDidReceiveAuthenticationChallenge:(NSURLAuthenticationChalleng if ( ! [strongDelegate respondsToSelector:@selector(authenticatingHTTPProtocol:canAuthenticateAgainstProtectionSpace:)] ) { [[self class] authenticatingHTTPProtocol:self logWithFormat:@"challenge %@ cancelled; no delegate method", [[challenge protectionSpace] authenticationMethod]]; - assert(NO); + NSParameterAssert(NO); [self clientThreadCancelAuthenticationChallenge:challenge completionHandler:completionHandler]; } else { @@ -556,9 +556,9 @@ - (void)mainThreadDidReceiveAuthenticationChallenge:(NSURLAuthenticationChalleng - (void)clientThreadCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(JAHPChallengeCompletionHandler)completionHandler { #pragma unused(challenge) - assert(challenge != nil); - assert(completionHandler != nil); - assert([NSThread isMainThread]); + NSParameterAssert(challenge != nil); + NSParameterAssert(completionHandler != nil); + NSParameterAssert([NSThread isMainThread]); [self performOnThread:self.clientThread modes:self.modes block:^{ completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil); @@ -574,7 +574,7 @@ - (void)clientThreadCancelAuthenticationChallenge:(NSURLAuthenticationChallenge - (void)cancelPendingChallenge { - assert([NSThread currentThread] == self.clientThread); + NSParameterAssert([NSThread currentThread] == self.clientThread); // Just pass the work off to the main thread. We do this so that all accesses // to pendingChallenge are done from the main thread, which avoids the need for @@ -613,7 +613,7 @@ - (void)cancelPendingChallenge [[self class] authenticatingHTTPProtocol:self logWithFormat:@"challenge %@ cancellation failed; no delegate method", [[challenge protectionSpace] authenticationMethod]]; // If we managed to send a challenge to the client but can't cancel it, that's bad. // There's nothing we can do at this point except log the problem. - assert(NO); + NSParameterAssert(NO); } } }]; @@ -622,8 +622,8 @@ - (void)cancelPendingChallenge - (void)resolvePendingAuthenticationChallengeWithCredential:(NSURLCredential *)credential { // credential may be nil - assert([NSThread isMainThread]); - assert(self.clientThread != nil); + NSParameterAssert([NSThread isMainThread]); + NSParameterAssert(self.clientThread != nil); JAHPChallengeCompletionHandler completionHandler; NSURLAuthenticationChallenge *challenge; @@ -650,8 +650,8 @@ - (void)resolvePendingAuthenticationChallengeWithCredential:(NSURLCredential *)c } - (void)cancelPendingAuthenticationChallenge { - assert([NSThread isMainThread]); - assert(self.clientThread != nil); + NSParameterAssert([NSThread isMainThread]); + NSParameterAssert(self.clientThread != nil); JAHPChallengeCompletionHandler completionHandler; NSURLAuthenticationChallenge *challenge; @@ -690,12 +690,12 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPer #pragma unused(session) #pragma unused(task) - assert(task == self.task); - assert(response != nil); - assert(newRequest != nil); + NSParameterAssert(task == self.task); + NSParameterAssert(response != nil); + NSParameterAssert(newRequest != nil); #pragma unused(completionHandler) - assert(completionHandler != nil); - assert([NSThread currentThread] == self.clientThread); + NSParameterAssert(completionHandler != nil); + NSParameterAssert([NSThread currentThread] == self.clientThread); [[self class] authenticatingHTTPProtocol:self logWithFormat:@"will redirect from %@ to %@", [response URL], [newRequest URL]]; @@ -707,7 +707,7 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPer // We also cancel our current connection because the client is going to start a new request for // us anyway. - assert([[self class] propertyForKey:kJAHPRecursiveRequestFlagProperty inRequest:newRequest] != nil); + NSParameterAssert([[self class] propertyForKey:kJAHPRecursiveRequestFlagProperty inRequest:newRequest] != nil); redirectRequest = [newRequest mutableCopy]; [[self class] removePropertyForKey:kJAHPRecursiveRequestFlagProperty inRequest:redirectRequest]; @@ -742,10 +742,10 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didRece #pragma unused(session) #pragma unused(task) - assert(task == self.task); - assert(challenge != nil); - assert(completionHandler != nil); - assert([NSThread currentThread] == self.clientThread); + NSParameterAssert(task == self.task); + NSParameterAssert(challenge != nil); + NSParameterAssert(completionHandler != nil); + NSParameterAssert([NSThread currentThread] == self.clientThread); // Ask our delegate whether it wants this challenge. We do this from this thread, not the main thread, // to avoid the overload of bouncing to the main thread for challenges that aren't going to be customised @@ -786,10 +786,10 @@ - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)data #pragma unused(session) #pragma unused(dataTask) - assert(dataTask == self.task); - assert(response != nil); - assert(completionHandler != nil); - assert([NSThread currentThread] == self.clientThread); + NSParameterAssert(dataTask == self.task); + NSParameterAssert(response != nil); + NSParameterAssert(completionHandler != nil); + NSParameterAssert([NSThread currentThread] == self.clientThread); // Pass the call on to our client. The only tricky thing is that we have to decide on a // cache storage policy, which is based on the actual request we issued, not the request @@ -799,7 +799,7 @@ - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)data cacheStoragePolicy = JAHPCacheStoragePolicyForRequestAndResponse(self.task.originalRequest, (NSHTTPURLResponse *) response); statusCode = [((NSHTTPURLResponse *) response) statusCode]; } else { - assert(NO); + NSParameterAssert(NO); cacheStoragePolicy = NSURLCacheStorageNotAllowed; statusCode = 42; } @@ -823,9 +823,9 @@ - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)data #pragma unused(session) #pragma unused(dataTask) - assert(dataTask == self.task); - assert(data != nil); - assert([NSThread currentThread] == self.clientThread); + NSParameterAssert(dataTask == self.task); + NSParameterAssert(data != nil); + NSParameterAssert([NSThread currentThread] == self.clientThread); // Just pass the call on to our client. @@ -846,10 +846,10 @@ - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)data #pragma unused(session) #pragma unused(dataTask) - assert(dataTask == self.task); - assert(proposedResponse != nil); - assert(completionHandler != nil); - assert([NSThread currentThread] == self.clientThread); + NSParameterAssert(dataTask == self.task); + NSParameterAssert(proposedResponse != nil); + NSParameterAssert(completionHandler != nil); + NSParameterAssert([NSThread currentThread] == self.clientThread); // We implement this delegate callback purely for the purposes of logging. @@ -863,8 +863,8 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didComp { #pragma unused(session) #pragma unused(task) - assert( (self.task == nil) || (task == self.task) ); // can be nil in the 'cancel from -stopLoading' case - assert([NSThread currentThread] == self.clientThread); + NSParameterAssert( (self.task == nil) || (task == self.task) ); // can be nil in the 'cancel from -stopLoading' case + NSParameterAssert([NSThread currentThread] == self.clientThread); // Just log and then, in most cases, pass the call on to our client. From e8763d2565a3e7ad4bbbf3614b47186f92c7eab6 Mon Sep 17 00:00:00 2001 From: Alexander Dodatko Date: Mon, 30 Jul 2018 12:17:32 +0300 Subject: [PATCH 4/8] [#6] added credentials memoization --- .../ViewController.m | 73 +++++++++++++++---- 1 file changed, 57 insertions(+), 16 deletions(-) diff --git a/JiveAuthenticatingHTTPProtocolDemo/JiveAuthenticatingHTTPProtocolDemo/ViewController.m b/JiveAuthenticatingHTTPProtocolDemo/JiveAuthenticatingHTTPProtocolDemo/ViewController.m index d64f5d3..f3e1818 100644 --- a/JiveAuthenticatingHTTPProtocolDemo/JiveAuthenticatingHTTPProtocolDemo/ViewController.m +++ b/JiveAuthenticatingHTTPProtocolDemo/JiveAuthenticatingHTTPProtocolDemo/ViewController.m @@ -16,6 +16,8 @@ @interface ViewController () Date: Mon, 30 Jul 2018 12:18:19 +0300 Subject: [PATCH 5/8] set url scheme to HTTPS to silence the ios security warnings --- .../JiveAuthenticatingHTTPProtocolDemo/ViewController.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/JiveAuthenticatingHTTPProtocolDemo/JiveAuthenticatingHTTPProtocolDemo/ViewController.m b/JiveAuthenticatingHTTPProtocolDemo/JiveAuthenticatingHTTPProtocolDemo/ViewController.m index f3e1818..b1805ec 100644 --- a/JiveAuthenticatingHTTPProtocolDemo/JiveAuthenticatingHTTPProtocolDemo/ViewController.m +++ b/JiveAuthenticatingHTTPProtocolDemo/JiveAuthenticatingHTTPProtocolDemo/ViewController.m @@ -30,7 +30,7 @@ - (void)viewDidLoad { [JAHPAuthenticatingHTTPProtocol setDelegate:self]; [JAHPAuthenticatingHTTPProtocol start]; self.webView.delegate = self; - [self.webView loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://httpbin.org/basic-auth/foo/bar"]]]; + [self.webView loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://httpbin.org/basic-auth/foo/bar"]]]; } #pragma mark - JAHPAuthenticatingHTTPProtocolDelegate @@ -127,7 +127,7 @@ - (JAHPDidCancelAuthenticationChallengeHandler)authenticatingHTTPProtocol:(JAHPA // Then all logs will go to the `authenticatingHTTPProtocol:logMessage:` method // //- (void)authenticatingHTTPProtocol:(JAHPAuthenticatingHTTPProtocol *)authenticatingHTTPProtocol logWithFormat:(NSString *)format arguments:(va_list)arguments { -// +// // NSLog(@"logWithFormat: %@", [[NSString alloc] initWithFormat:format arguments:arguments]); //} From 3afaaeb320fc25a0dd9ac1d2eb9d22576ac47f3a Mon Sep 17 00:00:00 2001 From: Alexander Dodatko Date: Mon, 30 Jul 2018 12:20:50 +0300 Subject: [PATCH 6/8] [#6] using NSSet for auth scheme support check --- .../ViewController.m | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/JiveAuthenticatingHTTPProtocolDemo/JiveAuthenticatingHTTPProtocolDemo/ViewController.m b/JiveAuthenticatingHTTPProtocolDemo/JiveAuthenticatingHTTPProtocolDemo/ViewController.m index b1805ec..0b4b91a 100644 --- a/JiveAuthenticatingHTTPProtocolDemo/JiveAuthenticatingHTTPProtocolDemo/ViewController.m +++ b/JiveAuthenticatingHTTPProtocolDemo/JiveAuthenticatingHTTPProtocolDemo/ViewController.m @@ -36,7 +36,19 @@ - (void)viewDidLoad { #pragma mark - JAHPAuthenticatingHTTPProtocolDelegate - (BOOL)authenticatingHTTPProtocol:(JAHPAuthenticatingHTTPProtocol *)authenticatingHTTPProtocol canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace { - BOOL canAuthenticate = [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPBasic]; + + NSArray* interceptedAuthMethods = + @[ + NSURLAuthenticationMethodHTTPBasic + , NSURLAuthenticationMethodHTTPDigest + , NSURLAuthenticationMethodNTLM + ]; + + NSSet* interceptedAuthMethodsSet = [NSSet setWithArray: interceptedAuthMethods]; + + BOOL canAuthenticate = + [interceptedAuthMethodsSet containsObject: protectionSpace.authenticationMethod]; + return canAuthenticate; } From 7c23494f1fc2269e831960d82b70c0f75208b2e2 Mon Sep 17 00:00:00 2001 From: Alexander Dodatko Date: Fri, 27 Jul 2018 16:30:53 +0300 Subject: [PATCH 7/8] [#4] explicitly added authentication scheme support description to the readme. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added some pitfalls I’ve faced to the readme too. --- README.md | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8e7d32f..e0a64c1 100644 --- a/README.md +++ b/README.md @@ -4,11 +4,66 @@ JiveAuthenticatingHTTPProtocol Based on [Apple's CustomHTTPProtocol](https://developer.apple.com/library/prerelease/ios/samplecode/CustomHTTPProtocol/Introduction/Intro.html), `JiveAuthenticatingHTTPProtocol` provides authentication callbacks for a `UIWebView`. +#### Note : + +``` +No cleanup of the sample code has been done, so there might be some strange "patterns" inside. +``` + +See https://github.com/jivesoftware/JiveAuthenticatingHTTPProtocol/issues/3 for more details. + + Usage ----- -`JiveAuthenticatingHTTPProtocol` captures all `NSURLConnection` traffic. Ensure that no other `NSURLConnection`s can start after you call `+[JAHPAuthenticatingHTTPProtocol start]`. Before loading an `NSURLRequest` that may require handling an authentication callback, call `+[JAHPAuthenticatingHTTPProtocol setDelegate:]`, then `+[JAHPAuthenticatingHTTPProtocol start]`. Finally, load your `NSURLRequest`, and handle the callbacks from `JAHPAuthenticatingHTTPProtocolDelegate`. +`JiveAuthenticatingHTTPProtocol` captures all `NSURLConnection` traffic. + +1. Ensure that no other `NSURLConnection`s can start after you call `+[JAHPAuthenticatingHTTPProtocol start]`. +2. Before loading an `NSURLRequest` that may require handling an authentication callback, call `+[JAHPAuthenticatingHTTPProtocol setDelegate:]`, +3. Then call `+[JAHPAuthenticatingHTTPProtocol start]`. +4. Finally, load your `NSURLRequest`, and handle the callbacks from `JAHPAuthenticatingHTTPProtocolDelegate`. + +See `JiveAuthenticatingHTTPProtocolDemo/ViewController.m` for an example. + +It might be a good idea to memoize the credentials entered by the user to avoid "spamming" them with the data input alerts. This is an absolute "must" if your page contains the video which requires authorization. + + + +Supported Authorization Methods +----- +This library supports all of the authentication schemes `NSURLAuthentication` supports. + +This has been tested with `NTLM` and `ADFS` authentication, (which `NSURLAuthentication` natively supports). Basically, if an authentication method works in `Safari`, it'll probably work here, too. + +You can add more authorization methods like this : + +```obj-c +- (BOOL)authenticatingHTTPProtocol:(JAHPAuthenticatingHTTPProtocol *)authenticatingHTTPProtocol +canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace +{ + NSArray* interceptedAuthMethods = + @[ + NSURLAuthenticationMethodHTTPBasic + , NSURLAuthenticationMethodNTLM + ]; + + NSSet* interceptedAuthMethodsSet = [NSSet setWithArray: interceptedAuthMethods]; + + BOOL canAuthenticate = + [interceptedAuthMethodsSet containsObject: protectionSpace.authenticationMethod]; + + return canAuthenticate; +} +``` + + +#### Caution : + +``` +Stubbing the above function to always return "true" is not always a good idea. +If you do so, it would be your responsibility to implement all the required handshakes. +``` + -See JiveAuthenticatingHTTPProtocolDemo/ViewController.m for an example. License ------- From bac3916b7073e1413a19018d8de2234b6f477453 Mon Sep 17 00:00:00 2001 From: Alexander Dodatko Date: Mon, 30 Jul 2018 12:33:07 +0300 Subject: [PATCH 8/8] replaced extern ==> FOUNDATION_EXPORT for better obj-c++ support --- Source/JiveAuthenticatingHTTPProtocol/JAHPCacheStoragePolicy.h | 2 +- Source/JiveAuthenticatingHTTPProtocol/JAHPCacheStoragePolicy.m | 2 +- Source/JiveAuthenticatingHTTPProtocol/JAHPCanonicalRequest.h | 2 +- Source/JiveAuthenticatingHTTPProtocol/JAHPCanonicalRequest.m | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Source/JiveAuthenticatingHTTPProtocol/JAHPCacheStoragePolicy.h b/Source/JiveAuthenticatingHTTPProtocol/JAHPCacheStoragePolicy.h index b4ec9b6..77a7fe7 100644 --- a/Source/JiveAuthenticatingHTTPProtocol/JAHPCacheStoragePolicy.h +++ b/Source/JiveAuthenticatingHTTPProtocol/JAHPCacheStoragePolicy.h @@ -58,4 +58,4 @@ * \returns A cache storage policy to use. */ -extern NSURLCacheStoragePolicy JAHPCacheStoragePolicyForRequestAndResponse(NSURLRequest * request, NSHTTPURLResponse * response); +FOUNDATION_EXPORT NSURLCacheStoragePolicy JAHPCacheStoragePolicyForRequestAndResponse(NSURLRequest * request, NSHTTPURLResponse * response); diff --git a/Source/JiveAuthenticatingHTTPProtocol/JAHPCacheStoragePolicy.m b/Source/JiveAuthenticatingHTTPProtocol/JAHPCacheStoragePolicy.m index fca3496..7e61533 100644 --- a/Source/JiveAuthenticatingHTTPProtocol/JAHPCacheStoragePolicy.m +++ b/Source/JiveAuthenticatingHTTPProtocol/JAHPCacheStoragePolicy.m @@ -47,7 +47,7 @@ OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF #import "JAHPCacheStoragePolicy.h" -extern NSURLCacheStoragePolicy JAHPCacheStoragePolicyForRequestAndResponse(NSURLRequest * request, NSHTTPURLResponse * response) +FOUNDATION_EXPORT NSURLCacheStoragePolicy JAHPCacheStoragePolicyForRequestAndResponse(NSURLRequest * request, NSHTTPURLResponse * response) // See comment in header. { BOOL cacheable; diff --git a/Source/JiveAuthenticatingHTTPProtocol/JAHPCanonicalRequest.h b/Source/JiveAuthenticatingHTTPProtocol/JAHPCanonicalRequest.h index 39c10da..e5f4bce 100644 --- a/Source/JiveAuthenticatingHTTPProtocol/JAHPCanonicalRequest.h +++ b/Source/JiveAuthenticatingHTTPProtocol/JAHPCanonicalRequest.h @@ -61,4 +61,4 @@ * \returns The canonical request; should never be nil. */ -extern NSMutableURLRequest * JAHPCanonicalRequestForRequest(NSURLRequest *request); +FOUNDATION_EXPORT NSMutableURLRequest * JAHPCanonicalRequestForRequest(NSURLRequest *request); diff --git a/Source/JiveAuthenticatingHTTPProtocol/JAHPCanonicalRequest.m b/Source/JiveAuthenticatingHTTPProtocol/JAHPCanonicalRequest.m index c2eadbf..dd1c222 100644 --- a/Source/JiveAuthenticatingHTTPProtocol/JAHPCanonicalRequest.m +++ b/Source/JiveAuthenticatingHTTPProtocol/JAHPCanonicalRequest.m @@ -343,7 +343,7 @@ static void CanonicaliseHeaders(NSMutableURLRequest * request) #pragma mark * API -extern NSMutableURLRequest * JAHPCanonicalRequestForRequest(NSURLRequest *request) +FOUNDATION_EXPORT NSMutableURLRequest * JAHPCanonicalRequestForRequest(NSURLRequest *request) { NSMutableURLRequest * result; NSString * scheme;