Browse files

Added XOAuth support

  • Loading branch information...
1 parent a26a8b4 commit 0156cfdbf91476678f03bfb65fecb05ecd017871 @rsieiro committed Aug 20, 2012
Showing with 44 additions and 0 deletions.
  1. +1 −0 RSOAuthEngine/RSOAuthEngine.h
  2. +43 −0 RSOAuthEngine/RSOAuthEngine.m
View
1 RSOAuthEngine/RSOAuthEngine.h
@@ -85,5 +85,6 @@ typedef enum _RSOAuthParameterStyle {
- (void)setAccessToken:(NSString *)token secret:(NSString *)tokenSecret;
- (void)signRequest:(MKNetworkOperation *)request;
- (void)enqueueSignedOperation:(MKNetworkOperation *)op;
+- (NSString *)generateXOAuthStringForURL:(NSString *)url method:(NSString *)method;
@end
View
43 RSOAuthEngine/RSOAuthEngine.m
@@ -408,4 +408,47 @@ - (void)enqueueSignedOperation:(MKNetworkOperation *)op
[self enqueueOperation:op];
}
+- (NSString *)generateXOAuthStringForURL:(NSString *)url method:(NSString *)method
+{
+ 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:@"%ld", time(NULL)] forKey:@"oauth_timestamp"];
+ [self setOAuthValue:[NSString uniqueString] forKey:@"oauth_nonce"];
+
+ // Construct the signature base string
+ NSString *baseString = [self signatureBaseStringForURL:url method:method parameters:nil];
+
+ // Generate the signature
+ switch (_signatureMethod) {
+ case RSOAuthHMAC_SHA1:
+ [self setOAuthValue:[self generateHMAC_SHA1SignatureFor:baseString] forKey:@"oauth_signature"];
+ break;
+ default:
+ [self setOAuthValue:[self generatePlaintextSignatureFor:baseString] forKey:@"oauth_signature"];
+ break;
+ }
+
+ NSMutableArray *oauthHeaders = [NSMutableArray array];
+
+ // 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 XOAuth String
+ NSString *xOAuthString = [NSString stringWithFormat:@"%@ %@ %@", [method uppercaseString], url, [oauthHeaders componentsJoinedByString:@","]];
+
+ // Base64-encode the string with no line wrap
+ size_t outputLength;
+ NSData *stringData = [xOAuthString dataUsingEncoding:NSUTF8StringEncoding];
+ char *outputBuffer = NewBase64Encode([stringData bytes], [stringData length], false, &outputLength);
+ NSString *finalString = [[NSString alloc] initWithBytes:outputBuffer length:outputLength encoding:NSASCIIStringEncoding];
+ free(outputBuffer);
+
+ return finalString;
+}
+
@end

0 comments on commit 0156cfd

Please sign in to comment.