Skip to content

Commit

Permalink
Support for multipart data requests
Browse files Browse the repository at this point in the history
  • Loading branch information
romaonthego committed Jan 30, 2013
1 parent 1dc0a61 commit c8d0ad6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
4 changes: 2 additions & 2 deletions AFXAuthClient.podspec
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Pod::Spec.new do |s|
s.name = 'AFXAuthClient'
s.version = '1.0.1'
s.version = '1.0.2'
s.summary = 'AFNetworking Extension for XAuth Authentication.'
s.homepage = 'https://github.com/romaonthego/AFXAuthClient'
s.license = { :type => "MIT", :file => "LICENSE" }
s.authors = { 'Roman Efimov' => '[email protected]', 'Mattt Thompson' => '[email protected]', 'Eric Johnson' => '[email protected]' }
s.source = { :git => 'https://github.com/romaonthego/AFXAuthClient.git', :tag => '1.0.1'}
s.source = { :git => 'https://github.com/romaonthego/AFXAuthClient.git', :tag => '1.0.2'}
s.source_files = 'AFXAuthClient'
s.requires_arc = true

Expand Down
40 changes: 31 additions & 9 deletions AFXAuthClient/AFXAuthClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,15 @@ - (NSString *)baseStringWithRequest:(NSURLRequest *)request parameters:(NSDictio

for (NSString *key in parameters) {
NSString *param = AFPercentEscapedQueryStringPairMemberFromStringWithEncoding([parameters objectForKey:key], NSUTF8StringEncoding);
//if ([key isEqualToString:@"data"])
param = AFPercentEscapedQueryStringPairMemberFromStringWithEncoding(param, NSUTF8StringEncoding);

params = [params arrayByAddingObjectsFromArray:@[[NSString stringWithFormat:@"%@%%3D%@", key, param]]];
}
if (self.token)
params = [params arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:[NSString stringWithFormat:@"%@%%3D%@", @"oauth_token", AFPercentEscapedQueryStringPairMemberFromStringWithEncoding(self.token.key, NSUTF8StringEncoding)], nil]];


params = [params sortedArrayUsingSelector:@selector(compare:)];
NSString *baseString = [@[request.HTTPMethod,
AFPercentEscapedQueryStringPairMemberFromStringWithEncoding(request.URL.absoluteString, NSUTF8StringEncoding),
Expand Down Expand Up @@ -208,25 +211,44 @@ - (void)authorizeUsingXAuthWithAccessTokenPath:(NSString *)accessTokenPath
[self enqueueHTTPRequestOperation:operation];
}

- (NSMutableDictionary *)authorizationHeaderWithRequest:(NSURLRequest *)request parameters:(NSDictionary *)parameters
{
NSMutableDictionary *authorizationHeader = [[NSMutableDictionary alloc] initWithDictionary:@{@"oauth_nonce": _nonce,
@"oauth_signature_method": @"HMAC-SHA1",
@"oauth_timestamp": _timestamp,
@"oauth_consumer_key": self.consumerKey,
@"oauth_signature": AFHMACSHA1Signature([self baseStringWithRequest:request parameters:parameters], _consumerSecret, _token.secret),
@"oauth_version": @"1.0"}];
if (self.token)
[authorizationHeader setObject:AFPercentEscapedQueryStringPairMemberFromStringWithEncoding(self.token.key, NSUTF8StringEncoding) forKey:@"oauth_token"];

return authorizationHeader;
}

#pragma mark - AFHTTPClient

- (NSMutableURLRequest *)requestWithMethod:(NSString *)method
path:(NSString *)path
parameters:(NSDictionary *)parameters
{
_nonce = [NSString stringWithFormat:@"%d", arc4random()];
_timestamp = [NSString stringWithFormat:@"%d", (int)ceil((float)[[NSDate date] timeIntervalSince1970])];// [//[NSString stringWithFormat:@"%d", (int)(((float)([[NSDate date] timeIntervalSince1970])) + 0.5 + arc4random()%10)];
_timestamp = [NSString stringWithFormat:@"%d", (int)ceil((float)[[NSDate date] timeIntervalSince1970])];

NSMutableURLRequest *request = [super requestWithMethod:method path:path parameters:parameters];
NSMutableDictionary *authorizationHeader = [[NSMutableDictionary alloc] initWithDictionary:@{@"oauth_nonce": _nonce,
@"oauth_signature_method": @"HMAC-SHA1",
@"oauth_timestamp": _timestamp,
@"oauth_consumer_key": self.consumerKey,
@"oauth_signature": AFHMACSHA1Signature([self baseStringWithRequest:request parameters:parameters], _consumerSecret, _token.secret),
@"oauth_version": @"1.0"}];
NSMutableDictionary *authorizationHeader = [self authorizationHeaderWithRequest:request parameters:parameters];

if (self.token)
[authorizationHeader setObject:AFPercentEscapedQueryStringPairMemberFromStringWithEncoding(self.token.key, NSUTF8StringEncoding) forKey:@"oauth_token"];
[request setValue:[self authorizationHeaderForParameters:authorizationHeader] forHTTPHeaderField:@"Authorization"];
[request setHTTPShouldHandleCookies:NO];
return request;
}

- (NSMutableURLRequest *)multipartFormRequestWithMethod:(NSString *)method path:(NSString *)path parameters:(NSDictionary *)parameters constructingBodyWithBlock:(void (^)(id<AFMultipartFormData>))block
{
_nonce = [NSString stringWithFormat:@"%d", arc4random()];
_timestamp = [NSString stringWithFormat:@"%d", (int)ceil((float)[[NSDate date] timeIntervalSince1970])];

NSMutableURLRequest *request = [super multipartFormRequestWithMethod:method path:path parameters:parameters constructingBodyWithBlock:block];
NSMutableDictionary *authorizationHeader = [self authorizationHeaderWithRequest:request parameters:parameters];

[request setValue:[self authorizationHeaderForParameters:authorizationHeader] forHTTPHeaderField:@"Authorization"];
[request setHTTPShouldHandleCookies:NO];
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[AFNetworking](https://github.com/AFNetworking/AFNetworking) Extension for XAuth Authentication.

## Example
## Example

Here's the sample code that works with Tumblr XAuth:

Expand All @@ -17,7 +17,7 @@ AFXAuthClient *client = [[AFXAuthClient alloc] initWithBaseURL:[NSURL URLWithStr
password:@"YOUR TUMBLR PASSWORD"
success:^(AFXAuthToken *accessToken) {
NSLog(@"Success = %@", accessToken);

// Now let's request profile information
//
NSMutableURLRequest *request = [client requestWithMethod:@"POST" path:@"/v2/user/info" parameters:nil];
Expand Down

0 comments on commit c8d0ad6

Please sign in to comment.