Skip to content

Commit

Permalink
implement http auth
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy@twotoasters.com authored and jeremy@twotoasters.com committed Jan 19, 2010
1 parent 7f531d8 commit 36827bb
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 7 deletions.
16 changes: 11 additions & 5 deletions OTRestClient.m
Expand Up @@ -91,38 +91,44 @@ - (NSURL*)URLForResourcePath:(NSString*)resourcePath {
return [NSURL URLWithString:urlString];
}

- (void)setupRequest:(OTRestRequest*)request {
request.additionalHTTPHeaders = _HTTPHeaders;
request.username = self.username;
request.password = self.password;
}

- (OTRestRequest*)get:(NSString*)resourcePath delegate:(id)delegate callback:(SEL)callback {
OTRestRequest* request = [[OTRestRequest alloc] initWithURL:[self URLForResourcePath:resourcePath] delegate:delegate callback:callback];
request.additionalHTTPHeaders = _HTTPHeaders;
[self setupRequest:request];
[request get];
return request;
}

- (OTRestRequest*)get:(NSString*)resourcePath params:(NSDictionary*)params delegate:(id)delegate callback:(SEL)callback {
NSString* resourcePathWithQueryString = [NSString stringWithFormat:@"%@?%@", resourcePath, [params URLEncodedString]];
OTRestRequest* request = [[OTRestRequest alloc] initWithURL:[self URLForResourcePath:resourcePathWithQueryString] delegate:delegate callback:callback];
request.additionalHTTPHeaders = _HTTPHeaders;
[self setupRequest:request];
[request get];
return request;
}

- (OTRestRequest*)post:(NSString*)resourcePath params:(NSObject<OTRestRequestSerializable>*)params delegate:(id)delegate callback:(SEL)callback {
OTRestRequest* request = [[OTRestRequest alloc] initWithURL:[self URLForResourcePath:resourcePath] delegate:delegate callback:callback];
request.additionalHTTPHeaders = _HTTPHeaders;
[self setupRequest:request];
[request postParams:params];
return request;
}

- (OTRestRequest*)put:(NSString*)resourcePath params:(NSObject<OTRestRequestSerializable>*)params delegate:(id)delegate callback:(SEL)callback {
OTRestRequest* request = [[OTRestRequest alloc] initWithURL:[self URLForResourcePath:resourcePath] delegate:delegate callback:callback];
request.additionalHTTPHeaders = _HTTPHeaders;
[self setupRequest:request];
[request putParams:params];
return request;
}

- (OTRestRequest*)delete:(NSString*)resourcePath delegate:(id)delegate callback:(SEL)callback {
OTRestRequest* request = [[OTRestRequest alloc] initWithURL:[self URLForResourcePath:resourcePath] delegate:delegate callback:callback];
request.additionalHTTPHeaders = _HTTPHeaders;
[self setupRequest:request];
[request delete];
return request;
}
Expand Down
13 changes: 13 additions & 0 deletions OTRestFramework.xcodeproj/project.pbxproj
Expand Up @@ -175,6 +175,13 @@
remoteGlobalIDString = C76EB5D10F74586B00EF8398;
remoteInfo = UISpec_Simulator;
};
3FACF562110638D70003AF0D /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 3F6C39B610FE738A008F47C5 /* UISpec.xcodeproj */;
proxyType = 1;
remoteGlobalIDString = C76EB5D10F74586B00EF8398;
remoteInfo = UISpec_Simulator;
};
3FF0DF4410FE7CEA008901F7 /* PBXContainerItemProxy */ = {
isa = PBXContainerItemProxy;
containerPortal = 3F6C39B610FE738A008F47C5 /* UISpec.xcodeproj */;
Expand Down Expand Up @@ -684,6 +691,7 @@
);
dependencies = (
3F6C399D10FE5C80008F47C5 /* PBXTargetDependency */,
3FACF563110638D70003AF0D /* PBXTargetDependency */,
);
name = OTRestFramework;
productName = OTRestFramework;
Expand Down Expand Up @@ -905,6 +913,11 @@
name = UISpec_Simulator;
targetProxy = 3F6C3A9B10FE753B008F47C5 /* PBXContainerItemProxy */;
};
3FACF563110638D70003AF0D /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
name = UISpec_Simulator;
targetProxy = 3FACF562110638D70003AF0D /* PBXContainerItemProxy */;
};
/* End PBXTargetDependency section */

/* Begin XCBuildConfiguration section */
Expand Down
2 changes: 1 addition & 1 deletion OTRestModelMapper.m
Expand Up @@ -109,7 +109,7 @@ - (void)updateObject:(id)model ifNewPropertyPropertyValue:(id)propertyValue forP
BOOL areEqual = ComparisonSender(currentValue, comparisonSelector, propertyValue);

if (NO == areEqual) {
NSLog(@"Setting property %@ to new value %@", propertyName, propertyValue);
//NSLog(@"Setting property %@ to new value %@", propertyName, propertyValue);
[model setValue:propertyValue forKey:propertyName];
}
}
Expand Down
8 changes: 8 additions & 0 deletions OTRestRequest.h
Expand Up @@ -18,8 +18,16 @@
id _delegate;
SEL _callback;
id _userData;
NSString* _username;
NSString* _password;
}

/**
* used for http auth chalange
*/
@property(nonatomic, retain) NSString* username;
@property(nonatomic, retain) NSString* password;

@property(nonatomic, readonly) NSURL* URL;

/**
Expand Down
4 changes: 3 additions & 1 deletion OTRestRequest.m
Expand Up @@ -14,7 +14,7 @@
@implementation OTRestRequest

@synthesize URL = _URL, URLRequest = _URLRequest, delegate = _delegate, callback = _callback, additionalHTTPHeaders = _additionalHTTPHeaders,
params = _params, userData = _userData;
params = _params, userData = _userData, username = _username, password = _password;

+ (OTRestRequest*)requestWithURL:(NSURL*)URL delegate:(id)delegate callback:(SEL)callback {
OTRestRequest* request = [[OTRestRequest alloc] initWithURL:URL delegate:delegate callback:callback];
Expand All @@ -40,6 +40,8 @@ - (void)dealloc {
[_delegate release];
[_params release];
[_additionalHTTPHeaders release];
[_username release];
[_password release];
[super dealloc];
}

Expand Down
14 changes: 14 additions & 0 deletions OTRestResponse.m
Expand Up @@ -38,6 +38,20 @@ - (void)dealloc {
[super dealloc];
}

// Handle basic auth
-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if ([challenge previousFailureCount] == 0) {
NSURLCredential *newCredential;
newCredential=[NSURLCredential credentialWithUser:[NSString stringWithFormat:@"%@", _request.username]
password:[NSString stringWithFormat:@"%@", _request.password]
persistence:NSURLCredentialPersistenceNone];
[[challenge sender] useCredential:newCredential
forAuthenticationChallenge:challenge];
} else {
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[_payload appendData:data];
}
Expand Down

0 comments on commit 36827bb

Please sign in to comment.