Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Support for HTTP basic auth #23

Open
wants to merge 1 commit into from

1 participant

@imagine

To use, set [EGOImageLoader sharedImageLoader].username and/or [EGOImageLoader sharedImageLoader].password

Josh Benjamin Support for HTTP basic auth. To use, set [EGOImageLoader sharedImageL…
…oader].username and/or [EGOImageLoader sharedImageLoader].password
616caf4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 12, 2012
  1. Support for HTTP basic auth. To use, set [EGOImageLoader sharedImageL…

    Josh Benjamin authored
    …oader].username and/or [EGOImageLoader sharedImageLoader].password
This page is out of date. Refresh to see the latest.
View
2  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<EGOImageLoadConnectionDelegate> delegate;
View
23 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;
View
2  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<NSObject>
View
4 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];
Something went wrong with that request. Please try again.