Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

+ Added parameterStyle option to switch between authorization header,

  body and query params style.
  • Loading branch information...
commit 4afd4e20c20c87c53b3ee184a5e48fc89fa24515 1 parent bd042fc
@petercv petercv authored
Showing with 78 additions and 14 deletions.
  1. +10 −0 RSOAuthEngine/RSOAuthEngine.h
  2. +68 −14 RSOAuthEngine/RSOAuthEngine.m
View
10 RSOAuthEngine/RSOAuthEngine.h
@@ -23,6 +23,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
+#import "MKNetworkKit.h"
+
typedef enum _RSOAuthTokenType
{
RSOAuthRequestToken,
@@ -35,6 +37,12 @@ typedef enum _RSOOAuthSignatureMethod {
RSOAuthHMAC_SHA1,
} RSOAuthSignatureMethod;
+typedef enum _RSOAuthParameterStyle {
+ RSOAuthParameterStyleHeader,
+ RSOAuthParameterStylePostBody,
+ RSOAuthParameterStyleQueryString
+} RSOAuthParameterStyle;
+
@interface RSOAuthEngine : MKNetworkEngine
{
@private
@@ -57,6 +65,8 @@ typedef enum _RSOOAuthSignatureMethod {
@property (readonly) NSString *tokenSecret;
@property (readonly) NSString *verifier;
+@property (nonatomic, assign) RSOAuthParameterStyle parameterStyle;
+
- (id)initWithHostName:(NSString *)hostName
customHeaderFields:(NSDictionary *)headers
signatureMethod:(RSOAuthSignatureMethod)signatureMethod
View
82 RSOAuthEngine/RSOAuthEngine.m
@@ -36,6 +36,33 @@
@"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 ()
- (NSString *)signatureBaseStringForRequest:(MKNetworkOperation *)request signOnlyWithOAuthParams:(BOOL)onlyOAuth;
@@ -48,6 +75,8 @@ - (void)setOAuthValue:(NSString *)value forKey:(NSString *)key;
@implementation RSOAuthEngine
+@synthesize parameterStyle = _parameterStyle;
+
#pragma mark - Read-only Properties
- (RSOAuthTokenType)tokenType {
@@ -115,6 +144,8 @@ - (id)initWithHostName:(NSString *)hostName
nil];
[self resetOAuthToken];
+
+ self.parameterStyle = RSOAuthParameterStyleHeader;
}
return self;
@@ -343,21 +374,43 @@ - (void)signRequest:(MKNetworkOperation *)request signOnlyWithOAuthParams:(BOOL)
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
- [_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];
-
- // Add the Authorization header to the request
- [request addHeaders:oauthHeader];
+ NSString *url = request.url;
+ NSString *separator = [url rangeOfString:@"?"].length > 0 ? @"&" : @"?";
+ url = [NSString stringWithFormat:@"%@%@%@", url, separator, [oauthParams componentsJoinedByString:@"&"]];
+ [request rs_setURL:[NSURL URLWithString:url]];
+ }
}
- (void)enqueueSignedOperation:(MKNetworkOperation *)op {
@@ -368,6 +421,7 @@ - (void)enqueueSignedOperation:(MKNetworkOperation *)op signOnlyWithOAuthParams:
{
// Sign and Enqueue the operation
[self signRequest:op signOnlyWithOAuthParams:onlyOAuth];
+ NSLog(@"%@", [op curlCommandLineString]);
[self enqueueOperation:op];
}
Please sign in to comment.
Something went wrong with that request. Please try again.