Skip to content

Commit

Permalink
+ Added parameterStyle option to switch between authorization header,
Browse files Browse the repository at this point in the history
  body and query params style.
  • Loading branch information
petercv committed Jul 11, 2012
1 parent bd042fc commit 4afd4e2
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 14 deletions.
10 changes: 10 additions & 0 deletions RSOAuthEngine/RSOAuthEngine.h
Expand Up @@ -23,6 +23,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. // THE SOFTWARE.


#import "MKNetworkKit.h"

typedef enum _RSOAuthTokenType typedef enum _RSOAuthTokenType
{ {
RSOAuthRequestToken, RSOAuthRequestToken,
Expand All @@ -35,6 +37,12 @@ typedef enum _RSOOAuthSignatureMethod {
RSOAuthHMAC_SHA1, RSOAuthHMAC_SHA1,
} RSOAuthSignatureMethod; } RSOAuthSignatureMethod;


typedef enum _RSOAuthParameterStyle {
RSOAuthParameterStyleHeader,
RSOAuthParameterStylePostBody,
RSOAuthParameterStyleQueryString
} RSOAuthParameterStyle;

@interface RSOAuthEngine : MKNetworkEngine @interface RSOAuthEngine : MKNetworkEngine
{ {
@private @private
Expand All @@ -57,6 +65,8 @@ typedef enum _RSOOAuthSignatureMethod {
@property (readonly) NSString *tokenSecret; @property (readonly) NSString *tokenSecret;
@property (readonly) NSString *verifier; @property (readonly) NSString *verifier;


@property (nonatomic, assign) RSOAuthParameterStyle parameterStyle;

- (id)initWithHostName:(NSString *)hostName - (id)initWithHostName:(NSString *)hostName
customHeaderFields:(NSDictionary *)headers customHeaderFields:(NSDictionary *)headers
signatureMethod:(RSOAuthSignatureMethod)signatureMethod signatureMethod:(RSOAuthSignatureMethod)signatureMethod
Expand Down
82 changes: 68 additions & 14 deletions RSOAuthEngine/RSOAuthEngine.m
Expand Up @@ -36,6 +36,33 @@
@"HMAC-SHA1", @"HMAC-SHA1",
}; };


@interface MKNetworkOperation (RSO)

@property (strong, nonatomic) NSMutableURLRequest *request;
@property (strong, nonatomic) NSMutableDictionary *fieldsToBePosted;

- (void)rs_setURL:(NSURL *)URL;
- (void)rs_setValue:(NSString *)value forKey:(NSString *)key;

@end

@implementation MKNetworkOperation (RSO)

@dynamic request;
@dynamic fieldsToBePosted;

- (void)rs_setURL:(NSURL *)URL {
[self.request setURL:URL];
}

- (void)rs_setValue:(NSString *)value forKey:(NSString *)key {
[self.fieldsToBePosted setObject:value forKey:key];
}

@end



@interface RSOAuthEngine () @interface RSOAuthEngine ()


- (NSString *)signatureBaseStringForRequest:(MKNetworkOperation *)request signOnlyWithOAuthParams:(BOOL)onlyOAuth; - (NSString *)signatureBaseStringForRequest:(MKNetworkOperation *)request signOnlyWithOAuthParams:(BOOL)onlyOAuth;
Expand All @@ -48,6 +75,8 @@ - (void)setOAuthValue:(NSString *)value forKey:(NSString *)key;


@implementation RSOAuthEngine @implementation RSOAuthEngine


@synthesize parameterStyle = _parameterStyle;

#pragma mark - Read-only Properties #pragma mark - Read-only Properties


- (RSOAuthTokenType)tokenType { - (RSOAuthTokenType)tokenType {
Expand Down Expand Up @@ -115,6 +144,8 @@ - (id)initWithHostName:(NSString *)hostName
nil]; nil];


[self resetOAuthToken]; [self resetOAuthToken];

self.parameterStyle = RSOAuthParameterStyleHeader;
} }


return self; return self;
Expand Down Expand Up @@ -343,21 +374,43 @@ - (void)signRequest:(MKNetworkOperation *)request signOnlyWithOAuthParams:(BOOL)
break; break;
} }


NSMutableArray *oauthHeaders = [NSMutableArray array]; if (self.parameterStyle == RSOAuthParameterStyleHeader) {
NSMutableArray *oauthHeaders = [NSMutableArray array];

[_oAuthValues enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
if (obj && ![obj isEqualToString:@""]) {
[oauthHeaders addObject:[NSString stringWithFormat:@"%@=\"%@\"", [key urlEncodedString], [obj urlEncodedString]]];
}
}];

// Set the Authorization header
NSString *oauthData = [NSString stringWithFormat:@"OAuth %@", [oauthHeaders componentsJoinedByString:@", "]];
NSDictionary *oauthHeader = [NSDictionary dictionaryWithObjectsAndKeys:oauthData, @"Authorization", nil];

[request addHeaders:oauthHeader];

} else if (self.parameterStyle == RSOAuthParameterStylePostBody && [request.readonlyRequest.HTTPMethod caseInsensitiveCompare:@"GET"] != NSOrderedSame) {
[_oAuthValues enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
if (obj && ![obj isEqualToString:@""]) {
[request rs_setValue:obj forKey:key];
}
}];

} else { // self.parameterStyle == RSOAuthParameterStyleQueryString
NSMutableArray *oauthParams = [NSMutableArray array];

// Fill the authorization header array
[_oAuthValues enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
if (obj && ![obj isEqualToString:@""]) {
[oauthParams addObject:[NSString stringWithFormat:@"%@=%@", [key urlEncodedString], [obj urlEncodedString]]];
}
}];


// Fill the authorization header array NSString *url = request.url;
[_oAuthValues enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { NSString *separator = [url rangeOfString:@"?"].length > 0 ? @"&" : @"?";
if (obj && ![obj isEqualToString:@""]) { url = [NSString stringWithFormat:@"%@%@%@", url, separator, [oauthParams componentsJoinedByString:@"&"]];
[oauthHeaders addObject:[NSString stringWithFormat:@"%@=\"%@\"", [key urlEncodedString], [obj urlEncodedString]]]; [request rs_setURL:[NSURL URLWithString:url]];
} }
}];

// Set the Authorization header
NSString *oauthData = [NSString stringWithFormat:@"OAuth %@", [oauthHeaders componentsJoinedByString:@", "]];
NSDictionary *oauthHeader = [NSDictionary dictionaryWithObjectsAndKeys:oauthData, @"Authorization", nil];

// Add the Authorization header to the request
[request addHeaders:oauthHeader];
} }


- (void)enqueueSignedOperation:(MKNetworkOperation *)op { - (void)enqueueSignedOperation:(MKNetworkOperation *)op {
Expand All @@ -368,6 +421,7 @@ - (void)enqueueSignedOperation:(MKNetworkOperation *)op signOnlyWithOAuthParams:
{ {
// Sign and Enqueue the operation // Sign and Enqueue the operation
[self signRequest:op signOnlyWithOAuthParams:onlyOAuth]; [self signRequest:op signOnlyWithOAuthParams:onlyOAuth];
NSLog(@"%@", [op curlCommandLineString]);
[self enqueueOperation:op]; [self enqueueOperation:op];
} }


Expand Down

0 comments on commit 4afd4e2

Please sign in to comment.