Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Removed redundant signature methods

Instead of adding a separate method to define if we should include the POST/GET parameters in the signature, decide automatically by checking if there are any files/data to be posted.
  • Loading branch information...
commit a26a8b41fa4a6c5d8b84b4baddc6639fd44f4ee7 1 parent 916259d
@rsieiro authored
Showing with 66 additions and 58 deletions.
  1. +1 −4 RSOAuthEngine/RSOAuthEngine.h
  2. +65 −54 RSOAuthEngine/RSOAuthEngine.m
View
5 RSOAuthEngine/RSOAuthEngine.h
@@ -23,8 +23,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
-#import "MKNetworkKit.h"
-
typedef enum _RSOAuthTokenType
{
RSOAuthRequestToken,
@@ -85,8 +83,7 @@ typedef enum _RSOAuthParameterStyle {
- (NSString *)customValueForKey:(NSString *)key;
- (void)fillTokenWithResponseBody:(NSString *)body type:(RSOAuthTokenType)tokenType;
- (void)setAccessToken:(NSString *)token secret:(NSString *)tokenSecret;
-- (void)signRequest:(MKNetworkOperation *)request signOnlyWithOAuthParams:(BOOL)onlyOAuth;
+- (void)signRequest:(MKNetworkOperation *)request;
- (void)enqueueSignedOperation:(MKNetworkOperation *)op;
-- (void)enqueueSignedOperation:(MKNetworkOperation *)op signOnlyWithOAuthParams:(BOOL)onlyOAuth;
@end
View
119 RSOAuthEngine/RSOAuthEngine.m
@@ -36,10 +36,16 @@
@"HMAC-SHA1",
};
+// This category for MKNetworkOperation was added
+// Because we need access to these fields
+// And they are private inside the class
+
@interface MKNetworkOperation (RSO)
@property (strong, nonatomic) NSMutableURLRequest *request;
@property (strong, nonatomic) NSMutableDictionary *fieldsToBePosted;
+@property (strong, nonatomic) NSMutableArray *filesToBePosted;
+@property (strong, nonatomic) NSMutableArray *dataToBePosted;
- (void)rs_setURL:(NSURL *)URL;
- (void)rs_setValue:(NSString *)value forKey:(NSString *)key;
@@ -50,22 +56,25 @@ @implementation MKNetworkOperation (RSO)
@dynamic request;
@dynamic fieldsToBePosted;
+@dynamic filesToBePosted;
+@dynamic dataToBePosted;
-- (void)rs_setURL:(NSURL *)URL {
+- (void)rs_setURL:(NSURL *)URL
+{
[self.request setURL:URL];
}
-- (void)rs_setValue:(NSString *)value forKey:(NSString *)key {
+- (void)rs_setValue:(NSString *)value forKey:(NSString *)key
+{
[self.fieldsToBePosted setObject:value forKey:key];
}
@end
-
-
@interface RSOAuthEngine ()
-- (NSString *)signatureBaseStringForRequest:(MKNetworkOperation *)request signOnlyWithOAuthParams:(BOOL)onlyOAuth;
+- (NSString *)signatureBaseStringForURL:(NSString *)url method:(NSString *)method parameters:(NSMutableArray *)parameters;
+- (NSString *)signatureBaseStringForRequest:(MKNetworkOperation *)request;
- (NSString *)generatePlaintextSignatureFor:(NSString *)baseString;
- (NSString *)generateHMAC_SHA1SignatureFor:(NSString *)baseString;
- (void)addCustomValue:(NSString *)value withKey:(NSString *)key;
@@ -77,11 +86,13 @@ @implementation RSOAuthEngine
#pragma mark - Read-only Properties
-- (NSString *)consumerKey {
+- (NSString *)consumerKey
+{
return (_oAuthValues) ? [_oAuthValues objectForKey:@"oauth_consumer_key"] : @"";
}
-- (NSString *)token {
+- (NSString *)token
+{
return (_oAuthValues) ? [_oAuthValues objectForKey:@"oauth_token"] : @"";
}
@@ -119,6 +130,7 @@ - (id)initWithHostName:(NSString *)hostName
[self resetOAuthToken];
+ // By default, add the OAuth parameters to the Authorization header
self.parameterStyle = RSOAuthParameterStyleHeader;
}
@@ -141,40 +153,11 @@ - (id)initWithHostName:(NSString *)hostName
#pragma mark - OAuth Signature Generators
-- (NSString *)signatureBaseStringForRequest:(MKNetworkOperation *)request signOnlyWithOAuthParams:(BOOL)onlyOAuth
+- (NSString *)signatureBaseStringForURL:(NSString *)url method:(NSString *)method parameters:(NSMutableArray *)parameters
{
- NSMutableArray *parameters = [NSMutableArray array];
-
- // Get the base URL String (with no parameters)
- NSArray *urlParts = [request.url componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"?#"]];
- NSString *baseURL = [urlParts objectAtIndex:0];
-
- // Add parameters from the query string
- if (!onlyOAuth) {
- NSURL *url = [NSURL URLWithString:request.url];
- NSArray *pairs = [[url.query urlDecodedString] componentsSeparatedByString:@"&"];
- [pairs enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
- NSArray *elements = [obj componentsSeparatedByString:@"="];
- NSString *key = [[elements objectAtIndex:0] urlEncodedString];
- NSString *value = (elements.count > 1) ? [[elements objectAtIndex:1] urlEncodedString] : @"";
-
- [parameters addObject:[NSDictionary dictionaryWithObjectsAndKeys:key, @"key", value, @"value", nil]];
- }];
- }
-
- // Add parameters from the request body
- // Only if we're POSTing, GET parameters were already added
- if (!onlyOAuth && [[[request HTTPMethod] uppercaseString] isEqualToString:@"POST"]) {
- [request.readonlyPostDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
- if ([key isKindOfClass:[NSString class]] && [obj isKindOfClass:[NSString class]]) {
- [parameters addObject:[NSDictionary dictionaryWithObjectsAndKeys:
- [key urlEncodedString],
- @"key",
- [obj urlEncodedString],
- @"value",
- nil]];
- }
- }];
+ // Create a NSMutableArray if not created yet
+ if (!parameters) {
+ parameters = [NSMutableArray arrayWithCapacity:[_oAuthValues count]];
}
// Add parameters from the OAuth header
@@ -200,13 +183,46 @@ - (NSString *)signatureBaseStringForRequest:(MKNetworkOperation *)request signOn
// Create the signature base string
NSString *signatureBaseString = [NSString stringWithFormat:@"%@&%@&%@",
- [[request HTTPMethod] uppercaseString],
- [baseURL urlEncodedString],
+ [method uppercaseString],
+ [url urlEncodedString],
[[normalizedParameters componentsJoinedByString:@"&"] urlEncodedString]];
-
+
return signatureBaseString;
}
+- (NSString *)signatureBaseStringForRequest:(MKNetworkOperation *)request
+{
+ NSMutableArray *parameters = [NSMutableArray array];
+ NSURL *url = [NSURL URLWithString:request.url];
+
+ // Get the base URL String (with no parameters)
+ NSArray *urlParts = [request.url componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"?#"]];
+ NSString *baseURL = [urlParts objectAtIndex:0];
+
+ // Only include GET and POST fields if there are no files or data to be posted
+ if ([request.filesToBePosted count] == 0 && [request.dataToBePosted count] == 0) {
+ // Add parameters from the query string
+ NSArray *pairs = [[url.query urlDecodedString] componentsSeparatedByString:@"&"];
+ [pairs enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
+ NSArray *elements = [obj componentsSeparatedByString:@"="];
+ NSString *key = [[elements objectAtIndex:0] urlEncodedString];
+ NSString *value = (elements.count > 1) ? [[elements objectAtIndex:1] urlEncodedString] : @"";
+
+ [parameters addObject:[NSDictionary dictionaryWithObjectsAndKeys:key, @"key", value, @"value", nil]];
+ }];
+
+ // Add parameters from the request body
+ // Only if we're POSTing, GET parameters were already added
+ if ([[[request HTTPMethod] uppercaseString] isEqualToString:@"POST"]) {
+ [request.readonlyPostDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
+ [parameters addObject:[NSDictionary dictionaryWithObjectsAndKeys:[key urlEncodedString], @"key", [obj urlEncodedString], @"value", nil]];
+ }];
+ }
+ }
+
+ return [self signatureBaseStringForURL:baseURL method:[request HTTPMethod] parameters:parameters];
+}
+
- (NSString *)generatePlaintextSignatureFor:(NSString *)baseString
{
return [NSString stringWithFormat:@"%@&%@",
@@ -327,16 +343,16 @@ - (void)setAccessToken:(NSString *)token secret:(NSString *)tokenSecret
[self setOAuthValue:nil forKey:@"oauth_verifier"];
}
-- (void)signRequest:(MKNetworkOperation *)request signOnlyWithOAuthParams:(BOOL)onlyOAuth
+- (void)signRequest:(MKNetworkOperation *)request
{
NSAssert(_oAuthValues && self.consumerKey && self.consumerSecret, @"Please use an initializer with Consumer Key and Consumer Secret.");
// Generate timestamp and nonce values
- [self setOAuthValue:[NSString stringWithFormat:@"%d", time(NULL)] forKey:@"oauth_timestamp"];
+ [self setOAuthValue:[NSString stringWithFormat:@"%ld", time(NULL)] forKey:@"oauth_timestamp"];
[self setOAuthValue:[NSString uniqueString] forKey:@"oauth_nonce"];
// Construct the signature base string
- NSString *baseString = [self signatureBaseStringForRequest:request signOnlyWithOAuthParams:(BOOL)onlyOAuth];
+ NSString *baseString = [self signatureBaseStringForRequest:request];
// Generate the signature
switch (_signatureMethod) {
@@ -362,14 +378,12 @@ - (void)signRequest:(MKNetworkOperation *)request signOnlyWithOAuthParams:(BOOL)
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];
@@ -387,13 +401,10 @@ - (void)signRequest:(MKNetworkOperation *)request signOnlyWithOAuthParams:(BOOL)
}
}
-- (void)enqueueSignedOperation:(MKNetworkOperation *)op {
- [self enqueueSignedOperation:op signOnlyWithOAuthParams:NO];
-}
-
-- (void)enqueueSignedOperation:(MKNetworkOperation *)op signOnlyWithOAuthParams:(BOOL)onlyOAuth {
+- (void)enqueueSignedOperation:(MKNetworkOperation *)op
+{
// Sign and Enqueue the operation
- [self signRequest:op signOnlyWithOAuthParams:onlyOAuth];
+ [self signRequest:op];
[self enqueueOperation:op];
}
Please sign in to comment.
Something went wrong with that request. Please try again.