Permalink
Browse files

Lots of fixes from other projects...

  • Loading branch information...
Gernot committed Nov 28, 2011
1 parent e403fca commit bf835c0523c091d653c02b7a16d7313f26c5a309
@@ -145,7 +145,7 @@ if (theRemainingBytes == 1)
if (theOutIndex % 74 == 72)
{
outOutputData[theOutIndex++] = '\r';
- outOutputData[theOutIndex++] = '\n';
+ outOutputData[theOutIndex] = '\n';
}
}
else if (theRemainingBytes == 2)
@@ -157,7 +157,7 @@ else if (theRemainingBytes == 2)
if (theOutIndex % 74 == 72)
{
outOutputData[theOutIndex++] = '\r';
- outOutputData[theOutIndex++] = '\n';
+ outOutputData[theOutIndex] = '\n';
}
}
return(true);
@@ -31,15 +31,12 @@ @implementation NSMutableURLRequest (OAParameterAdditions)
- (NSArray *)parameters
{
NSString *encodedParameters;
- BOOL shouldfree = NO;
-
if ([[self HTTPMethod] isEqualToString:@"GET"] || [[self HTTPMethod] isEqualToString:@"DELETE"])
encodedParameters = [[self URL] query];
else
{
// POST, PUT
- shouldfree = YES;
- encodedParameters = [[NSString alloc] initWithData:[self HTTPBody] encoding:NSASCIIStringEncoding];
+ encodedParameters = [[[NSString alloc] initWithData:[self HTTPBody] encoding:NSASCIIStringEncoding] autorelease];
}
if ((encodedParameters == nil) || ([encodedParameters isEqualToString:@""]))
@@ -55,10 +52,6 @@ - (NSArray *)parameters
value:[[encodedPairElements objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[requestParameters addObject:parameter];
}
-
- // Cleanup
- if (shouldfree)
- [encodedParameters release];
return [requestParameters autorelease];
}
@@ -77,7 +70,7 @@ - (void)setParameters:(NSArray *)parameters
position++;
}
- if ([[self HTTPMethod] isEqualToString:@"GET"] || [[self HTTPMethod] isEqualToString:@"DELETE"])
+ if ([[self HTTPMethod] isEqualToString:@"GET"] || [[self HTTPMethod] isEqualToString:@"HEAD"] || [[self HTTPMethod] isEqualToString:@"DELETE"])
[self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@", [[self URL] URLStringWithoutQuery], encodedParameterPairs]]];
else
{
@@ -40,6 +40,5 @@
- (id)initWithRequest:(OAMutableURLRequest *)aRequest delegate:(id)aDelegate didFinishSelector:(SEL)finishSelector didFailSelector:(SEL)failSelector;
- (void)start;
-- (void)cancel;
@end
@@ -52,8 +52,10 @@ - (void)start
if (connection)
[connection release];
- connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
-
+ connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
+ [connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
+ [connection start];
+
if (connection)
{
if (responseData)
@@ -75,11 +77,7 @@ - (void)start
- (void)cancel
{
if (connection)
- {
[connection cancel];
- [connection release];
- connection = nil;
- }
}
- (void)dealloc
@@ -92,7 +90,7 @@ - (void)dealloc
}
#pragma mark -
-#pragma mark NSURLConnection methods
+#pragma mark NSURLConnection's delegate methods
- (void)connection:(NSURLConnection *)aConnection didReceiveResponse:(NSURLResponse *)aResponse
{
@@ -132,3 +130,4 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)aConnection
}
@end
+
View
@@ -31,10 +31,12 @@
@protected
NSString *key;
NSString *secret;
+ NSString *callbackURL;
}
@property(retain) NSString *key;
@property(retain) NSString *secret;
+@property(retain) NSString *callbackURL;
-- (id)initWithKey:(NSString *)aKey secret:(NSString *)aSecret;
+- (id)initWithKey:(NSString *)aKey secret:(NSString *)aSecret callbackURL:(NSString *)aCallbackURL;
@end
View
@@ -27,16 +27,17 @@
@implementation OAConsumer
-@synthesize key, secret;
+@synthesize key, secret, callbackURL;
#pragma mark init
-- (id)initWithKey:(NSString *)aKey secret:(NSString *)aSecret
+- (id)initWithKey:(NSString *)aKey secret:(NSString *)aSecret callbackURL:(NSString *)aCallbackURL
{
if (self = [super init])
{
self.key = aKey;
self.secret = aSecret;
+ self.callbackURL = aCallbackURL;
}
return self;
}
@@ -45,6 +46,7 @@ - (void)dealloc
{
[key release];
[secret release];
+ [callbackURL release];
[super dealloc];
}
View
@@ -32,7 +32,6 @@
@private
OAMutableURLRequest *request;
NSURLResponse *response;
- NSURLConnection *connection;
NSError *error;
NSData *responseData;
id delegate;
View
@@ -52,13 +52,15 @@ - (void)fetchDataWithRequest:(OAMutableURLRequest *)aRequest
[delegate performSelector:didFailSelector
withObject:ticket
withObject:error];
+ [ticket release];
} else {
OAServiceTicket *ticket = [[OAServiceTicket alloc] initWithRequest:request
response:response
didSucceed:[(NSHTTPURLResponse *)response statusCode] < 400];
[delegate performSelector:didFinishSelector
withObject:ticket
withObject:responseData];
+ [ticket release];
}
}
@@ -25,6 +25,7 @@
#import "OAHMAC_SHA1SignatureProvider.h"
+
#import <CommonCrypto/CommonHMAC.h>
#include "Base64Transcoder.h"
@@ -38,16 +39,21 @@ - (NSString *)name
- (NSString *)signClearText:(NSString *)text withSecret:(NSString *)secret
{
- NSData *secretData = [secret dataUsingEncoding:NSUTF8StringEncoding];
+ NSData *secretData = [secret dataUsingEncoding:NSUTF8StringEncoding];
NSData *clearTextData = [text dataUsingEncoding:NSUTF8StringEncoding];
- unsigned char result[20];
- CCHmac(kCCHmacAlgSHA1, [secretData bytes], [secretData length], [clearTextData bytes], [clearTextData length], result);
-
+
+ uint8_t digest[CC_SHA1_DIGEST_LENGTH] = {0};
+
+ CCHmacContext hmacContext;
+ CCHmacInit(&hmacContext, kCCHmacAlgSHA1, secretData.bytes, secretData.length);
+ CCHmacUpdate(&hmacContext, clearTextData.bytes, clearTextData.length);
+ CCHmacFinal(&hmacContext, digest);
+
//Base64 Encoding
char base64Result[32];
size_t theResultLength = 32;
- Base64EncodeData(result, 20, base64Result, &theResultLength);
+ Base64EncodeData(digest, CC_SHA1_DIGEST_LENGTH, base64Result, &theResultLength);
NSData *theData = [NSData dataWithBytes:base64Result length:theResultLength];
NSString *base64EncodedResult = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];
View
@@ -42,7 +42,6 @@
id<OASignatureProviding> signatureProvider;
NSString *nonce;
NSString *timestamp;
- NSMutableDictionary *extraOAuthParameters;
}
@property(readonly) NSString *signature;
@property(readonly) NSString *nonce;
@@ -63,6 +62,4 @@ signatureProvider:(id<OASignatureProviding, NSObject>)aProvider
- (void)prepare;
-- (void)setOAuthParameterName:(NSString*)parameterName withValue:(NSString*)parameterValue;
-
@end
View
@@ -46,7 +46,7 @@ - (id)initWithURL:(NSURL *)aUrl
{
if (self = [super initWithURL:aUrl
cachePolicy:NSURLRequestReloadIgnoringCacheData
- timeoutInterval:10.0])
+ timeoutInterval:60.0])
{
consumer = [aConsumer retain];
@@ -120,24 +120,12 @@ - (void)dealloc
[signatureProvider release];
[timestamp release];
[nonce release];
- [extraOAuthParameters release];
[super dealloc];
}
#pragma mark -
#pragma mark Public
-- (void)setOAuthParameterName:(NSString*)parameterName withValue:(NSString*)parameterValue
-{
- assert(parameterName && parameterValue);
-
- if (extraOAuthParameters == nil) {
- extraOAuthParameters = [NSMutableDictionary new];
- }
-
- [extraOAuthParameters setObject:parameterValue forKey:parameterName];
-}
-
- (void)prepare
{
// sign
@@ -152,19 +140,20 @@ - (void)prepare
NSString *oauthToken;
if ([token.key isEqualToString:@""])
oauthToken = @""; // not used on Request Token transactions
- else
+ else if (!token.verifier || [token.verifier isEqualToString:@""])
oauthToken = [NSString stringWithFormat:@"oauth_token=\"%@\", ", [token.key URLEncodedString]];
+ else
+ oauthToken = [NSString stringWithFormat:@"oauth_token=\"%@\", oauth_verifier=\"%@\", ",
+ [token.key URLEncodedString],
+ [token.verifier URLEncodedString]];
- NSMutableString *extraParameters = [NSMutableString string];
+ NSString *callbackURL = @"";
+ if ([token.key isEqualToString:@""]
+ && consumer.callbackURL
+ && ![consumer.callbackURL isEqualToString:@""]) {
+ callbackURL = [NSString stringWithFormat:@", oauth_callback=\"%@\"", consumer.callbackURL];
+ }
- // Adding the optional parameters in sorted order isn't required by the OAuth spec, but it makes it possible to hard-code expected values in the unit tests.
- for(NSString *parameterName in [[extraOAuthParameters allKeys] sortedArrayUsingSelector:@selector(compare:)])
- {
- [extraParameters appendFormat:@", %@=\"%@\"",
- [parameterName URLEncodedString],
- [[extraOAuthParameters objectForKey:parameterName] URLEncodedString]];
- }
-
NSString *oauthHeader = [NSString stringWithFormat:@"OAuth realm=\"%@\", oauth_consumer_key=\"%@\", %@oauth_signature_method=\"%@\", oauth_signature=\"%@\", oauth_timestamp=\"%@\", oauth_nonce=\"%@\", oauth_version=\"1.0\"%@",
[realm URLEncodedString],
[consumer.key URLEncodedString],
@@ -173,7 +162,7 @@ - (void)prepare
[signature URLEncodedString],
timestamp,
nonce,
- extraParameters];
+ callbackURL];
[self setValue:oauthHeader forHTTPHeaderField:@"Authorization"];
}
@@ -187,10 +176,12 @@ - (void)_generateTimestamp
}
- (void)_generateNonce
-{
+{
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
- NSMakeCollectable(theUUID);
+ CFRelease(theUUID);
+ if (nonce)
+ [nonce release];
nonce = (NSString *)string;
}
@@ -208,7 +199,16 @@ - (NSString *)_signatureBaseString
if (![token.key isEqualToString:@""]) {
[parameterPairs addObject:[[OARequestParameter requestParameterWithName:@"oauth_token" value:token.key] URLEncodedNameValuePair]];
+ if (![token.verifier isEqualToString:@""]) {
+ [parameterPairs addObject:[[OARequestParameter requestParameterWithName:@"oauth_verifier" value:token.verifier] URLEncodedNameValuePair]];
+ }
}
+
+ if ([token.key isEqualToString:@""]
+ && consumer.callbackURL
+ && ![consumer.callbackURL isEqualToString:@""]) {
+ [parameterPairs addObject:[[OARequestParameter requestParameterWithName:@"oauth_callback" value:consumer.callbackURL] URLEncodedNameValuePair]];
+ }
for (OARequestParameter *param in [self parameters]) {
[parameterPairs addObject:[param URLEncodedNameValuePair]];
View
@@ -29,11 +29,13 @@
@protected
NSString *key;
NSString *secret;
+ NSString *verifier;
}
@property(retain) NSString *key;
@property(retain) NSString *secret;
+@property(retain) NSString *verifier;
-- (id)initWithKey:(NSString *)aKey secret:(NSString *)aSecret;
+- (id)initWithKey:(NSString *)aKey secret:(NSString *)aSecret verifier:(NSString *)aVerifier;
- (id)initWithUserDefaultsUsingServiceProviderName:(NSString *)provider prefix:(NSString *)prefix;
- (id)initWithHTTPResponseBody:(NSString *)body;
- (int)storeInUserDefaultsWithServiceProviderName:(NSString *)provider prefix:(NSString *)prefix;
Oops, something went wrong.

0 comments on commit bf835c0

Please sign in to comment.