From dcdc5807ade4ad33ee526f7c55f0f211c5111a0f Mon Sep 17 00:00:00 2001 From: Erik Aigner Date: Fri, 15 Jul 2011 00:16:31 +0200 Subject: [PATCH 1/2] ported OAuthConsumer --- FoundationKit.xcodeproj/project.pbxproj | 66 ++++++- Sources/NSData+NKBase64.h | 13 ++ Sources/NSData+NKBase64.m | 122 ++++++++++++ Sources/NSString+NKAdditions.h | 3 + Sources/NSString+NKAdditions.m | 16 ++ Sources/OAuth/NKOAuthConsumer.h | 16 ++ Sources/OAuth/NKOAuthConsumer.m | 23 +++ Sources/OAuth/NKOAuthDataFetcher.h | 18 ++ Sources/OAuth/NKOAuthDataFetcher.m | 87 +++++++++ .../OAuth/NKOAuthHMACSHA1SignatureProvider.h | 10 + .../OAuth/NKOAuthHMACSHA1SignatureProvider.m | 19 ++ Sources/OAuth/NKOAuthMutableURLRequest.h | 21 ++ Sources/OAuth/NKOAuthMutableURLRequest.m | 179 ++++++++++++++++++ .../OAuth/NKOAuthPlaintextSignatureProvider.h | 10 + .../OAuth/NKOAuthPlaintextSignatureProvider.m | 14 ++ Sources/OAuth/NKOAuthRequestParameter.h | 20 ++ Sources/OAuth/NKOAuthRequestParameter.m | 40 ++++ Sources/OAuth/NKOAuthServiceTicket.h | 19 ++ Sources/OAuth/NKOAuthServiceTicket.m | 31 +++ Sources/OAuth/NKOAuthSignatureProvider.h | 13 ++ Sources/OAuth/NKOAuthToken.h | 23 +++ Sources/OAuth/NKOAuthToken.m | 92 +++++++++ 22 files changed, 853 insertions(+), 2 deletions(-) create mode 100755 Sources/NSData+NKBase64.h create mode 100755 Sources/NSData+NKBase64.m create mode 100755 Sources/OAuth/NKOAuthConsumer.h create mode 100755 Sources/OAuth/NKOAuthConsumer.m create mode 100755 Sources/OAuth/NKOAuthDataFetcher.h create mode 100755 Sources/OAuth/NKOAuthDataFetcher.m create mode 100755 Sources/OAuth/NKOAuthHMACSHA1SignatureProvider.h create mode 100755 Sources/OAuth/NKOAuthHMACSHA1SignatureProvider.m create mode 100755 Sources/OAuth/NKOAuthMutableURLRequest.h create mode 100755 Sources/OAuth/NKOAuthMutableURLRequest.m create mode 100755 Sources/OAuth/NKOAuthPlaintextSignatureProvider.h create mode 100755 Sources/OAuth/NKOAuthPlaintextSignatureProvider.m create mode 100755 Sources/OAuth/NKOAuthRequestParameter.h create mode 100755 Sources/OAuth/NKOAuthRequestParameter.m create mode 100755 Sources/OAuth/NKOAuthServiceTicket.h create mode 100755 Sources/OAuth/NKOAuthServiceTicket.m create mode 100755 Sources/OAuth/NKOAuthSignatureProvider.h create mode 100755 Sources/OAuth/NKOAuthToken.h create mode 100755 Sources/OAuth/NKOAuthToken.m diff --git a/FoundationKit.xcodeproj/project.pbxproj b/FoundationKit.xcodeproj/project.pbxproj index fc89ae1..3a3d40f 100644 --- a/FoundationKit.xcodeproj/project.pbxproj +++ b/FoundationKit.xcodeproj/project.pbxproj @@ -9,7 +9,6 @@ /* Begin PBXBuildFile section */ 399F4CD413C905C70008CC0E /* NSObject+NKAssociatedObjects.m in Sources */ = {isa = PBXBuildFile; fileRef = 399F4CD313C905C70008CC0E /* NSObject+NKAssociatedObjects.m */; }; 39EA898C13C98C6C00B6427F /* NKMath.m in Sources */ = {isa = PBXBuildFile; fileRef = 39EA898B13C98C6C00B6427F /* NKMath.m */; }; - C582BF8C13C8B77C0012DF67 /* NKLog.m in Sources */ = {isa = PBXBuildFile; fileRef = C582BF8A13C8B77C0012DF67 /* NKLog.m */; }; C5AAAA7613C9A32A00E44CE7 /* NSStringAdditionsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = C5AAAA7313C9A31A00E44CE7 /* NSStringAdditionsTests.m */; }; C5AAAA7913C9A33F00E44CE7 /* NSString+NKAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = C5AAAA7813C9A33F00E44CE7 /* NSString+NKAdditions.m */; }; C5AAAA7A13C9A37A00E44CE7 /* libFoundationKit.a in Frameworks */ = {isa = PBXBuildFile; fileRef = DC410CFF13C39BFD00C90C19 /* libFoundationKit.a */; }; @@ -24,6 +23,15 @@ C5E31A0913C4EA7E00D9CA76 /* NSMutableDictionary+NKBlocks.m in Sources */ = {isa = PBXBuildFile; fileRef = C5E31A0113C4EA7D00D9CA76 /* NSMutableDictionary+NKBlocks.m */; }; C5EF038913C98ECE00953F2B /* NKSynthesizeSingletonTests.m in Sources */ = {isa = PBXBuildFile; fileRef = C5EF038613C98EC100953F2B /* NKSynthesizeSingletonTests.m */; }; DC410D0313C39BFD00C90C19 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DC410D0213C39BFD00C90C19 /* Foundation.framework */; }; + DC5A1FD513CF8A370023CCC0 /* NKOAuthConsumer.m in Sources */ = {isa = PBXBuildFile; fileRef = DC5A1FC513CF8A370023CCC0 /* NKOAuthConsumer.m */; }; + DC5A1FD613CF8A370023CCC0 /* NKOAuthDataFetcher.m in Sources */ = {isa = PBXBuildFile; fileRef = DC5A1FC713CF8A370023CCC0 /* NKOAuthDataFetcher.m */; }; + DC5A1FD713CF8A370023CCC0 /* NKOAuthHMACSHA1SignatureProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = DC5A1FC913CF8A370023CCC0 /* NKOAuthHMACSHA1SignatureProvider.m */; }; + DC5A1FD813CF8A370023CCC0 /* NKOAuthMutableURLRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = DC5A1FCB13CF8A370023CCC0 /* NKOAuthMutableURLRequest.m */; }; + DC5A1FD913CF8A370023CCC0 /* NKOAuthPlaintextSignatureProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = DC5A1FCD13CF8A370023CCC0 /* NKOAuthPlaintextSignatureProvider.m */; }; + DC5A1FDA13CF8A370023CCC0 /* NKOAuthRequestParameter.m in Sources */ = {isa = PBXBuildFile; fileRef = DC5A1FCF13CF8A370023CCC0 /* NKOAuthRequestParameter.m */; }; + DC5A1FDB13CF8A370023CCC0 /* NKOAuthServiceTicket.m in Sources */ = {isa = PBXBuildFile; fileRef = DC5A1FD113CF8A370023CCC0 /* NKOAuthServiceTicket.m */; }; + DC5A1FDC13CF8A370023CCC0 /* NKOAuthToken.m in Sources */ = {isa = PBXBuildFile; fileRef = DC5A1FD413CF8A370023CCC0 /* NKOAuthToken.m */; }; + DC5A1FE013CF93F00023CCC0 /* NSData+NKBase64.m in Sources */ = {isa = PBXBuildFile; fileRef = DC5A1FDF13CF93F00023CCC0 /* NSData+NKBase64.m */; }; DCB0127213C4AE9400AB692F /* NSError+NKAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = DCB0127013C4AE9400AB692F /* NSError+NKAdditions.m */; }; DCF68FBF13C74ADB0057517A /* NSObject+NKSwizzle.m in Sources */ = {isa = PBXBuildFile; fileRef = DCF68FBD13C74ADB0057517A /* NSObject+NKSwizzle.m */; }; /* End PBXBuildFile section */ @@ -82,6 +90,25 @@ DC1CF43013C484B800E5BEBC /* NSSet+NKConcise.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSSet+NKConcise.h"; sourceTree = ""; }; DC410CFF13C39BFD00C90C19 /* libFoundationKit.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libFoundationKit.a; sourceTree = BUILT_PRODUCTS_DIR; }; DC410D0213C39BFD00C90C19 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; }; + DC5A1FC413CF8A370023CCC0 /* NKOAuthConsumer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NKOAuthConsumer.h; sourceTree = ""; }; + DC5A1FC513CF8A370023CCC0 /* NKOAuthConsumer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NKOAuthConsumer.m; sourceTree = ""; }; + DC5A1FC613CF8A370023CCC0 /* NKOAuthDataFetcher.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NKOAuthDataFetcher.h; sourceTree = ""; }; + DC5A1FC713CF8A370023CCC0 /* NKOAuthDataFetcher.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NKOAuthDataFetcher.m; sourceTree = ""; }; + DC5A1FC813CF8A370023CCC0 /* NKOAuthHMACSHA1SignatureProvider.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NKOAuthHMACSHA1SignatureProvider.h; sourceTree = ""; }; + DC5A1FC913CF8A370023CCC0 /* NKOAuthHMACSHA1SignatureProvider.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NKOAuthHMACSHA1SignatureProvider.m; sourceTree = ""; }; + DC5A1FCA13CF8A370023CCC0 /* NKOAuthMutableURLRequest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NKOAuthMutableURLRequest.h; sourceTree = ""; }; + DC5A1FCB13CF8A370023CCC0 /* NKOAuthMutableURLRequest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NKOAuthMutableURLRequest.m; sourceTree = ""; }; + DC5A1FCC13CF8A370023CCC0 /* NKOAuthPlaintextSignatureProvider.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NKOAuthPlaintextSignatureProvider.h; sourceTree = ""; }; + DC5A1FCD13CF8A370023CCC0 /* NKOAuthPlaintextSignatureProvider.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NKOAuthPlaintextSignatureProvider.m; sourceTree = ""; }; + DC5A1FCE13CF8A370023CCC0 /* NKOAuthRequestParameter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NKOAuthRequestParameter.h; sourceTree = ""; }; + DC5A1FCF13CF8A370023CCC0 /* NKOAuthRequestParameter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NKOAuthRequestParameter.m; sourceTree = ""; }; + DC5A1FD013CF8A370023CCC0 /* NKOAuthServiceTicket.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NKOAuthServiceTicket.h; sourceTree = ""; }; + DC5A1FD113CF8A370023CCC0 /* NKOAuthServiceTicket.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NKOAuthServiceTicket.m; sourceTree = ""; }; + DC5A1FD213CF8A370023CCC0 /* NKOAuthSignatureProvider.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NKOAuthSignatureProvider.h; sourceTree = ""; }; + DC5A1FD313CF8A370023CCC0 /* NKOAuthToken.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NKOAuthToken.h; sourceTree = ""; }; + DC5A1FD413CF8A370023CCC0 /* NKOAuthToken.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NKOAuthToken.m; sourceTree = ""; }; + DC5A1FDE13CF93F00023CCC0 /* NSData+NKBase64.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSData+NKBase64.h"; sourceTree = ""; }; + DC5A1FDF13CF93F00023CCC0 /* NSData+NKBase64.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSData+NKBase64.m"; sourceTree = ""; }; DCB0126F13C4AE9400AB692F /* NSError+NKAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSError+NKAdditions.h"; sourceTree = ""; }; DCB0127013C4AE9400AB692F /* NSError+NKAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSError+NKAdditions.m"; sourceTree = ""; }; DCF68FBC13C74ADB0057517A /* NSObject+NKSwizzle.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSObject+NKSwizzle.h"; sourceTree = ""; }; @@ -128,6 +155,7 @@ C5E319C913C473BD00D9CA76 /* Sources */ = { isa = PBXGroup; children = ( + DC5A1FC213CF8A220023CCC0 /* OAuth */, C5E319F513C4E90B00D9CA76 /* Audio */, C5E319D713C474FE00D9CA76 /* FoundationKit.h */, C582BF8913C8B77C0012DF67 /* NKLog.h */, @@ -137,6 +165,8 @@ C5E319FA13C4EA7C00D9CA76 /* NSArray+NKBlocks.h */, C5E319FB13C4EA7C00D9CA76 /* NSArray+NKBlocks.m */, DC1CF42313C483BC00E5BEBC /* NSArray+NKConcise.h */, + DC5A1FDE13CF93F00023CCC0 /* NSData+NKBase64.h */, + DC5A1FDF13CF93F00023CCC0 /* NSData+NKBase64.m */, C5E319D113C4744800D9CA76 /* NSData+NKCrypto.h */, C5E319D213C4744800D9CA76 /* NSData+NKCrypto.m */, C5E319FC13C4EA7C00D9CA76 /* NSDictionary+NKBlocks.h */, @@ -213,6 +243,30 @@ name = Frameworks; sourceTree = ""; }; + DC5A1FC213CF8A220023CCC0 /* OAuth */ = { + isa = PBXGroup; + children = ( + DC5A1FC413CF8A370023CCC0 /* NKOAuthConsumer.h */, + DC5A1FC513CF8A370023CCC0 /* NKOAuthConsumer.m */, + DC5A1FC613CF8A370023CCC0 /* NKOAuthDataFetcher.h */, + DC5A1FC713CF8A370023CCC0 /* NKOAuthDataFetcher.m */, + DC5A1FC813CF8A370023CCC0 /* NKOAuthHMACSHA1SignatureProvider.h */, + DC5A1FC913CF8A370023CCC0 /* NKOAuthHMACSHA1SignatureProvider.m */, + DC5A1FCA13CF8A370023CCC0 /* NKOAuthMutableURLRequest.h */, + DC5A1FCB13CF8A370023CCC0 /* NKOAuthMutableURLRequest.m */, + DC5A1FCC13CF8A370023CCC0 /* NKOAuthPlaintextSignatureProvider.h */, + DC5A1FCD13CF8A370023CCC0 /* NKOAuthPlaintextSignatureProvider.m */, + DC5A1FCE13CF8A370023CCC0 /* NKOAuthRequestParameter.h */, + DC5A1FCF13CF8A370023CCC0 /* NKOAuthRequestParameter.m */, + DC5A1FD013CF8A370023CCC0 /* NKOAuthServiceTicket.h */, + DC5A1FD113CF8A370023CCC0 /* NKOAuthServiceTicket.m */, + DC5A1FD213CF8A370023CCC0 /* NKOAuthSignatureProvider.h */, + DC5A1FD313CF8A370023CCC0 /* NKOAuthToken.h */, + DC5A1FD413CF8A370023CCC0 /* NKOAuthToken.m */, + ); + path = OAuth; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -323,10 +377,18 @@ C5E31A0713C4EA7E00D9CA76 /* NSMutableArray+NKBlocks.m in Sources */, C5E31A0913C4EA7E00D9CA76 /* NSMutableDictionary+NKBlocks.m in Sources */, DCF68FBF13C74ADB0057517A /* NSObject+NKSwizzle.m in Sources */, - C582BF8C13C8B77C0012DF67 /* NKLog.m in Sources */, 399F4CD413C905C70008CC0E /* NSObject+NKAssociatedObjects.m in Sources */, 39EA898C13C98C6C00B6427F /* NKMath.m in Sources */, C5AAAA7913C9A33F00E44CE7 /* NSString+NKAdditions.m in Sources */, + DC5A1FD513CF8A370023CCC0 /* NKOAuthConsumer.m in Sources */, + DC5A1FD613CF8A370023CCC0 /* NKOAuthDataFetcher.m in Sources */, + DC5A1FD713CF8A370023CCC0 /* NKOAuthHMACSHA1SignatureProvider.m in Sources */, + DC5A1FD813CF8A370023CCC0 /* NKOAuthMutableURLRequest.m in Sources */, + DC5A1FD913CF8A370023CCC0 /* NKOAuthPlaintextSignatureProvider.m in Sources */, + DC5A1FDA13CF8A370023CCC0 /* NKOAuthRequestParameter.m in Sources */, + DC5A1FDB13CF8A370023CCC0 /* NKOAuthServiceTicket.m in Sources */, + DC5A1FDC13CF8A370023CCC0 /* NKOAuthToken.m in Sources */, + DC5A1FE013CF93F00023CCC0 /* NSData+NKBase64.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Sources/NSData+NKBase64.h b/Sources/NSData+NKBase64.h new file mode 100755 index 0000000..ae37e12 --- /dev/null +++ b/Sources/NSData+NKBase64.h @@ -0,0 +1,13 @@ +// Part of FoundationKit http://foundationk.it +// +// Code optimized and adapted from http://www.cocoadev.com/index.pl?BaseSixtyFour + +#import + + +@interface NSData (NKBase64) + ++ (NSData *)dataWithBase64String:(NSString *)string; +- (NSString *)base64String; + +@end diff --git a/Sources/NSData+NKBase64.m b/Sources/NSData+NKBase64.m new file mode 100755 index 0000000..6d3a76e --- /dev/null +++ b/Sources/NSData+NKBase64.m @@ -0,0 +1,122 @@ +#import "NSData+NKBase64.h" + + +static const char _encode64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +@implementation NSData (NKBase64) + ++ (NSData *)dataWithBase64String:(NSString *)string { + if ([string length] <= 0) { + return [NSData data]; + } + + // Build decoding table + char decode64[256]; + memset(decode64, CHAR_MAX, 256); + for (int i=0; i<64; i++) { + decode64[(short)_encode64[i]] = i; + } + + // Get data from string and init buffer + const char *bytes = [string UTF8String]; + NSUInteger numBytes = [string length]; + + NSUInteger dataLen = ((numBytes + 3) / 4) * 3; + NSUInteger dataOffset = 0; + char data[dataLen]; + + NSUInteger i = 0; + + while (YES) { + char buffer[4]; + short bufferOffset; + + for (bufferOffset = 0; bufferOffset < 4; i++) { + short c = bytes[i]; + if (c == '\0') { + break; + } + + else if (isspace(c) || c == '=') { + continue; + } + + buffer[bufferOffset] = decode64[c]; + if (buffer[bufferOffset++] == CHAR_MAX) { + return nil; + } + } + + if (bufferOffset == 0) { + break; + } + else if(bufferOffset == 1) { + return nil; + } + + // Decode characters to bytes + data[dataOffset++] = (buffer[0] << 2) | (buffer[1] >> 4); + if (bufferOffset > 2) { + data[dataOffset++] = (buffer[1] << 4) | (buffer[2] >> 2); + } + if (bufferOffset > 3) { + data[dataOffset++] = (buffer[2] << 6) | buffer[3]; + } + } + + return [NSData dataWithBytes:data length:dataLen]; +} + +- (NSString *)base64String { + if ([self length] <= 0) { + return @""; + } + + // Get data bytes and length + char *bytes = (char *)[self bytes]; + NSUInteger numBytes = [self length]; + + // Output buffer + NSUInteger offset = 0; + NSUInteger outputLen = ((numBytes + 2) / 3) * 4; + char output[outputLen]; + + // Convert data + NSUInteger i = 0; + + while (i < numBytes) { + char buffer[3] = {0}; + short bufferLen = 0; + + while (bufferLen < 3 && i < numBytes) { + buffer[bufferLen++] = bytes[i++]; + } + + // Encode bytes to characters + output[offset++] = _encode64[(buffer[0] & 0xFC) >> 2]; + output[offset++] = _encode64[((buffer[0] & 0x03) << 4) | ((buffer[1] & 0xF0) >> 4)]; + + if (bufferLen > 1) { + output[offset++] = _encode64[((buffer[1] & 0x0F) << 2) | ((buffer[2] & 0xC0) >> 6)]; + } + else { + output[offset++] = '='; + } + + if (bufferLen > 2) { + output[offset++] = _encode64[buffer[2] & 0x3F]; + } + else { + output[offset++] = '='; + } + } + + // Generate output string + NSString *encoded = [[NSString alloc] initWithBytes:output + length:offset + encoding:NSASCIIStringEncoding]; + + return encoded; +} + +@end diff --git a/Sources/NSString+NKAdditions.h b/Sources/NSString+NKAdditions.h index a187fe7..4f2dfb0 100644 --- a/Sources/NSString+NKAdditions.h +++ b/Sources/NSString+NKAdditions.h @@ -9,4 +9,7 @@ - (NSRange)stringRange; - (NSString *)trimmed; +- (NSString *)URLEncodedString; +- (NSString *)URLEncodedStringEscapingAllCharacters; + @end diff --git a/Sources/NSString+NKAdditions.m b/Sources/NSString+NKAdditions.m index b7c9eea..485230e 100644 --- a/Sources/NSString+NKAdditions.m +++ b/Sources/NSString+NKAdditions.m @@ -26,4 +26,20 @@ - (NSString *)trimmed { return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; } +- (NSString *)URLEncodedString { + return CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, + (__bridge CFStringRef)self, + NULL, + CFSTR("?=&+"), + kCFStringEncodingUTF8)); +} + +- (NSString *)URLEncodedStringEscapingAllCharacters { + return CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, + (__bridge CFStringRef)self, + NULL, + CFSTR(":/=,!$&'()*+;[]@#?"), + kCFStringEncodingUTF8)); +} + @end diff --git a/Sources/OAuth/NKOAuthConsumer.h b/Sources/OAuth/NKOAuthConsumer.h new file mode 100755 index 0000000..44d85ad --- /dev/null +++ b/Sources/OAuth/NKOAuthConsumer.h @@ -0,0 +1,16 @@ +// Part of FoundationKit http://foundationk.it +// +// Derived from Jon Crosby's MIT-licensed OAuthConsumer: http://oauth.googlecode.com/svn/code/obj-c/OAuthConsumer/ + +#import + + +@interface NKOAuthConsumer : NSObject { +} +@property (copy, readwrite) NSString *key; +@property (copy, readwrite) NSString *secret; + ++ (id)consumerWithKey:(NSString *)key secret:(NSString *)secret; +- (id)initWithKey:(NSString *)key secret:(NSString *)secret; + +@end diff --git a/Sources/OAuth/NKOAuthConsumer.m b/Sources/OAuth/NKOAuthConsumer.m new file mode 100755 index 0000000..bebcfc3 --- /dev/null +++ b/Sources/OAuth/NKOAuthConsumer.m @@ -0,0 +1,23 @@ +#import "NKOAuthConsumer.h" + + +@implementation NKOAuthConsumer +@synthesize key = key_; +@synthesize secret = secret_; + ++ (id)consumerWithKey:(NSString *)key secret:(NSString *)secret { + return [[NKOAuthConsumer alloc] initWithKey:key secret:secret]; +} + +- (id)initWithKey:(NSString *)key secret:(NSString *)secret { + self = [super init]; + if (self) { + self.key = key; + self.secret = secret; + } + return self; +} + + + +@end diff --git a/Sources/OAuth/NKOAuthDataFetcher.h b/Sources/OAuth/NKOAuthDataFetcher.h new file mode 100755 index 0000000..cce999d --- /dev/null +++ b/Sources/OAuth/NKOAuthDataFetcher.h @@ -0,0 +1,18 @@ +// Part of FoundationKit http://foundationk.it +// +// Derived from Jon Crosby's MIT-licensed OAuthConsumer: http://oauth.googlecode.com/svn/code/obj-c/OAuthConsumer/ + +#import +#import "NKOAuthMutableURLRequest.h" +#import "NKOAuthServiceTicket.h" + + +@interface NKOAuthDataFetcher : NSObject { +} + ++ (id)fetcherWithRequest:(NKOAuthMutableURLRequest *)request delegate:(id)dlg didFinishSelector:(SEL)finSlc didFailSelector:(SEL)failSlc; + +- (void)fetchDataWithRequest:(NKOAuthMutableURLRequest *)request delegate:(id)dlg didFinishSelector:(SEL)finSlc didFailSelector:(SEL)failSlc; +- (void)fetchData; + +@end diff --git a/Sources/OAuth/NKOAuthDataFetcher.m b/Sources/OAuth/NKOAuthDataFetcher.m new file mode 100755 index 0000000..3bc4426 --- /dev/null +++ b/Sources/OAuth/NKOAuthDataFetcher.m @@ -0,0 +1,87 @@ +#import "NKOAuthDataFetcher.h" + +@interface NKOAuthDataFetcher () +@property (nonatomic, strong) NSURLResponse *response; +@property (nonatomic, strong) NSMutableData *responseData; +@property (nonatomic, strong) NSURLConnection *connection; +@property (nonatomic, strong) NKOAuthMutableURLRequest *request; +@property (nonatomic, assign) id delegate; +@property (nonatomic, assign) SEL didFinishSelector; +@property (nonatomic, assign) SEL didFailSelector; +@end + + +@implementation NKOAuthDataFetcher +@synthesize connection = connection_; +@synthesize response = response_; +@synthesize responseData = responseData_; +@synthesize request = request_; +@synthesize delegate = delegate_; +@synthesize didFinishSelector = didFinishSelector_; +@synthesize didFailSelector = didFailSelector_; + ++ (id)fetcherWithRequest:(NKOAuthMutableURLRequest *)request delegate:(id)dlg didFinishSelector:(SEL)finSlc didFailSelector:(SEL)failSlc { + NKOAuthDataFetcher *fetcher = [NKOAuthDataFetcher new]; + fetcher.request = request; + fetcher.delegate = dlg; + fetcher.didFinishSelector = finSlc; + fetcher.didFailSelector = failSlc; + return fetcher; +} + +- (void)fetchDataWithRequest:(NKOAuthMutableURLRequest *)request delegate:(id)dlg didFinishSelector:(SEL)finSlc didFailSelector:(SEL)failSlc { + self.request = request; + self.delegate = dlg; + self.didFinishSelector = finSlc; + self.didFailSelector = failSlc; + [self fetchData]; +} + +- (void)fetchData { + [request_ prepare]; + + self.connection = [NSURLConnection connectionWithRequest:request_ delegate:self]; + if (connection_) { + self.responseData = [NSMutableData data]; + } else { + NKOAuthServiceTicket *ticket = [[NKOAuthServiceTicket alloc] initWithRequest:request_ response:nil didSucceed:NO]; + [delegate_ performSelector:didFailSelector_ withObject:ticket withObject:nil]; + } +} + +- (void)dealloc { + self.connection = nil; + self.responseData = nil; + self.response = nil; + self.request = nil; +} + +- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { + self.response = response; + [responseData_ setLength:0]; +} + +- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { + [responseData_ appendData:data]; +} + +- (void)connectionDidFinishLoading:(NSURLConnection *)connection { + BOOL success = [(NSHTTPURLResponse *)response_ statusCode] < 400; + NKOAuthServiceTicket *ticket = [[NKOAuthServiceTicket alloc] initWithRequest:request_ response:response_ didSucceed:success]; + [delegate_ performSelector:didFinishSelector_ withObject:ticket withObject:responseData_]; + + self.connection = nil; + self.response = nil; + self.responseData = nil; +} + +- (void)connection:(NSURLConnection *)theConnection didFailWithError:(NSError *)error { + NKOAuthServiceTicket *ticket = [[NKOAuthServiceTicket alloc] initWithRequest:request_ response:response_ didSucceed:NO]; + [delegate_ performSelector:didFailSelector_ withObject:ticket withObject:error]; + + self.connection = nil; + self.response = nil; + self.responseData = nil; +} + +@end diff --git a/Sources/OAuth/NKOAuthHMACSHA1SignatureProvider.h b/Sources/OAuth/NKOAuthHMACSHA1SignatureProvider.h new file mode 100755 index 0000000..09a1957 --- /dev/null +++ b/Sources/OAuth/NKOAuthHMACSHA1SignatureProvider.h @@ -0,0 +1,10 @@ +// Part of FoundationKit http://foundationk.it +// +// Derived from Jon Crosby's MIT-licensed OAuthConsumer: http://oauth.googlecode.com/svn/code/obj-c/OAuthConsumer/ + +#import + +#import "NKOAuthSignatureProvider.h" + + +@interface NKOAuthHMACSHA1SignatureProvider : NSObject @end diff --git a/Sources/OAuth/NKOAuthHMACSHA1SignatureProvider.m b/Sources/OAuth/NKOAuthHMACSHA1SignatureProvider.m new file mode 100755 index 0000000..827c9e1 --- /dev/null +++ b/Sources/OAuth/NKOAuthHMACSHA1SignatureProvider.m @@ -0,0 +1,19 @@ +#import "NKOAuthHMACSHA1SignatureProvider.h" + +#import "NSData+NKCrypto.h" +#import "NSData+NKBase64.h" + + +@implementation NKOAuthHMACSHA1SignatureProvider + +- (NSString *)name { + return @"HMAC-SHA1"; +} + +- (NSString *)signClearText:(NSString *)text withSecret:(NSString *)secret { + NSData *textData = [text dataUsingEncoding:NSUTF8StringEncoding]; + NSData *hmac = [textData HMACWithAlgorithm:kCCHmacAlgSHA1 key:secret]; + return [hmac base64String]; +} + +@end diff --git a/Sources/OAuth/NKOAuthMutableURLRequest.h b/Sources/OAuth/NKOAuthMutableURLRequest.h new file mode 100755 index 0000000..62f3535 --- /dev/null +++ b/Sources/OAuth/NKOAuthMutableURLRequest.h @@ -0,0 +1,21 @@ +// Part of FoundationKit http://foundationk.it +// +// Derived from Jon Crosby's MIT-licensed OAuthConsumer: http://oauth.googlecode.com/svn/code/obj-c/OAuthConsumer/ + +#import + +#import "NKOAuthToken.h" +#import "NKOAuthConsumer.h" +#import "NKOAuthSignatureProvider.h" + + +@interface NKOAuthMutableURLRequest : NSMutableURLRequest { +} +@property (copy, readonly) NSString *signature; +@property (copy, readonly) NSString *nonce; + +- (id)initWithURL:(NSURL *)url consumer:(NKOAuthConsumer *)consumer token:(NKOAuthToken *)token realm:(NSString *)realm signatureProvider:(id)provider; +- (id)initWithURL:(NSURL *)url consumer:(NKOAuthConsumer *)consumer token:(NKOAuthToken *)token realm:(NSString *)realm signatureProvider:(id)provider nonce:(NSString *)aNonce timestamp:(NSString *)aTimestamp; +- (void)prepare; + +@end diff --git a/Sources/OAuth/NKOAuthMutableURLRequest.m b/Sources/OAuth/NKOAuthMutableURLRequest.m new file mode 100755 index 0000000..906ffaf --- /dev/null +++ b/Sources/OAuth/NKOAuthMutableURLRequest.m @@ -0,0 +1,179 @@ +#import "NKOAuthMutableURLRequest.h" + +#import "NKOAuthRequestParameter.h" +#import "NKOAuthHMACSHA1SignatureProvider.h" +#import "NSString+NKAdditions.h" + + +@interface NKOAuthMutableURLRequest () +@property (copy, readwrite) NSString *signature; +@property (copy, readwrite) NSString *nonce; +@property (copy, readwrite) NSString *realm; +@property (copy, readwrite) NSString *timestamp; +@property (nonatomic, strong) NKOAuthConsumer *consumer; +@property (nonatomic, strong) NKOAuthToken *token; +@property (nonatomic, strong) id signatureProvider; +- (void)_generateTimestamp; +- (void)_generateNonce; +- (NSString *)_signatureBaseString; +- (NSArray *)_parameters; +- (void)_setParameters:(NSArray *)parameters; +@end + +@implementation NKOAuthMutableURLRequest +@synthesize signature = signature_; +@synthesize nonce = nonce_; +@synthesize consumer = consumer_; +@synthesize token = token_; +@synthesize realm = realm_; +@synthesize signatureProvider = signatureProvider_; +@synthesize timestamp = timestamp_; + +- (id)initWithURL:(NSURL *)url consumer:(NKOAuthConsumer *)consumer token:(NKOAuthToken *)token realm:(NSString *)realm signatureProvider:(id)provider { + self = [super initWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0]; + if (self) { + self.consumer = consumer; + self.token = (token == nil ? [NKOAuthToken new] : token); + self.realm = (realm == nil ? [NSString new] : realm); + self.signatureProvider = (provider == nil ? [NKOAuthHMACSHA1SignatureProvider new] : provider); + + [self _generateTimestamp]; + [self _generateNonce]; + } + return self; +} + +// DEVNOTE: Setting a timestamp and nonce to known values can be helpful for testing +- (id)initWithURL:(NSURL *)url consumer:(NKOAuthConsumer *)consumer token:(NKOAuthToken *)token realm:(NSString *)realm signatureProvider:(id)provider nonce:(NSString *)nonce timestamp:(NSString *)timestamp { + self = [self initWithURL:url consumer:consumer token:token realm:realm signatureProvider:provider]; + if (self) { + self.nonce = nonce; + self.timestamp = timestamp; + } + return self; +} + +- (void)prepare { + // Sign + NSString *secret = [NSString stringWithFormat:@"%@&%@", self.consumer.secret, self.token.secret]; + self.signature = [self.signatureProvider signClearText:[self _signatureBaseString] withSecret:secret]; + + // Set OAuth headers + NSString *oauthToken; + if ([token_.key isEqualToString:@""]) { + oauthToken = @""; // not used on Request Token transactions + } else { + oauthToken = [NSString stringWithFormat:@"oauth_token=\"%@\", ", [self.token.key URLEncodedStringEscapingAllCharacters]]; + } + + NSString *oauthHeader = [NSString stringWithFormat:@"OAuth realm=\"%@\", oauth_consumer_key=\"%@\", %@oauth_signature_method=\"%@\", oauth_signature=\"%@\", oauth_timestamp=\"%@\", oauth_nonce=\"%@\", oauth_version=\"1.0\"", + [self.realm URLEncodedStringEscapingAllCharacters], + [self.consumer.key URLEncodedStringEscapingAllCharacters], + oauthToken, + [[self.signatureProvider name] URLEncodedStringEscapingAllCharacters], + [self.signature URLEncodedStringEscapingAllCharacters], + self.timestamp, + self.nonce]; + + [self setValue:oauthHeader forHTTPHeaderField:@"Authorization"]; +} + +- (void)_generateTimestamp { + self.timestamp = [NSString stringWithFormat:@"%d", time(NULL)]; +} + +- (void)_generateNonce { + CFUUIDRef theUUID = CFUUIDCreate(NULL); + self.nonce = CFBridgingRelease(CFUUIDCreateString(NULL, theUUID)); + CFRelease(theUUID); +} + +static NSString *NSURLStringRemoveQuery(NSURL *url) { + return [[[url absoluteString] componentsSeparatedByString:@"?"] objectAtIndex:0]; +} + +- (NSString *)_signatureBaseString { + // Calling parameters is a bit expensive so only call it once + NSArray *params = [self _parameters]; + + // OAuth Spec, Section 9.1.1 "Normalize Request Parameters" + // build a sorted array of both request parameters and OAuth header parameters + // + // DEVNOTE: 6 being the number of OAuth params in the Signature Base String + NSMutableArray *parameterPairs = [[NSMutableArray alloc] initWithCapacity:(6 + [params count])]; + + [parameterPairs addObject:[[NKOAuthRequestParameter requestParameterWithName:@"oauth_consumer_key" value:self.consumer.key] URLEncodedNameValuePair]]; + [parameterPairs addObject:[[NKOAuthRequestParameter requestParameterWithName:@"oauth_signature_method" value:[self.signatureProvider name]] URLEncodedNameValuePair]]; + [parameterPairs addObject:[[NKOAuthRequestParameter requestParameterWithName:@"oauth_timestamp" value:self.timestamp] URLEncodedNameValuePair]]; + [parameterPairs addObject:[[NKOAuthRequestParameter requestParameterWithName:@"oauth_nonce" value:self.nonce] URLEncodedNameValuePair]]; + [parameterPairs addObject:[[NKOAuthRequestParameter requestParameterWithName:@"oauth_version" value:@"1.0"] URLEncodedNameValuePair]]; + + if (![self.token.key isEqualToString:@""]) { + [parameterPairs addObject:[[NKOAuthRequestParameter requestParameterWithName:@"oauth_token" value:self.token.key] URLEncodedNameValuePair]]; + } + for (NKOAuthRequestParameter *param in params) { + [parameterPairs addObject:[param URLEncodedNameValuePair]]; + } + + NSArray *sortedPairs = [parameterPairs sortedArrayUsingSelector:@selector(compare:)]; + NSString *normalizedRequestParameters = [sortedPairs componentsJoinedByString:@"&"]; + + // OAuth Spec, Section 9.1.2 "Concatenate Request Elements" + return [NSString stringWithFormat:@"%@&%@&%@", + [self HTTPMethod], + [NSURLStringRemoveQuery([self URL]) URLEncodedStringEscapingAllCharacters], + [normalizedRequestParameters URLEncodedString]]; +} + +#pragma mark - +#pragma mark Private + +- (NSArray *)_parameters { + NSString *encodedParameters = nil; + if ([[self HTTPMethod] isEqualToString:@"GET"] || [[self HTTPMethod] isEqualToString:@"DELETE"]) { + encodedParameters = [[self URL] query]; + } else { + // POST, PUT + encodedParameters = [[NSString alloc] initWithData:[self HTTPBody] encoding:NSASCIIStringEncoding]; + } + + if ((encodedParameters == nil) || ([encodedParameters isEqualToString:@""])) { + return nil; + } + + NSArray *encodedParameterPairs = [encodedParameters componentsSeparatedByString:@"&"]; + NSMutableArray *requestParameters = [[NSMutableArray alloc] initWithCapacity:16]; + + for (NSString *encodedPair in encodedParameterPairs) { + NSArray *encodedPairElements = [encodedPair componentsSeparatedByString:@"="]; + NKOAuthRequestParameter *parameter = [NKOAuthRequestParameter requestParameterWithName:[[encodedPairElements objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] + value:[[encodedPairElements objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; + [requestParameters addObject:parameter]; + } + + return requestParameters; +} + +- (void)_setParameters:(NSArray *)parameters { + NSMutableString *encodedParameterPairs = [[NSMutableString alloc] initWithCapacity:256]; + NSInteger position = 1; + for (NKOAuthRequestParameter *requestParameter in parameters) { + [encodedParameterPairs appendString:[requestParameter URLEncodedNameValuePair]]; + if (position < [parameters count]) { + [encodedParameterPairs appendString:@"&"]; + } + position++; + } + + if ([[self HTTPMethod] isEqualToString:@"GET"] || [[self HTTPMethod] isEqualToString:@"DELETE"]) { + [self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@", NSURLStringRemoveQuery([self URL]), encodedParameterPairs]]]; + } else { + // POST, PUT + NSData *postData = [encodedParameterPairs dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; + [self setHTTPBody:postData]; + [self setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"]; + [self setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; + } +} + +@end diff --git a/Sources/OAuth/NKOAuthPlaintextSignatureProvider.h b/Sources/OAuth/NKOAuthPlaintextSignatureProvider.h new file mode 100755 index 0000000..9e5dcfe --- /dev/null +++ b/Sources/OAuth/NKOAuthPlaintextSignatureProvider.h @@ -0,0 +1,10 @@ +// Part of FoundationKit http://foundationk.it +// +// Derived from Jon Crosby's MIT-licensed OAuthConsumer: http://oauth.googlecode.com/svn/code/obj-c/OAuthConsumer/ + +#import + +#import "NKOAuthSignatureProvider.h" + + +@interface NKOAuthPlaintextSignatureProvider : NSObject @end diff --git a/Sources/OAuth/NKOAuthPlaintextSignatureProvider.m b/Sources/OAuth/NKOAuthPlaintextSignatureProvider.m new file mode 100755 index 0000000..6788286 --- /dev/null +++ b/Sources/OAuth/NKOAuthPlaintextSignatureProvider.m @@ -0,0 +1,14 @@ +#import "NKOAuthPlaintextSignatureProvider.h" + + +@implementation NKOAuthPlaintextSignatureProvider + +- (NSString *)name { + return @"PLAINTEXT"; +} + +- (NSString *)signClearText:(NSString *)text withSecret:(NSString *)secret { + return secret; +} + +@end diff --git a/Sources/OAuth/NKOAuthRequestParameter.h b/Sources/OAuth/NKOAuthRequestParameter.h new file mode 100755 index 0000000..c0e2f5e --- /dev/null +++ b/Sources/OAuth/NKOAuthRequestParameter.h @@ -0,0 +1,20 @@ +// Part of FoundationKit http://foundationk.it +// +// Derived from Jon Crosby's MIT-licensed OAuthConsumer: http://oauth.googlecode.com/svn/code/obj-c/OAuthConsumer/ + +#import + + +@interface NKOAuthRequestParameter : NSObject { +} +@property (nonatomic, copy) NSString *name; +@property (nonatomic, copy) NSString *value; + ++ (id)requestParameterWithName:(NSString *)name value:(NSString *)value; +- (id)initWithName:(NSString *)name value:(NSString *)value; + +- (NSString *)URLEncodedName; +- (NSString *)URLEncodedValue; +- (NSString *)URLEncodedNameValuePair; + +@end diff --git a/Sources/OAuth/NKOAuthRequestParameter.m b/Sources/OAuth/NKOAuthRequestParameter.m new file mode 100755 index 0000000..280aa55 --- /dev/null +++ b/Sources/OAuth/NKOAuthRequestParameter.m @@ -0,0 +1,40 @@ +#import "NKOAuthRequestParameter.h" + +#import "NSString+NKAdditions.h" + + +@implementation NKOAuthRequestParameter +@synthesize name = name_; +@synthesize value = value_; + +- (void)dealloc { + self.name = nil; + self.value = nil; +} + ++ (id)requestParameterWithName:(NSString *)name value:(NSString *)value { + return [[NKOAuthRequestParameter alloc] initWithName:name value:value]; +} + +- (id)initWithName:(NSString *)name value:(NSString *)value { + self = [super init]; + if (self) { + self.name = name; + self.value = value; + } + return self; +} + +- (NSString *)URLEncodedName { + return [self.name URLEncodedStringEscapingAllCharacters]; +} + +- (NSString *)URLEncodedValue { + return [self.value URLEncodedStringEscapingAllCharacters]; +} + +- (NSString *)URLEncodedNameValuePair { + return [NSString stringWithFormat:@"%@=%@", [self URLEncodedName], [self URLEncodedValue]]; +} + +@end diff --git a/Sources/OAuth/NKOAuthServiceTicket.h b/Sources/OAuth/NKOAuthServiceTicket.h new file mode 100755 index 0000000..a062ed2 --- /dev/null +++ b/Sources/OAuth/NKOAuthServiceTicket.h @@ -0,0 +1,19 @@ +// Part of FoundationKit http://foundationk.it +// +// Derived from Jon Crosby's MIT-licensed OAuthConsumer: http://oauth.googlecode.com/svn/code/obj-c/OAuthConsumer/ + +#import + +#import "NKOAuthMutableURLRequest.h" + + +@interface NKOAuthServiceTicket : NSObject { +} +@property (strong, readonly) NKOAuthMutableURLRequest *request; +@property (strong, readonly) NSURLResponse *response; +@property (assign, readonly) BOOL didSucceed; + ++ (id)ticketWithRequest:(NKOAuthMutableURLRequest *)request response:(NSURLResponse *)response didSucceed:(BOOL)success; +- (id)initWithRequest:(NKOAuthMutableURLRequest *)request response:(NSURLResponse *)response didSucceed:(BOOL)success; + +@end diff --git a/Sources/OAuth/NKOAuthServiceTicket.m b/Sources/OAuth/NKOAuthServiceTicket.m new file mode 100755 index 0000000..9ae5b9d --- /dev/null +++ b/Sources/OAuth/NKOAuthServiceTicket.m @@ -0,0 +1,31 @@ +#import "NKOAuthServiceTicket.h" + + +@interface NKOAuthServiceTicket () +@property (strong, readwrite) NKOAuthMutableURLRequest *request; +@property (strong, readwrite) NSURLResponse *response; +@property (assign, readwrite) BOOL didSucceed; +@end + +@implementation NKOAuthServiceTicket +@synthesize request = request_; +@synthesize response = response_; +@synthesize didSucceed = didSucceed_; + ++ (id)ticketWithRequest:(NKOAuthMutableURLRequest *)request response:(NSURLResponse *)response didSucceed:(BOOL)success { + return [[NKOAuthServiceTicket alloc] initWithRequest:request response:response didSucceed:success]; +} + +- (id)initWithRequest:(NKOAuthMutableURLRequest *)request response:(NSURLResponse *)response didSucceed:(BOOL)success { + self = [super init]; + if (self) { + self.request = request; + self.response = response; + self.didSucceed = success; + } + return self; +} + + + +@end diff --git a/Sources/OAuth/NKOAuthSignatureProvider.h b/Sources/OAuth/NKOAuthSignatureProvider.h new file mode 100755 index 0000000..f7cb3b6 --- /dev/null +++ b/Sources/OAuth/NKOAuthSignatureProvider.h @@ -0,0 +1,13 @@ +// Part of FoundationKit http://foundationk.it +// +// Derived from Jon Crosby's MIT-licensed OAuthConsumer: http://oauth.googlecode.com/svn/code/obj-c/OAuthConsumer/ + +#import + + +@protocol NKOAuthSignatureProvider + +- (NSString *)name; +- (NSString *)signClearText:(NSString *)text withSecret:(NSString *)secret; + +@end diff --git a/Sources/OAuth/NKOAuthToken.h b/Sources/OAuth/NKOAuthToken.h new file mode 100755 index 0000000..1fea82a --- /dev/null +++ b/Sources/OAuth/NKOAuthToken.h @@ -0,0 +1,23 @@ +// Part of FoundationKit http://foundationk.it +// +// Derived from Jon Crosby's MIT-licensed OAuthConsumer: http://oauth.googlecode.com/svn/code/obj-c/OAuthConsumer/ + +#import + +@interface NKOAuthToken : NSObject { +} +@property (copy) NSString *key; +@property (copy) NSString *secret; + + ++ (id)tokenWithKey:(NSString *)key secret:(NSString *)secret; ++ (id)tokenWithUserDefaultsUsingServiceProviderName:(NSString *)provider prefix:(NSString *)prefix; ++ (id)tokenWithHTTPResponseBody:(NSString *)body; ++ (void)removeFromUserDefaultsWithServiceProviderName:(NSString *)provider prefix:(NSString *)prefix; + +- (id)initWithKey:(NSString *)key secret:(NSString *)secret; +- (id)initWithUserDefaultsUsingServiceProviderName:(NSString *)provider prefix:(NSString *)prefix; +- (id)initWithHTTPResponseBody:(NSString *)body; +- (int)storeInUserDefaultsWithServiceProviderName:(NSString *)provider prefix:(NSString *)prefix; + +@end diff --git a/Sources/OAuth/NKOAuthToken.m b/Sources/OAuth/NKOAuthToken.m new file mode 100755 index 0000000..d28aff3 --- /dev/null +++ b/Sources/OAuth/NKOAuthToken.m @@ -0,0 +1,92 @@ +#import "NKOAuthToken.h" + + +@implementation NKOAuthToken +@synthesize key = key_; +@synthesize secret = secret_; + ++ (id)tokenWithKey:(NSString *)key secret:(NSString *)secret { + return [[NKOAuthToken alloc] initWithKey:key secret:secret]; +} + ++ (id)tokenWithUserDefaultsUsingServiceProviderName:(NSString *)provider prefix:(NSString *)prefix { + return [[NKOAuthToken alloc] initWithUserDefaultsUsingServiceProviderName:provider prefix:prefix]; +} + ++ (id)tokenWithHTTPResponseBody:(NSString *)body { + return [[NKOAuthToken alloc] initWithHTTPResponseBody:body]; +} + +- (id)init { + return [self initWithKey:@"" secret:@""]; +} + +- (id)initWithCoder:(NSCoder *)coder { + self = [super init]; + if (self) { + [self setKey:[coder decodeObjectForKey:@"key"]]; + [self setSecret:[coder decodeObjectForKey:@"secret"]]; + } + return self; +} + +- (void)encodeWithCoder:(NSCoder *)coder { + [coder encodeObject:self.key forKey:@"key"]; + [coder encodeObject:self.secret forKey:@"secret"]; +} + +- (id)initWithKey:(NSString *)key secret:(NSString *)secret { + self = [super init]; + if (self) { + self.key = key; + self.secret = secret; + } + return self; +} + +- (id)initWithHTTPResponseBody:(NSString *)body { + self = [super init]; + if (self) { + NSArray *pairs = [body componentsSeparatedByString:@"&"]; + for (NSString *pair in pairs) { + NSArray *elements = [pair componentsSeparatedByString:@"="]; + if ([[elements objectAtIndex:0] isEqualToString:@"oauth_token"]) { + self.key = [elements objectAtIndex:1]; + } else if ([[elements objectAtIndex:0] isEqualToString:@"oauth_token_secret"]) { + self.secret = [elements objectAtIndex:1]; + } + } + } + return self; +} + +- (id)initWithUserDefaultsUsingServiceProviderName:(NSString *)provider prefix:(NSString *)prefix { + self = [super init]; + if (self) { + NSString *theKey = [[NSUserDefaults standardUserDefaults] stringForKey:[NSString stringWithFormat:@"OAUTH_%@_%@_KEY", prefix, provider]]; + NSString *theSecret = [[NSUserDefaults standardUserDefaults] stringForKey:[NSString stringWithFormat:@"OAUTH_%@_%@_SECRET", prefix, provider]]; + + if (theKey == NULL || theSecret == NULL) { + return nil; + } + + self.key = theKey; + self.secret = theSecret; + } + return self; +} + +- (int)storeInUserDefaultsWithServiceProviderName:(NSString *)provider prefix:(NSString *)prefix { + [[NSUserDefaults standardUserDefaults] setObject:self.key forKey:[NSString stringWithFormat:@"OAUTH_%@_%@_KEY", prefix, provider]]; + [[NSUserDefaults standardUserDefaults] setObject:self.secret forKey:[NSString stringWithFormat:@"OAUTH_%@_%@_SECRET", prefix, provider]]; + [[NSUserDefaults standardUserDefaults] synchronize]; + return(0); +} + ++ (void)removeFromUserDefaultsWithServiceProviderName:(NSString *)provider prefix:(NSString *)prefix { + [[NSUserDefaults standardUserDefaults] setObject:nil forKey:[NSString stringWithFormat:@"OAUTH_%@_%@_KEY", prefix, provider]]; + [[NSUserDefaults standardUserDefaults] setObject:nil forKey:[NSString stringWithFormat:@"OAUTH_%@_%@_SECRET", prefix, provider]]; + [[NSUserDefaults standardUserDefaults] synchronize]; +} + +@end From 963a8ee5c621327dfda1516c0558672bd2404042 Mon Sep 17 00:00:00 2001 From: Erik Aigner Date: Fri, 15 Jul 2011 00:22:44 +0200 Subject: [PATCH 2/2] cleaned up tabs, empty lines and whitespaces --- Sources/OAuth/NKOAuthConsumer.m | 6 +-- Sources/OAuth/NKOAuthDataFetcher.m | 42 +++++++++---------- .../OAuth/NKOAuthHMACSHA1SignatureProvider.m | 2 +- Sources/OAuth/NKOAuthMutableURLRequest.m | 2 +- Sources/OAuth/NKOAuthRequestParameter.m | 6 +-- Sources/OAuth/NKOAuthServiceTicket.m | 4 +- Sources/OAuth/NKOAuthToken.m | 34 +++++++-------- 7 files changed, 46 insertions(+), 50 deletions(-) diff --git a/Sources/OAuth/NKOAuthConsumer.m b/Sources/OAuth/NKOAuthConsumer.m index bebcfc3..2a7308c 100755 --- a/Sources/OAuth/NKOAuthConsumer.m +++ b/Sources/OAuth/NKOAuthConsumer.m @@ -6,7 +6,7 @@ @implementation NKOAuthConsumer @synthesize secret = secret_; + (id)consumerWithKey:(NSString *)key secret:(NSString *)secret { - return [[NKOAuthConsumer alloc] initWithKey:key secret:secret]; + return [[NKOAuthConsumer alloc] initWithKey:key secret:secret]; } - (id)initWithKey:(NSString *)key secret:(NSString *)secret { @@ -15,9 +15,7 @@ - (id)initWithKey:(NSString *)key secret:(NSString *)secret { self.key = key; self.secret = secret; } - return self; + return self; } - - @end diff --git a/Sources/OAuth/NKOAuthDataFetcher.m b/Sources/OAuth/NKOAuthDataFetcher.m index 3bc4426..08ceb5d 100755 --- a/Sources/OAuth/NKOAuthDataFetcher.m +++ b/Sources/OAuth/NKOAuthDataFetcher.m @@ -21,12 +21,12 @@ @implementation NKOAuthDataFetcher @synthesize didFailSelector = didFailSelector_; + (id)fetcherWithRequest:(NKOAuthMutableURLRequest *)request delegate:(id)dlg didFinishSelector:(SEL)finSlc didFailSelector:(SEL)failSlc { - NKOAuthDataFetcher *fetcher = [NKOAuthDataFetcher new]; - fetcher.request = request; - fetcher.delegate = dlg; - fetcher.didFinishSelector = finSlc; - fetcher.didFailSelector = failSlc; - return fetcher; + NKOAuthDataFetcher *fetcher = [NKOAuthDataFetcher new]; + fetcher.request = request; + fetcher.delegate = dlg; + fetcher.didFinishSelector = finSlc; + fetcher.didFailSelector = failSlc; + return fetcher; } - (void)fetchDataWithRequest:(NKOAuthMutableURLRequest *)request delegate:(id)dlg didFinishSelector:(SEL)finSlc didFailSelector:(SEL)failSlc { @@ -41,12 +41,12 @@ - (void)fetchData { [request_ prepare]; self.connection = [NSURLConnection connectionWithRequest:request_ delegate:self]; - if (connection_) { - self.responseData = [NSMutableData data]; - } else { - NKOAuthServiceTicket *ticket = [[NKOAuthServiceTicket alloc] initWithRequest:request_ response:nil didSucceed:NO]; - [delegate_ performSelector:didFailSelector_ withObject:ticket withObject:nil]; - } + if (connection_) { + self.responseData = [NSMutableData data]; + } else { + NKOAuthServiceTicket *ticket = [[NKOAuthServiceTicket alloc] initWithRequest:request_ response:nil didSucceed:NO]; + [delegate_ performSelector:didFailSelector_ withObject:ticket withObject:nil]; + } } - (void)dealloc { @@ -57,19 +57,19 @@ - (void)dealloc { } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { - self.response = response; + self.response = response; [responseData_ setLength:0]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { - [responseData_ appendData:data]; + [responseData_ appendData:data]; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { - BOOL success = [(NSHTTPURLResponse *)response_ statusCode] < 400; - NKOAuthServiceTicket *ticket = [[NKOAuthServiceTicket alloc] initWithRequest:request_ response:response_ didSucceed:success]; - [delegate_ performSelector:didFinishSelector_ withObject:ticket withObject:responseData_]; - + BOOL success = [(NSHTTPURLResponse *)response_ statusCode] < 400; + NKOAuthServiceTicket *ticket = [[NKOAuthServiceTicket alloc] initWithRequest:request_ response:response_ didSucceed:success]; + [delegate_ performSelector:didFinishSelector_ withObject:ticket withObject:responseData_]; + self.connection = nil; self.response = nil; self.responseData = nil; @@ -77,9 +77,9 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection { - (void)connection:(NSURLConnection *)theConnection didFailWithError:(NSError *)error { NKOAuthServiceTicket *ticket = [[NKOAuthServiceTicket alloc] initWithRequest:request_ response:response_ didSucceed:NO]; - [delegate_ performSelector:didFailSelector_ withObject:ticket withObject:error]; - - self.connection = nil; + [delegate_ performSelector:didFailSelector_ withObject:ticket withObject:error]; + + self.connection = nil; self.response = nil; self.responseData = nil; } diff --git a/Sources/OAuth/NKOAuthHMACSHA1SignatureProvider.m b/Sources/OAuth/NKOAuthHMACSHA1SignatureProvider.m index 827c9e1..ea8f93a 100755 --- a/Sources/OAuth/NKOAuthHMACSHA1SignatureProvider.m +++ b/Sources/OAuth/NKOAuthHMACSHA1SignatureProvider.m @@ -7,7 +7,7 @@ @implementation NKOAuthHMACSHA1SignatureProvider - (NSString *)name { - return @"HMAC-SHA1"; + return @"HMAC-SHA1"; } - (NSString *)signClearText:(NSString *)text withSecret:(NSString *)secret { diff --git a/Sources/OAuth/NKOAuthMutableURLRequest.m b/Sources/OAuth/NKOAuthMutableURLRequest.m index 906ffaf..6140a59 100755 --- a/Sources/OAuth/NKOAuthMutableURLRequest.m +++ b/Sources/OAuth/NKOAuthMutableURLRequest.m @@ -147,7 +147,7 @@ - (NSArray *)_parameters { for (NSString *encodedPair in encodedParameterPairs) { NSArray *encodedPairElements = [encodedPair componentsSeparatedByString:@"="]; NKOAuthRequestParameter *parameter = [NKOAuthRequestParameter requestParameterWithName:[[encodedPairElements objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] - value:[[encodedPairElements objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; + value:[[encodedPairElements objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; [requestParameters addObject:parameter]; } diff --git a/Sources/OAuth/NKOAuthRequestParameter.m b/Sources/OAuth/NKOAuthRequestParameter.m index 280aa55..3a066e3 100755 --- a/Sources/OAuth/NKOAuthRequestParameter.m +++ b/Sources/OAuth/NKOAuthRequestParameter.m @@ -26,15 +26,15 @@ - (id)initWithName:(NSString *)name value:(NSString *)value { } - (NSString *)URLEncodedName { - return [self.name URLEncodedStringEscapingAllCharacters]; + return [self.name URLEncodedStringEscapingAllCharacters]; } - (NSString *)URLEncodedValue { - return [self.value URLEncodedStringEscapingAllCharacters]; + return [self.value URLEncodedStringEscapingAllCharacters]; } - (NSString *)URLEncodedNameValuePair { - return [NSString stringWithFormat:@"%@=%@", [self URLEncodedName], [self URLEncodedValue]]; + return [NSString stringWithFormat:@"%@=%@", [self URLEncodedName], [self URLEncodedValue]]; } @end diff --git a/Sources/OAuth/NKOAuthServiceTicket.m b/Sources/OAuth/NKOAuthServiceTicket.m index 9ae5b9d..908bddd 100755 --- a/Sources/OAuth/NKOAuthServiceTicket.m +++ b/Sources/OAuth/NKOAuthServiceTicket.m @@ -13,7 +13,7 @@ @implementation NKOAuthServiceTicket @synthesize didSucceed = didSucceed_; + (id)ticketWithRequest:(NKOAuthMutableURLRequest *)request response:(NSURLResponse *)response didSucceed:(BOOL)success { - return [[NKOAuthServiceTicket alloc] initWithRequest:request response:response didSucceed:success]; + return [[NKOAuthServiceTicket alloc] initWithRequest:request response:response didSucceed:success]; } - (id)initWithRequest:(NKOAuthMutableURLRequest *)request response:(NSURLResponse *)response didSucceed:(BOOL)success { @@ -26,6 +26,4 @@ - (id)initWithRequest:(NKOAuthMutableURLRequest *)request response:(NSURLRespons return self; } - - @end diff --git a/Sources/OAuth/NKOAuthToken.m b/Sources/OAuth/NKOAuthToken.m index d28aff3..6ef3f8d 100755 --- a/Sources/OAuth/NKOAuthToken.m +++ b/Sources/OAuth/NKOAuthToken.m @@ -6,15 +6,15 @@ @implementation NKOAuthToken @synthesize secret = secret_; + (id)tokenWithKey:(NSString *)key secret:(NSString *)secret { - return [[NKOAuthToken alloc] initWithKey:key secret:secret]; + return [[NKOAuthToken alloc] initWithKey:key secret:secret]; } + (id)tokenWithUserDefaultsUsingServiceProviderName:(NSString *)provider prefix:(NSString *)prefix { - return [[NKOAuthToken alloc] initWithUserDefaultsUsingServiceProviderName:provider prefix:prefix]; + return [[NKOAuthToken alloc] initWithUserDefaultsUsingServiceProviderName:provider prefix:prefix]; } + (id)tokenWithHTTPResponseBody:(NSString *)body { - return [[NKOAuthToken alloc] initWithHTTPResponseBody:body]; + return [[NKOAuthToken alloc] initWithHTTPResponseBody:body]; } - (id)init { @@ -23,16 +23,16 @@ - (id)init { - (id)initWithCoder:(NSCoder *)coder { self = [super init]; - if (self) { - [self setKey:[coder decodeObjectForKey:@"key"]]; - [self setSecret:[coder decodeObjectForKey:@"secret"]]; - } - return self; + if (self) { + [self setKey:[coder decodeObjectForKey:@"key"]]; + [self setSecret:[coder decodeObjectForKey:@"secret"]]; + } + return self; } - (void)encodeWithCoder:(NSCoder *)coder { - [coder encodeObject:self.key forKey:@"key"]; - [coder encodeObject:self.secret forKey:@"secret"]; + [coder encodeObject:self.key forKey:@"key"]; + [coder encodeObject:self.secret forKey:@"secret"]; } - (id)initWithKey:(NSString *)key secret:(NSString *)secret { @@ -77,16 +77,16 @@ - (id)initWithUserDefaultsUsingServiceProviderName:(NSString *)provider prefix:( } - (int)storeInUserDefaultsWithServiceProviderName:(NSString *)provider prefix:(NSString *)prefix { - [[NSUserDefaults standardUserDefaults] setObject:self.key forKey:[NSString stringWithFormat:@"OAUTH_%@_%@_KEY", prefix, provider]]; - [[NSUserDefaults standardUserDefaults] setObject:self.secret forKey:[NSString stringWithFormat:@"OAUTH_%@_%@_SECRET", prefix, provider]]; - [[NSUserDefaults standardUserDefaults] synchronize]; - return(0); + [[NSUserDefaults standardUserDefaults] setObject:self.key forKey:[NSString stringWithFormat:@"OAUTH_%@_%@_KEY", prefix, provider]]; + [[NSUserDefaults standardUserDefaults] setObject:self.secret forKey:[NSString stringWithFormat:@"OAUTH_%@_%@_SECRET", prefix, provider]]; + [[NSUserDefaults standardUserDefaults] synchronize]; + return(0); } + (void)removeFromUserDefaultsWithServiceProviderName:(NSString *)provider prefix:(NSString *)prefix { - [[NSUserDefaults standardUserDefaults] setObject:nil forKey:[NSString stringWithFormat:@"OAUTH_%@_%@_KEY", prefix, provider]]; - [[NSUserDefaults standardUserDefaults] setObject:nil forKey:[NSString stringWithFormat:@"OAUTH_%@_%@_SECRET", prefix, provider]]; - [[NSUserDefaults standardUserDefaults] synchronize]; + [[NSUserDefaults standardUserDefaults] setObject:nil forKey:[NSString stringWithFormat:@"OAUTH_%@_%@_KEY", prefix, provider]]; + [[NSUserDefaults standardUserDefaults] setObject:nil forKey:[NSString stringWithFormat:@"OAUTH_%@_%@_SECRET", prefix, provider]]; + [[NSUserDefaults standardUserDefaults] synchronize]; } @end