Skip to content
Browse files

Add RSOAuthEngine, FiveHundredPxOAuthEngine from demo project.

  • Loading branch information...
1 parent 8e3599f commit c1e72d8d52f160337e9b24d3225e1de2f17655e1 @iKenndac committed Feb 18, 2012
View
3 .gitmodules
@@ -1,3 +1,6 @@
[submodule "TouchJSON"]
path = TouchJSON
url = git://github.com/TouchCode/TouchJSON.git
+[submodule "RSOAuthEngine"]
+ path = RSOAuthEngine
+ url = git://github.com/iKenndac/RSOAuthEngine.git
View
114 500px Aperture Uploader.xcodeproj/project.pbxproj
@@ -17,6 +17,19 @@
5044FE9414EFD1380015F359 /* CJSONDeserializer.m in Sources */ = {isa = PBXBuildFile; fileRef = 5044FE8814EFD1380015F359 /* CJSONDeserializer.m */; };
5044FE9514EFD1380015F359 /* CJSONScanner.m in Sources */ = {isa = PBXBuildFile; fileRef = 5044FE8A14EFD1380015F359 /* CJSONScanner.m */; };
5044FE9614EFD1380015F359 /* CJSONSerializer.m in Sources */ = {isa = PBXBuildFile; fileRef = 5044FE8C14EFD1380015F359 /* CJSONSerializer.m */; };
+ 5044FEBD14EFD3BD0015F359 /* RSOAuthEngine.m in Sources */ = {isa = PBXBuildFile; fileRef = 5044FEBC14EFD3BD0015F359 /* RSOAuthEngine.m */; };
+ 5044FED414EFD3C60015F359 /* NSAlert+MKNetworkKitAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 5044FEC114EFD3C60015F359 /* NSAlert+MKNetworkKitAdditions.m */; };
+ 5044FED514EFD3C60015F359 /* NSData+Base64.m in Sources */ = {isa = PBXBuildFile; fileRef = 5044FEC314EFD3C60015F359 /* NSData+Base64.m */; };
+ 5044FED614EFD3C60015F359 /* NSDate+RFC1123.m in Sources */ = {isa = PBXBuildFile; fileRef = 5044FEC514EFD3C60015F359 /* NSDate+RFC1123.m */; };
+ 5044FED714EFD3C60015F359 /* NSDictionary+RequestEncoding.m in Sources */ = {isa = PBXBuildFile; fileRef = 5044FEC714EFD3C60015F359 /* NSDictionary+RequestEncoding.m */; };
+ 5044FED814EFD3C60015F359 /* NSString+MKNetworkKitAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 5044FEC914EFD3C60015F359 /* NSString+MKNetworkKitAdditions.m */; };
+ 5044FED914EFD3C60015F359 /* UIAlertView+MKNetworkKitAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 5044FECB14EFD3C60015F359 /* UIAlertView+MKNetworkKitAdditions.m */; };
+ 5044FEDA14EFD3C60015F359 /* MKNetworkEngine.m in Sources */ = {isa = PBXBuildFile; fileRef = 5044FECD14EFD3C60015F359 /* MKNetworkEngine.m */; };
+ 5044FEDB14EFD3C60015F359 /* MKNetworkOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = 5044FED014EFD3C60015F359 /* MKNetworkOperation.m */; };
+ 5044FEDC14EFD3C60015F359 /* Reachability.m in Sources */ = {isa = PBXBuildFile; fileRef = 5044FED314EFD3C60015F359 /* Reachability.m */; };
+ 5044FEDE14EFD4910015F359 /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5044FEDD14EFD4910015F359 /* Security.framework */; };
+ 5044FEE014EFD4970015F359 /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5044FEDF14EFD4970015F359 /* SystemConfiguration.framework */; };
+ 5044FEE314EFD6D30015F359 /* FiveHundredPxOAuthEngine.m in Sources */ = {isa = PBXBuildFile; fileRef = 5044FEE214EFD6D30015F359 /* FiveHundredPxOAuthEngine.m */; };
50AAF3A514EEC0F8009F3BFE /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 50AAF3A414EEC0F8009F3BFE /* Cocoa.framework */; };
50AAF3AF14EEC0F8009F3BFE /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 50AAF3AD14EEC0F8009F3BFE /* InfoPlist.strings */; };
50AAF3BE14EEC1B0009F3BFE /* FiveHundredPxApertureExporter.m in Sources */ = {isa = PBXBuildFile; fileRef = 50AAF3BD14EEC1B0009F3BFE /* FiveHundredPxApertureExporter.m */; };
@@ -44,6 +57,31 @@
5044FE8B14EFD1380015F359 /* CJSONSerializer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CJSONSerializer.h; sourceTree = "<group>"; };
5044FE8C14EFD1380015F359 /* CJSONSerializer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CJSONSerializer.m; sourceTree = "<group>"; };
5044FE8D14EFD1380015F359 /* JSONRepresentation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSONRepresentation.h; sourceTree = "<group>"; };
+ 5044FEBB14EFD3BD0015F359 /* RSOAuthEngine.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RSOAuthEngine.h; sourceTree = "<group>"; };
+ 5044FEBC14EFD3BD0015F359 /* RSOAuthEngine.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RSOAuthEngine.m; sourceTree = "<group>"; };
+ 5044FEC014EFD3C60015F359 /* NSAlert+MKNetworkKitAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSAlert+MKNetworkKitAdditions.h"; sourceTree = "<group>"; };
+ 5044FEC114EFD3C60015F359 /* NSAlert+MKNetworkKitAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSAlert+MKNetworkKitAdditions.m"; sourceTree = "<group>"; };
+ 5044FEC214EFD3C60015F359 /* NSData+Base64.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSData+Base64.h"; sourceTree = "<group>"; };
+ 5044FEC314EFD3C60015F359 /* NSData+Base64.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSData+Base64.m"; sourceTree = "<group>"; };
+ 5044FEC414EFD3C60015F359 /* NSDate+RFC1123.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSDate+RFC1123.h"; sourceTree = "<group>"; };
+ 5044FEC514EFD3C60015F359 /* NSDate+RFC1123.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSDate+RFC1123.m"; sourceTree = "<group>"; };
+ 5044FEC614EFD3C60015F359 /* NSDictionary+RequestEncoding.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSDictionary+RequestEncoding.h"; sourceTree = "<group>"; };
+ 5044FEC714EFD3C60015F359 /* NSDictionary+RequestEncoding.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSDictionary+RequestEncoding.m"; sourceTree = "<group>"; };
+ 5044FEC814EFD3C60015F359 /* NSString+MKNetworkKitAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSString+MKNetworkKitAdditions.h"; sourceTree = "<group>"; };
+ 5044FEC914EFD3C60015F359 /* NSString+MKNetworkKitAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSString+MKNetworkKitAdditions.m"; sourceTree = "<group>"; };
+ 5044FECA14EFD3C60015F359 /* UIAlertView+MKNetworkKitAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIAlertView+MKNetworkKitAdditions.h"; sourceTree = "<group>"; };
+ 5044FECB14EFD3C60015F359 /* UIAlertView+MKNetworkKitAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIAlertView+MKNetworkKitAdditions.m"; sourceTree = "<group>"; };
+ 5044FECC14EFD3C60015F359 /* MKNetworkEngine.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MKNetworkEngine.h; sourceTree = "<group>"; };
+ 5044FECD14EFD3C60015F359 /* MKNetworkEngine.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MKNetworkEngine.m; sourceTree = "<group>"; };
+ 5044FECE14EFD3C60015F359 /* MKNetworkKit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MKNetworkKit.h; sourceTree = "<group>"; };
+ 5044FECF14EFD3C60015F359 /* MKNetworkOperation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MKNetworkOperation.h; sourceTree = "<group>"; };
+ 5044FED014EFD3C60015F359 /* MKNetworkOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MKNetworkOperation.m; sourceTree = "<group>"; };
+ 5044FED214EFD3C60015F359 /* Reachability.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Reachability.h; sourceTree = "<group>"; };
+ 5044FED314EFD3C60015F359 /* Reachability.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Reachability.m; sourceTree = "<group>"; };
+ 5044FEDD14EFD4910015F359 /* Security.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Security.framework; path = System/Library/Frameworks/Security.framework; sourceTree = SDKROOT; };
+ 5044FEDF14EFD4970015F359 /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = System/Library/Frameworks/SystemConfiguration.framework; sourceTree = SDKROOT; };
+ 5044FEE114EFD6D30015F359 /* FiveHundredPxOAuthEngine.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FiveHundredPxOAuthEngine.h; sourceTree = "<group>"; };
+ 5044FEE214EFD6D30015F359 /* FiveHundredPxOAuthEngine.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FiveHundredPxOAuthEngine.m; sourceTree = "<group>"; };
50AAF3A114EEC0F8009F3BFE /* 500px Aperture Uploader.ApertureExport */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "500px Aperture Uploader.ApertureExport"; sourceTree = BUILT_PRODUCTS_DIR; };
50AAF3A414EEC0F8009F3BFE /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; };
50AAF3A714EEC0F8009F3BFE /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = System/Library/Frameworks/AppKit.framework; sourceTree = SDKROOT; };
@@ -64,6 +102,8 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
+ 5044FEE014EFD4970015F359 /* SystemConfiguration.framework in Frameworks */,
+ 5044FEDE14EFD4910015F359 /* Security.framework in Frameworks */,
50AAF3C014EEC236009F3BFE /* PluginManager.framework in Frameworks */,
50AAF3A514EEC0F8009F3BFE /* Cocoa.framework in Frameworks */,
);
@@ -75,7 +115,11 @@
5044FE7614EFD12B0015F359 /* External */ = {
isa = PBXGroup;
children = (
+ 50AAF3C214EEC3F1009F3BFE /* ApertureExportManager.h */,
+ 50AAF3C314EEC3F1009F3BFE /* ApertureExportPlugIn.h */,
5044FE7714EFD1380015F359 /* TouchJSON */,
+ 5044FEBA14EFD3BD0015F359 /* RSOAuthEngine */,
+ 5044FEBE14EFD3C60015F359 /* MKNetworkKit */,
);
name = External;
sourceTree = "<group>";
@@ -131,9 +175,64 @@
path = JSON;
sourceTree = "<group>";
};
+ 5044FEBA14EFD3BD0015F359 /* RSOAuthEngine */ = {
+ isa = PBXGroup;
+ children = (
+ 5044FEBB14EFD3BD0015F359 /* RSOAuthEngine.h */,
+ 5044FEBC14EFD3BD0015F359 /* RSOAuthEngine.m */,
+ );
+ name = RSOAuthEngine;
+ path = RSOAuthEngine/RSOAuthEngine;
+ sourceTree = SOURCE_ROOT;
+ };
+ 5044FEBE14EFD3C60015F359 /* MKNetworkKit */ = {
+ isa = PBXGroup;
+ children = (
+ 5044FEBF14EFD3C60015F359 /* Categories */,
+ 5044FECC14EFD3C60015F359 /* MKNetworkEngine.h */,
+ 5044FECD14EFD3C60015F359 /* MKNetworkEngine.m */,
+ 5044FECE14EFD3C60015F359 /* MKNetworkKit.h */,
+ 5044FECF14EFD3C60015F359 /* MKNetworkOperation.h */,
+ 5044FED014EFD3C60015F359 /* MKNetworkOperation.m */,
+ 5044FED114EFD3C60015F359 /* Reachability */,
+ );
+ name = MKNetworkKit;
+ path = RSOAuthEngine/MKNetworkKit/MKNetworkKit;
+ sourceTree = SOURCE_ROOT;
+ };
+ 5044FEBF14EFD3C60015F359 /* Categories */ = {
+ isa = PBXGroup;
+ children = (
+ 5044FEC014EFD3C60015F359 /* NSAlert+MKNetworkKitAdditions.h */,
+ 5044FEC114EFD3C60015F359 /* NSAlert+MKNetworkKitAdditions.m */,
+ 5044FEC214EFD3C60015F359 /* NSData+Base64.h */,
+ 5044FEC314EFD3C60015F359 /* NSData+Base64.m */,
+ 5044FEC414EFD3C60015F359 /* NSDate+RFC1123.h */,
+ 5044FEC514EFD3C60015F359 /* NSDate+RFC1123.m */,
+ 5044FEC614EFD3C60015F359 /* NSDictionary+RequestEncoding.h */,
+ 5044FEC714EFD3C60015F359 /* NSDictionary+RequestEncoding.m */,
+ 5044FEC814EFD3C60015F359 /* NSString+MKNetworkKitAdditions.h */,
+ 5044FEC914EFD3C60015F359 /* NSString+MKNetworkKitAdditions.m */,
+ 5044FECA14EFD3C60015F359 /* UIAlertView+MKNetworkKitAdditions.h */,
+ 5044FECB14EFD3C60015F359 /* UIAlertView+MKNetworkKitAdditions.m */,
+ );
+ path = Categories;
+ sourceTree = "<group>";
+ };
+ 5044FED114EFD3C60015F359 /* Reachability */ = {
+ isa = PBXGroup;
+ children = (
+ 5044FED214EFD3C60015F359 /* Reachability.h */,
+ 5044FED314EFD3C60015F359 /* Reachability.m */,
+ );
+ path = Reachability;
+ sourceTree = "<group>";
+ };
50AAF39614EEC0F8009F3BFE = {
isa = PBXGroup;
children = (
+ 5044FEDF14EFD4970015F359 /* SystemConfiguration.framework */,
+ 5044FEDD14EFD4910015F359 /* Security.framework */,
50AAF3AA14EEC0F8009F3BFE /* 500px Aperture Uploader */,
50AAF3A314EEC0F8009F3BFE /* Frameworks */,
50AAF3A214EEC0F8009F3BFE /* Products */,
@@ -171,11 +270,11 @@
50AAF3AA14EEC0F8009F3BFE /* 500px Aperture Uploader */ = {
isa = PBXGroup;
children = (
- 50AAF3C214EEC3F1009F3BFE /* ApertureExportManager.h */,
- 50AAF3C314EEC3F1009F3BFE /* ApertureExportPlugIn.h */,
50AAF3BC14EEC1B0009F3BFE /* FiveHundredPxApertureExporter.h */,
50AAF3BD14EEC1B0009F3BFE /* FiveHundredPxApertureExporter.m */,
5044FE7214EFD0080015F359 /* FiveHundredPxApertureExporter.xib */,
+ 5044FEE114EFD6D30015F359 /* FiveHundredPxOAuthEngine.h */,
+ 5044FEE214EFD6D30015F359 /* FiveHundredPxOAuthEngine.m */,
5044FE7614EFD12B0015F359 /* External */,
50AAF3AB14EEC0F8009F3BFE /* Supporting Files */,
);
@@ -265,6 +364,17 @@
5044FE9414EFD1380015F359 /* CJSONDeserializer.m in Sources */,
5044FE9514EFD1380015F359 /* CJSONScanner.m in Sources */,
5044FE9614EFD1380015F359 /* CJSONSerializer.m in Sources */,
+ 5044FEBD14EFD3BD0015F359 /* RSOAuthEngine.m in Sources */,
+ 5044FED414EFD3C60015F359 /* NSAlert+MKNetworkKitAdditions.m in Sources */,
+ 5044FED514EFD3C60015F359 /* NSData+Base64.m in Sources */,
+ 5044FED614EFD3C60015F359 /* NSDate+RFC1123.m in Sources */,
+ 5044FED714EFD3C60015F359 /* NSDictionary+RequestEncoding.m in Sources */,
+ 5044FED814EFD3C60015F359 /* NSString+MKNetworkKitAdditions.m in Sources */,
+ 5044FED914EFD3C60015F359 /* UIAlertView+MKNetworkKitAdditions.m in Sources */,
+ 5044FEDA14EFD3C60015F359 /* MKNetworkEngine.m in Sources */,
+ 5044FEDB14EFD3C60015F359 /* MKNetworkOperation.m in Sources */,
+ 5044FEDC14EFD3C60015F359 /* Reachability.m in Sources */,
+ 5044FEE314EFD6D30015F359 /* FiveHundredPxOAuthEngine.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
3 500px Aperture Uploader/FiveHundredPxApertureExporter.m
@@ -7,6 +7,7 @@
//
#import "FiveHundredPxApertureExporter.h"
+#import "FiveHundredPxOAuthEngine.h"
@implementation FiveHundredPxApertureExporter {
ApertureExportProgress exportProgress;
@@ -43,7 +44,7 @@ -(id)initWithAPIManager:(id <PROAPIAccessing>)anApiManager {
}
-(void)awakeFromNib {
- @synchronized(exportManager) {
+ @synchronized(self.exportManager) {
//[[self.movieNameField cell] setPlaceholderString:[[self.exportManager propertiesWithoutThumbnailForImageAtIndex:0] valueForKey:kExportKeyProjectName]];
}
}
View
39 500px Aperture Uploader/FiveHundredPxOAuthEngine.h
@@ -0,0 +1,39 @@
+//
+// FiveHundredPxOAuthEngine.h
+// Demo
+//
+// Created by Daniel Kennett on 17/02/2012.
+// Copyright (c) 2012 KennettNet Software Limited. All rights reserved.
+//
+
+#import "RSOAuthEngine.h"
+
+@protocol FiveHundredPxEngineDelegate;
+
+typedef void (^FiveHundredPxCompletionBlock)(NSError *error);
+typedef void (^FiveHundredPxCompletionWithValueBlock)(NSDictionary *returnValue, NSError *error);
+
+@interface FiveHundredPxOAuthEngine : RSOAuthEngine
+
+@property (weak, nonatomic, readwrite) id <FiveHundredPxEngineDelegate> delegate;
+@property (strong, readonly, nonatomic) NSString *screenName;
+@property (readonly, nonatomic, strong) MKNetworkEngine *fileUploadEngine;
+
+- (id)initWithDelegate:(id <FiveHundredPxEngineDelegate>)delegate;
+- (void)authenticateWithUsername:(NSString *)username password:(NSString *)password;
+- (void)authenticateWithCompletionBlock:(FiveHundredPxCompletionBlock)completionBlock;
+- (void)cancelAuthentication;
+- (void)forgetStoredToken;
+
+-(void)getPhotosForLoggedInUser:(FiveHundredPxCompletionWithValueBlock)block;
+-(void)getDetailsForLoggedInUser:(FiveHundredPxCompletionWithValueBlock)block;
+-(void)uploadPhoto:(NSData *)jpgData withTitle:(NSString *)title description:(NSString *)desc uploadProgressBlock:(MKNKProgressBlock)progressBlock completionBlock:(FiveHundredPxCompletionWithValueBlock)block;
+@end
+
+@protocol FiveHundredPxEngineDelegate <NSObject>
+
+- (void)fiveHundredPxNeedsAuthentication:(FiveHundredPxOAuthEngine *)engine;
+- (void)fiveHundredPx:(FiveHundredPxOAuthEngine *)engine statusUpdate:(NSString *)message;
+
+
+@end
View
358 500px Aperture Uploader/FiveHundredPxOAuthEngine.m
@@ -0,0 +1,358 @@
+//
+// FiveHundredPxOAuthEngine.m
+// Demo
+//
+// Created by Daniel Kennett on 17/02/2012.
+// Copyright (c) 2012 KennettNet Software Limited. All rights reserved.
+//
+
+// Note: This file is basically a copy+paste of the Instapaper
+// demo project that comes with RSOAuthEngine.
+
+#import "FiveHundredPxOAuthEngine.h"
+#import "CJSONDeserializer.h"
+
+@interface FiveHundredPxOAuthEngine ()
+
+- (void)removeOAuthTokenFromKeychain;
+- (void)storeOAuthTokenInKeychain;
+- (void)retrieveOAuthTokenFromKeychain;
+
+@property (readwrite, nonatomic, strong) FiveHundredPxCompletionBlock completionBlock;
+@property (strong, readwrite, nonatomic) NSString *screenName;
+@property (readwrite, nonatomic, strong) MKNetworkEngine *fileUploadEngine;
+
+@end
+
+@implementation FiveHundredPxOAuthEngine
+
+NSString *k500pxConsumerKey;
+NSString *k500pxConsumerSecret;
+
+// Default hostname and paths
+static NSString * const k500pxHostname = @"api.500px.com";
+static NSString * const k500pxGetAccessTokenPath = @"v1/oauth/access_token";
+static NSString * const k500pxGetRequestTokenPath = @"v1/oauth/request_token";
+static NSString * const k500pxGetPhotosPath = @"v1/photos";
+static NSString * const k500pxGetUserDetailsPath = @"v1/users";
+static NSString * const k500pxPostPhotoPath = @"v1/photos";
+static NSString * const k500pxUploadPhotoPath = @"v1/upload";
+
+#pragma mark - Initialization
+
+- (id)initWithDelegate:(id <FiveHundredPxEngineDelegate>)delegate
+{
+ self = [super initWithHostName:k500pxHostname
+ customHeaderFields:nil
+ signatureMethod:RSOAuthHMAC_SHA1
+ consumerKey:k500pxConsumerKey
+ consumerSecret:k500pxConsumerSecret
+ callbackURL:@"http://authLocal/auth"];
+
+
+
+ if (self) {
+ self.delegate = delegate;
+ self.fileUploadEngine = [[MKNetworkEngine alloc] initWithHostName:k500pxHostname
+ customHeaderFields:nil];
+
+ // Retrieve OAuth access token (if previously stored)
+ [self retrieveOAuthTokenFromKeychain];
+ }
+
+ return self;
+}
+
+@synthesize completionBlock;
+@synthesize screenName;
+@synthesize delegate;
+@synthesize fileUploadEngine;
+
+#pragma mark - OAuth Access Token store/retrieve
+
+- (void)removeOAuthTokenFromKeychain
+{
+ // Build the keychain query
+ NSMutableDictionary *keychainQuery = [NSMutableDictionary dictionaryWithObjectsAndKeys:
+ (__bridge_transfer NSString *)kSecClassGenericPassword, (__bridge_transfer NSString *)kSecClass,
+ self.consumerKey, kSecAttrService,
+ self.consumerKey, kSecAttrAccount,
+ kCFBooleanTrue, kSecReturnAttributes,
+ nil];
+
+ // If there's a token stored for this user, delete it
+ SecItemDelete((__bridge_retained CFDictionaryRef) keychainQuery);
+}
+
+- (void)storeOAuthTokenInKeychain
+{
+ // Build the keychain query
+ NSMutableDictionary *keychainQuery = [NSMutableDictionary dictionaryWithObjectsAndKeys:
+ (__bridge_transfer NSString *)kSecClassGenericPassword, (__bridge_transfer NSString *)kSecClass,
+ self.consumerKey, kSecAttrService,
+ self.consumerKey, kSecAttrAccount,
+ kCFBooleanTrue, kSecReturnAttributes,
+ nil];
+
+ CFTypeRef resData = NULL;
+
+ // If there's a token stored for this user, delete it first
+ SecItemDelete((__bridge_retained CFDictionaryRef) keychainQuery);
+
+ // Build the token dictionary
+ NSMutableDictionary *tokenDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
+ self.token, @"oauth_token",
+ self.tokenSecret, @"oauth_token_secret",
+ self.screenName, @"screen_name",
+ nil];
+
+ // Add the token dictionary to the query
+ [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:tokenDictionary]
+ forKey:(__bridge_transfer NSString *)kSecValueData];
+
+ // Add the token data to the keychain
+ // Even if we never use resData, replacing with NULL in the call throws EXC_BAD_ACCESS
+ SecItemAdd((__bridge_retained CFDictionaryRef)keychainQuery, (CFTypeRef *) &resData);
+}
+
+- (void)retrieveOAuthTokenFromKeychain
+{
+ // Build the keychain query
+ NSMutableDictionary *keychainQuery = [NSMutableDictionary dictionaryWithObjectsAndKeys:
+ (__bridge_transfer NSString *)kSecClassGenericPassword, (__bridge_transfer NSString *)kSecClass,
+ self.consumerKey, kSecAttrService,
+ self.consumerKey, kSecAttrAccount,
+ kCFBooleanTrue, kSecReturnData,
+ kSecMatchLimitOne, kSecMatchLimit,
+ nil];
+
+ // Get the token data from the keychain
+ CFTypeRef resData = NULL;
+
+ // Get the token dictionary from the keychain
+ if (SecItemCopyMatching((__bridge_retained CFDictionaryRef) keychainQuery, (CFTypeRef *) &resData) == noErr)
+ {
+ NSData *resultData = (__bridge_transfer NSData *)resData;
+
+ if (resultData)
+ {
+ NSMutableDictionary *tokenDictionary = [NSKeyedUnarchiver unarchiveObjectWithData:resultData];
+
+ if (tokenDictionary) {
+ [self setAccessToken:[tokenDictionary objectForKey:@"oauth_token"]
+ secret:[tokenDictionary objectForKey:@"oauth_token_secret"]];
+
+ self.screenName = [tokenDictionary objectForKey:@"screen_name"];
+ }
+ }
+ }
+}
+
+#pragma mark - OAuth Authentication Flow
+
+- (void)authenticateWithCompletionBlock:(FiveHundredPxCompletionBlock)block
+{
+ // First we reset the OAuth token, so we won't send previous tokens in the request
+ [self resetOAuthToken];
+
+ // Store the Completion Block to call after Authenticated
+ self.completionBlock = [block copy];
+
+ [self.delegate fiveHundredPx:self statusUpdate:@"Waiting for user authorization..."];
+ [self.delegate fiveHundredPxNeedsAuthentication:self];
+}
+
+- (void)authenticateWithUsername:(NSString *)username password:(NSString *)password
+{
+
+ // Fill the post body with the xAuth parameters
+ NSMutableDictionary *postParams = [NSMutableDictionary dictionaryWithObjectsAndKeys:
+ username, @"x_auth_username",
+ password, @"x_auth_password",
+ @"client_auth", @"x_auth_mode",
+ nil];
+
+ MKNetworkOperation *requestTokenOp = [self operationWithPath:k500pxGetRequestTokenPath
+ params:nil
+ httpMethod:@"POST"
+ ssl:YES];
+
+ [requestTokenOp onCompletion:^(MKNetworkOperation *completedOperation)
+ {
+ [self fillTokenWithResponseBody:completedOperation.responseString type:RSOAuthAccessToken];
+
+ // Now, request PW
+
+ MKNetworkOperation *op = [self operationWithPath:k500pxGetAccessTokenPath
+ params:postParams
+ httpMethod:@"POST"
+ ssl:YES];
+
+ [op onCompletion:^(MKNetworkOperation *completedOperation)
+ {
+ // Fill the access token with the returned data
+ [self fillTokenWithResponseBody:[completedOperation responseString] type:RSOAuthAccessToken];
+
+ // Set the user's screen name
+ self.screenName = [username copy];
+
+ // Store the OAuth access token
+ [self storeOAuthTokenInKeychain];
+
+ // Finished, return to previous method
+ if (self.completionBlock) self.completionBlock(nil);
+ self.completionBlock = nil;
+ }
+ onError:^(NSError *error)
+ {
+ if (self.completionBlock) self.completionBlock(error);
+ self.completionBlock = nil;
+ }];
+
+ [self.delegate fiveHundredPx:self statusUpdate:@"Authenticating..."];
+ [self enqueueSignedOperation:op];
+
+ }
+ onError:^(NSError *error)
+ {
+ if (self.completionBlock) self.completionBlock(error);
+ self.completionBlock = nil;
+ }];
+
+ [self enqueueSignedOperation:requestTokenOp];
+
+}
+
+- (void)cancelAuthentication
+{
+ NSDictionary *ui = [NSDictionary dictionaryWithObjectsAndKeys:@"Authentication cancelled.", NSLocalizedDescriptionKey, nil];
+ NSError *error = [NSError errorWithDomain:@"org.danielkennett.500px.ErrorDomain" code:401 userInfo:ui];
+
+ if (self.completionBlock) self.completionBlock(error);
+ self.completionBlock = nil;
+}
+
+- (void)forgetStoredToken
+{
+ [self removeOAuthTokenFromKeychain];
+ [self resetOAuthToken];
+ self.screenName = nil;
+}
+
+#pragma mark - Public Methods
+
+-(void)performAuthenticatedRequestToPath:(NSString *)path
+ parameters:(NSMutableDictionary *)parameters
+ method:(NSString *)httpMethod
+ callbackBlock:(FiveHundredPxCompletionWithValueBlock)block {
+
+
+ if (!self.isAuthenticated) {
+
+ [self authenticateWithCompletionBlock:^(NSError *error) {
+ if (error) {
+ // Authentication failed, return the error
+ block(nil, error);
+ } else {
+ // Authentication succeeded, call this method again
+ [self performAuthenticatedRequestToPath:path
+ parameters:parameters
+ method:httpMethod
+ callbackBlock:block];
+ }
+ }];
+
+ // This method will be called again once the authentication completes
+ return;
+ }
+
+ MKNetworkOperation *op = [self operationWithPath:path
+ params:parameters
+ httpMethod:httpMethod
+ ssl:YES];
+
+ [op onCompletion:^(MKNetworkOperation *completedOperation) {
+ NSDictionary *dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:completedOperation.responseData
+ error:nil];
+ block(dict, nil);
+ } onError:^(NSError *error) {
+ block(nil, error);
+ }];
+
+ [self enqueueSignedOperation:op];
+}
+
+-(void)getPhotosForLoggedInUser:(FiveHundredPxCompletionWithValueBlock)block {
+
+ NSMutableDictionary *postParams = [NSMutableDictionary dictionaryWithObjectsAndKeys:
+ self.screenName, @"username",
+ @"user", @"feature",
+ self.consumerKey, @"consumer_key",
+ nil];
+
+ [self performAuthenticatedRequestToPath:k500pxGetPhotosPath
+ parameters:postParams
+ method:@"GET"
+ callbackBlock:block];
+}
+
+-(void)getDetailsForLoggedInUser:(FiveHundredPxCompletionWithValueBlock)block {
+
+ [self performAuthenticatedRequestToPath:k500pxGetUserDetailsPath
+ parameters:nil
+ method:@"GET"
+ callbackBlock:block];
+}
+
+-(void)uploadPhoto:(NSData *)jpgData withTitle:(NSString *)title description:(NSString *)desc uploadProgressBlock:(MKNKProgressBlock)progressBlock completionBlock:(FiveHundredPxCompletionWithValueBlock)block {
+
+ NSMutableDictionary *postParams = [NSMutableDictionary dictionaryWithObjectsAndKeys:
+ title, @"name",
+ desc, @"description",
+ nil];
+
+
+ [self performAuthenticatedRequestToPath:k500pxPostPhotoPath
+ parameters:postParams
+ method:@"POST"
+ callbackBlock:^(NSDictionary *returnValue, NSError *error) {
+
+ if (error == nil) {
+
+ NSString *photoId = [[[returnValue valueForKey:@"photo"] valueForKey:@"id"] stringValue];
+ NSString *uploadKey = [returnValue valueForKey:@"upload_key"];
+
+ NSMutableDictionary *postParams = [NSMutableDictionary dictionaryWithObjectsAndKeys:
+ photoId, @"photo_id",
+ self.consumerKey, @"consumer_key",
+ self.token, @"access_key",
+ uploadKey, @"upload_key",
+ nil];
+
+ MKNetworkOperation *op = [self.fileUploadEngine operationWithPath:k500pxUploadPhotoPath
+ params:postParams
+ httpMethod:@"POST"
+ ssl:YES];
+ [op addData:jpgData
+ forKey:@"file"];
+
+ [op onCompletion:^(MKNetworkOperation *completedOperation) {
+ NSDictionary *dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:completedOperation.responseData
+ error:nil];
+ block(dict, nil);
+ } onError:^(NSError *error) {
+ block(nil, error);
+ }];
+
+ [op onUploadProgressChanged:progressBlock];
+
+ [self.fileUploadEngine enqueueOperation:op];
+
+ } else {
+ block(nil, error);
+ }
+ }];
+
+}
+
+@end
1 RSOAuthEngine
@@ -0,0 +1 @@
+Subproject commit f3ece0825d2633bdf25424af9c4118659617e9a7

0 comments on commit c1e72d8

Please sign in to comment.
Something went wrong with that request. Please try again.