From e18c305034f25b41ad4bf9dd8bedce8fdddf7e34 Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Sun, 4 Dec 2011 03:35:21 -0600 Subject: [PATCH 1/4] Default implementation for connection:didReceiveAuthenticationChallenge: delegate method --- AFNetworking/AFURLConnectionOperation.m | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/AFNetworking/AFURLConnectionOperation.m b/AFNetworking/AFURLConnectionOperation.m index bc8a71e..dda6c12 100644 --- a/AFNetworking/AFURLConnectionOperation.m +++ b/AFNetworking/AFURLConnectionOperation.m @@ -305,6 +305,31 @@ static inline NSString * AFKeyPathFromOperationState(AFOperationState state) { #pragma mark - NSURLConnectionDelegate +- (void)connection:(NSURLConnection *)connection +didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge +{ + if ([challenge previousFailureCount] == 0) { + NSURLCredential *credential = nil; + + NSString *username = [(NSString *)CFURLCopyUserName((CFURLRef)[self.request URL]) autorelease]; + NSString *password = [(NSString *)CFURLCopyPassword((CFURLRef)[self.request URL]) autorelease]; + + if (username && password) { + credential = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceNone]; + } else if (username) { + credential = [[[NSURLCredentialStorage sharedCredentialStorage] credentialsForProtectionSpace:[challenge protectionSpace]] objectForKey:username]; + } else { + credential = [[NSURLCredentialStorage sharedCredentialStorage] defaultCredentialForProtectionSpace:[challenge protectionSpace]]; + } + + if (credential) { + [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge]; + } + } else { + [[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge]; + } +} + - (void)connection:(NSURLConnection *)__unused connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten From 8869bb9ad3493ddb2728bb64185bd92f69dfdb6e Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Sun, 4 Dec 2011 03:45:02 -0600 Subject: [PATCH 2/4] Adding authentication challenge callback block to override default NSURLConnection delegate method implementation --- AFNetworking/AFURLConnectionOperation.h | 6 ++++++ AFNetworking/AFURLConnectionOperation.m | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/AFNetworking/AFURLConnectionOperation.h b/AFNetworking/AFURLConnectionOperation.h index 600981c..abd2495 100644 --- a/AFNetworking/AFURLConnectionOperation.h +++ b/AFNetworking/AFURLConnectionOperation.h @@ -185,4 +185,10 @@ extern NSString * const AFNetworkingOperationDidFinishNotification; */ - (void)setDownloadProgressBlock:(void (^)(NSInteger bytesRead, NSInteger totalBytesRead, NSInteger totalBytesExpectedToRead))block; +///------------------------------------------------- +/// @name Setting Authentication Challenge Callbacks +///------------------------------------------------- + +- (void)setAuthenticationChallengeBlock:(void (^)(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge))block; + @end diff --git a/AFNetworking/AFURLConnectionOperation.m b/AFNetworking/AFURLConnectionOperation.m index dda6c12..0172a71 100644 --- a/AFNetworking/AFURLConnectionOperation.m +++ b/AFNetworking/AFURLConnectionOperation.m @@ -37,6 +37,7 @@ NSString * const AFNetworkingOperationDidStartNotification = @"com.alamofire.net NSString * const AFNetworkingOperationDidFinishNotification = @"com.alamofire.networking.operation.finish"; typedef void (^AFURLConnectionOperationProgressBlock)(NSInteger bytes, NSInteger totalBytes, NSInteger totalBytesExpected); +typedef void (^AFURLConnectionOperationAuthenticationChallengeBlock)(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge); static inline NSString * AFKeyPathFromOperationState(AFOperationState state) { switch (state) { @@ -64,6 +65,7 @@ static inline NSString * AFKeyPathFromOperationState(AFOperationState state) { @property (readwrite, nonatomic, retain) NSMutableData *dataAccumulator; @property (readwrite, nonatomic, copy) AFURLConnectionOperationProgressBlock uploadProgress; @property (readwrite, nonatomic, copy) AFURLConnectionOperationProgressBlock downloadProgress; +@property (readwrite, nonatomic, copy) AFURLConnectionOperationAuthenticationChallengeBlock authenticationBlock; - (BOOL)shouldTransitionToState:(AFOperationState)state; - (void)operationDidStart; @@ -86,6 +88,7 @@ static inline NSString * AFKeyPathFromOperationState(AFOperationState state) { @synthesize outputStream = _outputStream; @synthesize uploadProgress = _uploadProgress; @synthesize downloadProgress = _downloadProgress; +@synthesize authenticationBlock = _authenticationBlock; + (void)networkRequestThreadEntryPoint:(id)__unused object { do { @@ -136,6 +139,7 @@ static inline NSString * AFKeyPathFromOperationState(AFOperationState state) { [_uploadProgress release]; [_downloadProgress release]; + [_authenticationBlock release]; [super dealloc]; } @@ -170,6 +174,10 @@ static inline NSString * AFKeyPathFromOperationState(AFOperationState state) { self.downloadProgress = block; } +- (void)setAuthenticationChallengeBlock:(void (^)(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge))block { + self.authenticationBlock = block; +} + - (void)setState:(AFOperationState)state { if (![self shouldTransitionToState:state]) { return; @@ -308,6 +316,11 @@ static inline NSString * AFKeyPathFromOperationState(AFOperationState state) { - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { + if (self.authenticationBlock) { + self.authenticationBlock(connection, challenge); + return; + } + if ([challenge previousFailureCount] == 0) { NSURLCredential *credential = nil; From bdab46889b7e0551cfb90fd881de97e7a0848684 Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Tue, 10 Jan 2012 13:41:36 -0800 Subject: [PATCH 3/4] Use nested if/else instead of early return in -connection:didReceiveAuthenticationChallenge: --- AFNetworking/AFURLConnectionOperation.m | 41 ++++++++++++------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/AFNetworking/AFURLConnectionOperation.m b/AFNetworking/AFURLConnectionOperation.m index 0172a71..cceb306 100644 --- a/AFNetworking/AFURLConnectionOperation.m +++ b/AFNetworking/AFURLConnectionOperation.m @@ -318,28 +318,27 @@ didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { if (self.authenticationBlock) { self.authenticationBlock(connection, challenge); - return; - } - - if ([challenge previousFailureCount] == 0) { - NSURLCredential *credential = nil; - - NSString *username = [(NSString *)CFURLCopyUserName((CFURLRef)[self.request URL]) autorelease]; - NSString *password = [(NSString *)CFURLCopyPassword((CFURLRef)[self.request URL]) autorelease]; - - if (username && password) { - credential = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceNone]; - } else if (username) { - credential = [[[NSURLCredentialStorage sharedCredentialStorage] credentialsForProtectionSpace:[challenge protectionSpace]] objectForKey:username]; - } else { - credential = [[NSURLCredentialStorage sharedCredentialStorage] defaultCredentialForProtectionSpace:[challenge protectionSpace]]; - } - - if (credential) { - [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge]; - } } else { - [[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge]; + if ([challenge previousFailureCount] == 0) { + NSURLCredential *credential = nil; + + NSString *username = [(NSString *)CFURLCopyUserName((CFURLRef)[self.request URL]) autorelease]; + NSString *password = [(NSString *)CFURLCopyPassword((CFURLRef)[self.request URL]) autorelease]; + + if (username && password) { + credential = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceNone]; + } else if (username) { + credential = [[[NSURLCredentialStorage sharedCredentialStorage] credentialsForProtectionSpace:[challenge protectionSpace]] objectForKey:username]; + } else { + credential = [[NSURLCredentialStorage sharedCredentialStorage] defaultCredentialForProtectionSpace:[challenge protectionSpace]]; + } + + if (credential) { + [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge]; + } + } else { + [[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge]; + } } } From 728f5c3d75aed53ac2b58b83bc5b959850a60202 Mon Sep 17 00:00:00 2001 From: Jorge Bernal Date: Thu, 5 Jan 2012 17:45:24 +0100 Subject: [PATCH 4/4] Cancel request if there is an authentication challenge but no credentials for it Conflicts: AFNetworking/AFURLConnectionOperation.m --- AFNetworking/AFURLConnectionOperation.m | 2 ++ 1 file changed, 2 insertions(+) diff --git a/AFNetworking/AFURLConnectionOperation.m b/AFNetworking/AFURLConnectionOperation.m index cceb306..114cec2 100644 --- a/AFNetworking/AFURLConnectionOperation.m +++ b/AFNetworking/AFURLConnectionOperation.m @@ -335,6 +335,8 @@ didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge if (credential) { [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge]; + } else { + [[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge]; } } else { [[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge];