Permalink
Browse files

Added multipartFormRequest and refactored auth header

  • Loading branch information...
1 parent 711fab4 commit 18ea4fe48290385ed36e4f95a7287408a4f314fb @pj4533 pj4533 committed Feb 21, 2013
Showing with 27 additions and 15 deletions.
  1. +27 −15 AFOAuth1Client/AFOAuth1Client.m
@@ -143,7 +143,9 @@ - (NSString *)OAuthSignatureForMethod:(NSString *)method
path:(NSString *)path
parameters:(NSDictionary *)parameters
token:(AFOAuth1Token *)requestToken;
-- (NSString *)authorizationHeaderForParameters:(NSDictionary *)parameters;
+- (NSString *)authorizationHeaderForMethod:(NSString*)method
+ path:(NSString*)path
+ parameters:(NSDictionary *)parameters;
@end
@implementation AFOAuth1Client
@@ -208,14 +210,21 @@ - (NSString *)OAuthSignatureForMethod:(NSString *)method
}
}
-- (NSString *)authorizationHeaderForParameters:(NSDictionary *)parameters {
+- (NSString *)authorizationHeaderForMethod:(NSString*)method
+ path:(NSString*)path
+ parameters:(NSDictionary *)parameters {
static NSString * const kAFOAuth1AuthorizationFormatString = @"OAuth %@";
- if (!parameters) {
- return nil;
+ NSMutableDictionary *mutableParameters = parameters ? [parameters mutableCopy] : [NSMutableDictionary dictionary];
+
+ if (self.accessToken) {
+ [mutableParameters addEntriesFromDictionary:[self OAuthParameters]];
+ [mutableParameters setValue:self.accessToken.key forKey:@"oauth_token"];
}
+
+ [mutableParameters setValue:[self OAuthSignatureForMethod:method path:path parameters:mutableParameters token:self.accessToken] forKey:@"oauth_signature"];
- NSArray *sortedComponents = [[AFQueryStringFromParametersWithEncoding(parameters, self.stringEncoding) componentsSeparatedByString:@"&"] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
+ NSArray *sortedComponents = [[AFQueryStringFromParametersWithEncoding(mutableParameters, self.stringEncoding) componentsSeparatedByString:@"&"] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
NSMutableArray *mutableComponents = [NSMutableArray array];
for (NSString *component in sortedComponents) {
NSArray *subcomponents = [component componentsSeparatedByString:@"="];
@@ -327,22 +336,25 @@ - (NSMutableURLRequest *)requestWithMethod:(NSString *)method
path:(NSString *)path
parameters:(NSDictionary *)parameters
{
- NSMutableDictionary *mutableParameters = parameters ? [parameters mutableCopy] : [NSMutableDictionary dictionary];
-
- if (self.accessToken) {
- [mutableParameters addEntriesFromDictionary:[self OAuthParameters]];
- [mutableParameters setValue:self.accessToken.key forKey:@"oauth_token"];
- }
-
- [mutableParameters setValue:[self OAuthSignatureForMethod:method path:path parameters:mutableParameters token:self.accessToken] forKey:@"oauth_signature"];
-
NSMutableURLRequest *request = [super requestWithMethod:method path:path parameters:parameters];
- [request setValue:[self authorizationHeaderForParameters:mutableParameters] forHTTPHeaderField:@"Authorization"];
+ [request setValue:[self authorizationHeaderForMethod:method path:path parameters:parameters] forHTTPHeaderField:@"Authorization"];
[request setHTTPShouldHandleCookies:NO];
return request;
}
+- (NSMutableURLRequest *)multipartFormRequestWithMethod:(NSString *)method
+ path:(NSString *)path
+ parameters:(NSDictionary *)parameters
+ constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
+{
+ NSMutableURLRequest* request = [super multipartFormRequestWithMethod:method path:path parameters:parameters constructingBodyWithBlock:block];
+ [request setValue:[self authorizationHeaderForMethod:method path:path parameters:parameters] forHTTPHeaderField:@"Authorization"];
+ [request setHTTPShouldHandleCookies:NO];
+
+ return request;
+}
+
@end
#pragma mark -

0 comments on commit 18ea4fe

Please sign in to comment.