From 616caf4585ca2add494af4811ef51892f96003b4 Mon Sep 17 00:00:00 2001 From: Josh Benjamin Date: Thu, 12 Apr 2012 13:35:58 -0700 Subject: [PATCH] Support for HTTP basic auth. To use, set [EGOImageLoader sharedImageLoader].username and/or [EGOImageLoader sharedImageLoader].password --- EGOImageLoader/EGOImageLoadConnection.h | 2 ++ EGOImageLoader/EGOImageLoadConnection.m | 23 +++++++++++++++++++++++ EGOImageLoader/EGOImageLoader.h | 2 ++ EGOImageLoader/EGOImageLoader.m | 4 ++++ 4 files changed, 31 insertions(+) diff --git a/EGOImageLoader/EGOImageLoadConnection.h b/EGOImageLoader/EGOImageLoadConnection.h index c0584ab..886b7dd 100644 --- a/EGOImageLoader/EGOImageLoadConnection.h +++ b/EGOImageLoader/EGOImageLoadConnection.h @@ -47,6 +47,8 @@ @property(nonatomic,readonly) NSData* responseData; @property(nonatomic,readonly,getter=imageURL) NSURL* imageURL; +@property(nonatomic,retain) NSString *username; +@property(nonatomic,retain) NSString *password; @property(nonatomic,retain) NSURLResponse* response; @property(nonatomic,assign) id delegate; diff --git a/EGOImageLoader/EGOImageLoadConnection.m b/EGOImageLoader/EGOImageLoadConnection.m index 435225a..e20a045 100644 --- a/EGOImageLoader/EGOImageLoadConnection.m +++ b/EGOImageLoader/EGOImageLoadConnection.m @@ -29,6 +29,7 @@ @implementation EGOImageLoadConnection @synthesize imageURL=_imageURL, response=_response, delegate=_delegate, timeoutInterval=_timeoutInterval; +@synthesize username=_username, password=_password; #if __EGOIL_USE_BLOCKS @synthesize handlers; @@ -92,6 +93,28 @@ - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)err } } +- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)space { + if([[space authenticationMethod] isEqualToString:NSURLAuthenticationMethodHTTPBasic]) { + return self.username || self.password; + } + return NO; +} + +- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { + if ([challenge previousFailureCount] == 0) { + NSURLCredential *newCredential = [NSURLCredential credentialWithUser:self.username + password:self.password + persistence:NSURLCredentialPersistenceNone]; + [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge]; + } + else { +#ifdef DEBUG + NSLog(@"Failed authentication challenge after %ld failures", (long) [challenge previousFailureCount]); +#endif + [[challenge sender] cancelAuthenticationChallenge:challenge]; + } +} + - (void)dealloc { self.response = nil; diff --git a/EGOImageLoader/EGOImageLoader.h b/EGOImageLoader/EGOImageLoader.h index 7997e9d..3c87e1b 100644 --- a/EGOImageLoader/EGOImageLoader.h +++ b/EGOImageLoader/EGOImageLoader.h @@ -70,6 +70,8 @@ - (void)clearCacheForURL:(NSURL*)aURL style:(NSString*)style; @property(nonatomic,retain) NSDictionary* currentConnections; +@property(nonatomic,retain) NSString *username; +@property(nonatomic,retain) NSString *password; @end @protocol EGOImageLoaderObserver diff --git a/EGOImageLoader/EGOImageLoader.m b/EGOImageLoader/EGOImageLoader.m index 32a619a..1bce3bf 100644 --- a/EGOImageLoader/EGOImageLoader.m +++ b/EGOImageLoader/EGOImageLoader.m @@ -59,6 +59,7 @@ - (void)handleCompletionsForConnection:(EGOImageLoadConnection*)connection image @implementation EGOImageLoader @synthesize currentConnections=_currentConnections; +@synthesize username=_username, password=_password; + (EGOImageLoader*)sharedImageLoader { @synchronized(self) { @@ -128,6 +129,9 @@ - (EGOImageLoadConnection*)loadImageForURL:(NSURL*)aURL { return connection; } else { connection = [[EGOImageLoadConnection alloc] initWithImageURL:aURL delegate:self]; + + connection.username = self.username; + connection.password = self.password; [connectionsLock lock]; [currentConnections setObject:connection forKey:aURL];