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

Support for HTTP basic auth #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 EGOImageLoader/EGOImageLoadConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<EGOImageLoadConnectionDelegate> delegate;

Expand Down
23 changes: 23 additions & 0 deletions EGOImageLoader/EGOImageLoadConnection.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions EGOImageLoader/EGOImageLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<NSObject>
Expand Down
4 changes: 4 additions & 0 deletions EGOImageLoader/EGOImageLoader.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ - (void)handleCompletionsForConnection:(EGOImageLoadConnection*)connection image

@implementation EGOImageLoader
@synthesize currentConnections=_currentConnections;
@synthesize username=_username, password=_password;

+ (EGOImageLoader*)sharedImageLoader {
@synchronized(self) {
Expand Down Expand Up @@ -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];
Expand Down