From 18a4d4f1b4983006dc18a7a93e0c81356fc61022 Mon Sep 17 00:00:00 2001 From: Julien Seren-Rosso Date: Fri, 14 Feb 2014 15:30:19 +0100 Subject: [PATCH 1/3] added Facebook SSO --- SimpleAuth.xcodeproj/project.pbxproj | 17 +++++++++++++++++ SimpleAuthDemo/SADAppDelegate.m | 11 +++++++++++ SimpleAuthDemo/SADProviderListViewController.m | 3 ++- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/SimpleAuth.xcodeproj/project.pbxproj b/SimpleAuth.xcodeproj/project.pbxproj index ac7fb23..a5d1943 100644 --- a/SimpleAuth.xcodeproj/project.pbxproj +++ b/SimpleAuth.xcodeproj/project.pbxproj @@ -37,6 +37,8 @@ 81A50A491892649300D5D8AC /* SimpleAuthLinkedInWebProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = 81A50A481892649300D5D8AC /* SimpleAuthLinkedInWebProvider.m */; }; 81A50A4C189264C500D5D8AC /* SimpleAuthLinkedInWebLoginViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 81A50A4B189264C500D5D8AC /* SimpleAuthLinkedInWebLoginViewController.m */; }; F435E27FC62B4584BF9DBF2C /* libPods.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 0D4998CFA03844FB99EE78AA /* libPods.a */; }; + F910228918A9445100A0216E /* SimpleAuthSingleSignOnFacebookProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = F910228718A9444800A0216E /* SimpleAuthSingleSignOnFacebookProvider.m */; }; + F978D08018AE382600017ED5 /* SimpleAuthSingleSignOnProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = F978D07F18AE382600017ED5 /* SimpleAuthSingleSignOnProvider.m */; }; F9BE4F3E189139E900AE303A /* SimpleAuthFoursquareWebProvider.m in Sources */ = {isa = PBXBuildFile; fileRef = F9BE4F34189120DE00AE303A /* SimpleAuthFoursquareWebProvider.m */; }; F9BE4F3F189139F200AE303A /* SimpleAuthFoursquareWebLoginViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = F9BE4F3A1891308200AE303A /* SimpleAuthFoursquareWebLoginViewController.m */; }; /* End PBXBuildFile section */ @@ -116,6 +118,10 @@ 81A50A481892649300D5D8AC /* SimpleAuthLinkedInWebProvider.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SimpleAuthLinkedInWebProvider.m; sourceTree = ""; }; 81A50A4A189264C500D5D8AC /* SimpleAuthLinkedInWebLoginViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SimpleAuthLinkedInWebLoginViewController.h; sourceTree = ""; }; 81A50A4B189264C500D5D8AC /* SimpleAuthLinkedInWebLoginViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SimpleAuthLinkedInWebLoginViewController.m; sourceTree = ""; }; + F910228618A9444800A0216E /* SimpleAuthSingleSignOnFacebookProvider.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SimpleAuthSingleSignOnFacebookProvider.h; sourceTree = ""; }; + F910228718A9444800A0216E /* SimpleAuthSingleSignOnFacebookProvider.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SimpleAuthSingleSignOnFacebookProvider.m; sourceTree = ""; }; + F978D07E18AE382600017ED5 /* SimpleAuthSingleSignOnProvider.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SimpleAuthSingleSignOnProvider.h; sourceTree = ""; }; + F978D07F18AE382600017ED5 /* SimpleAuthSingleSignOnProvider.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SimpleAuthSingleSignOnProvider.m; sourceTree = ""; }; F9BE4F33189120DE00AE303A /* SimpleAuthFoursquareWebProvider.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SimpleAuthFoursquareWebProvider.h; sourceTree = ""; }; F9BE4F34189120DE00AE303A /* SimpleAuthFoursquareWebProvider.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SimpleAuthFoursquareWebProvider.m; sourceTree = ""; }; F9BE4F391891308200AE303A /* SimpleAuthFoursquareWebLoginViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SimpleAuthFoursquareWebLoginViewController.h; sourceTree = ""; }; @@ -145,6 +151,7 @@ 3B52BB8D1887088400C73329 /* Providers */ = { isa = PBXGroup; children = ( + F910228518A943F100A0216E /* FacebookSSO */, 3B52BB9218871F6200C73329 /* Facebook */, 3B66656918903F4000F3BF58 /* Facebook Web */, 3B52BB8E1887088400C73329 /* Twitter */, @@ -369,6 +376,15 @@ path = LinkedIn; sourceTree = ""; }; + F910228518A943F100A0216E /* FacebookSSO */ = { + isa = PBXGroup; + children = ( + F910228618A9444800A0216E /* SimpleAuthSingleSignOnFacebookProvider.h */, + F910228718A9444800A0216E /* SimpleAuthSingleSignOnFacebookProvider.m */, + ); + path = FacebookSSO; + sourceTree = ""; + }; F9BE4F32189120BE00AE303A /* Foursquare Web */ = { isa = PBXGroup; children = ( @@ -517,6 +533,7 @@ 3B6584631888ABFB00D59100 /* SimpleAuthTumblrLoginViewController.m in Sources */, 3B52BB9518871F6200C73329 /* SimpleAuthFacebookProvider.m in Sources */, 3B9AB064182AC2710011FB9E /* SimpleAuthProvider.m in Sources */, + F910228918A9445100A0216E /* SimpleAuthSingleSignOnFacebookProvider.m in Sources */, 3B52BB9B188731A300C73329 /* SimpleAuthInstagramLoginViewController.m in Sources */, F9BE4F3F189139F200AE303A /* SimpleAuthFoursquareWebLoginViewController.m in Sources */, 3C0A5393182C4217002C050C /* SimpleAuthWebViewController.m in Sources */, diff --git a/SimpleAuthDemo/SADAppDelegate.m b/SimpleAuthDemo/SADAppDelegate.m index b5d5b35..5371069 100644 --- a/SimpleAuthDemo/SADAppDelegate.m +++ b/SimpleAuthDemo/SADAppDelegate.m @@ -67,6 +67,9 @@ - (void)configureAuthorizaionProviders { // client_id, client_secret, and and redirect_uri are required SimpleAuth.configuration[@"linkedin-web"] = @{}; + + // Request additional permissions with permissions (array) + SimpleAuth.configuration[@"facebook-sso"] = @{}; } @@ -78,4 +81,12 @@ - (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions: return YES; } +- (BOOL)application:(UIApplication *)application + openURL:(NSURL *)url + sourceApplication:(NSString *)sourceApplication + annotation:(id)annotation +{ + return [SimpleAuth handleCallback:url]; +} + @end diff --git a/SimpleAuthDemo/SADProviderListViewController.m b/SimpleAuthDemo/SADProviderListViewController.m index 4f8c7d0..8374a06 100644 --- a/SimpleAuthDemo/SADProviderListViewController.m +++ b/SimpleAuthDemo/SADProviderListViewController.m @@ -52,7 +52,8 @@ + (NSArray *)providers { @"tumblr", @"foursquare-web", @"dropbox-web", - @"linkedin-web" + @"linkedin-web", + @"facebook-sso" ]; }); return array; From fe630022c217fbb519c79d71501c661c55afc3a9 Mon Sep 17 00:00:00 2001 From: Julien Seren-Rosso Date: Fri, 14 Feb 2014 16:25:11 +0100 Subject: [PATCH 2/3] forgotten files --- .../SimpleAuthSingleSignOnFacebookProvider.h | 23 ++++ .../SimpleAuthSingleSignOnFacebookProvider.m | 115 ++++++++++++++++++ .../SADProviderListViewController.m | 2 +- 3 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 Providers/FacebookSSO/SimpleAuthSingleSignOnFacebookProvider.h create mode 100644 Providers/FacebookSSO/SimpleAuthSingleSignOnFacebookProvider.m diff --git a/Providers/FacebookSSO/SimpleAuthSingleSignOnFacebookProvider.h b/Providers/FacebookSSO/SimpleAuthSingleSignOnFacebookProvider.h new file mode 100644 index 0000000..bd9ff17 --- /dev/null +++ b/Providers/FacebookSSO/SimpleAuthSingleSignOnFacebookProvider.h @@ -0,0 +1,23 @@ +// +// SimpleAuthSingleSignOnFacebookProvider.h +// SimpleAuth +// +// Created by Julien Seren-Rosso on 10/02/2014. +// Copyright (c) 2014 Byliner, Inc. All rights reserved. +// + +#import "SimpleAuthSingleSignOnProvider.h" + +// Facebook +#import + +extern NSString *const SimpleAuthSingleSignOnFacebookProviderDomain; + +enum { + SimpleAuthSingleSignOnFacebookProviderLoginFailed = 1, + SimpleAuthSingleSignOnFacebookProviderFacebookError +}; + +@interface SimpleAuthSingleSignOnFacebookProvider : SimpleAuthSingleSignOnProvider + +@end diff --git a/Providers/FacebookSSO/SimpleAuthSingleSignOnFacebookProvider.m b/Providers/FacebookSSO/SimpleAuthSingleSignOnFacebookProvider.m new file mode 100644 index 0000000..8b07b3d --- /dev/null +++ b/Providers/FacebookSSO/SimpleAuthSingleSignOnFacebookProvider.m @@ -0,0 +1,115 @@ +// +// SimpleAuthSingleSignOnFacebookProvider.m +// SimpleAuth +// +// Created by Julien Seren-Rosso on 10/02/2014. +// Copyright (c) 2014 Byliner, Inc. All rights reserved. +// + +#import "SimpleAuthSingleSignOnFacebookProvider.h" +#import "SimpleAuth.h" + +NSString *const SimpleAuthSingleSignOnFacebookProviderDomain = @"io.simpleauth.sso.facebook"; + +@interface SimpleAuthSingleSignOnFacebookProvider () + +@property (nonatomic, strong) SimpleAuthRequestHandler completion; + +@end + + +@implementation SimpleAuthSingleSignOnFacebookProvider + +#pragma mark - SimpleAuthProvider + ++ (NSString *)type { + return @"facebook-sso"; +} + ++ (NSDictionary *)defaultOptions { + return @{ + @"permissions" : @[ @"basic_info" ] + }; +} + + +- (void)authorizeWithCompletion:(SimpleAuthRequestHandler)completion +{ + self.completion = completion; + + [FBSession openActiveSessionWithReadPermissions:self.options[@"permission"] allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) { + [self sessionStateChanged:session state:status error:error]; + + // Immediately close the session (if successfully opened) in order to retrieve a new token next time + if ([session isOpen]) { + [FBSession.activeSession closeAndClearTokenInformation]; + } + }]; +} + + +#pragma mark - Facebook SSO + +- (BOOL)handleCallback:(NSURL *)url +{ + return [FBSession.activeSession handleOpenURL:url]; +} + +- (void)sessionStateChanged:(FBSession *)session state:(FBSessionState) state error:(NSError *)error +{ + // If the session was opened successfully + if (!error && state == FBSessionStateOpen) { + NSLog(@"Session opened"); + + // Retrieve the information from the sesssion + self.completion([self dictionaryWithFBSession:session], nil); + return; + } + + if (state == FBSessionStateClosed) { + NSLog(@"Session closed"); + // The session was closed, do nothing + return; + } + + if (state == FBSessionStateClosedLoginFailed) { + NSLog(@"Login failed"); + + // Something wrong happened + NSError *error = [NSError errorWithDomain:SimpleAuthSingleSignOnFacebookProviderDomain + code:SimpleAuthSingleSignOnFacebookProviderLoginFailed + userInfo:@{ NSLocalizedDescriptionKey: @"Something went wrong, try again." }]; + + self.completion(nil, error); + return; + } + + // Handle errors + if (error) { + NSLog(@"Facebook error"); + NSError *ssoError = [NSError errorWithDomain:SimpleAuthSingleSignOnFacebookProviderDomain + code:SimpleAuthSingleSignOnFacebookProviderFacebookError + userInfo:@{ + NSLocalizedDescriptionKey: @"Something went wrong, try again.", + NSUnderlyingErrorKey: error + }]; + + self.completion(nil, ssoError); + } +} + + +- (NSDictionary *)dictionaryWithFBSession:(FBSession *)session +{ + NSMutableDictionary *dictionary = [NSMutableDictionary new]; + + // Provider + dictionary[@"provider"] = [[self class] type]; + + // Credentials + dictionary[@"credentials"] = @{ @"token" : session.accessTokenData.accessToken }; + + return dictionary; +} + +@end diff --git a/SimpleAuthDemo/SADProviderListViewController.m b/SimpleAuthDemo/SADProviderListViewController.m index 8374a06..b27b0ad 100644 --- a/SimpleAuthDemo/SADProviderListViewController.m +++ b/SimpleAuthDemo/SADProviderListViewController.m @@ -86,7 +86,7 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath NSString *provider = [[self class] providers][indexPath.row]; NSDictionary *configuration = SimpleAuth.configuration[provider]; - if ([configuration count] == 0) { + if ([configuration count] == 0 && ![provider isEqualToString:@"facebook-sso"]) { NSLog(@"It looks like you haven't configured the \"%@\" provider.\n" "Consider calling +[SimpleAuth configuration] in `application:willFinishLaunchingWithOptions: " "and providing all relevant options for the given provider.", From 23d23a1ae47db93c47e8e69c21f0d278805cb1b3 Mon Sep 17 00:00:00 2001 From: Julien Seren-Rosso Date: Fri, 14 Feb 2014 16:39:26 +0100 Subject: [PATCH 3/3] updated podfile and podspec --- Podfile | 4 +++- Podfile.lock | 3 +++ SimpleAuth.podspec | 8 ++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Podfile b/Podfile index 636556f..1a250f5 100644 --- a/Podfile +++ b/Podfile @@ -1,3 +1,5 @@ platform :ios, '6.0' -podspec :name => 'SimpleAuth' \ No newline at end of file +podspec :name => 'SimpleAuth' + +pod 'Facebook-iOS-SDK', '~> 3.12.0' \ No newline at end of file diff --git a/Podfile.lock b/Podfile.lock index 86b53b7..bec2dc7 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -2,6 +2,7 @@ PODS: - CMDQueryStringSerialization (0.1.0) - cocoa-oauth (0.0.1): - NSData+Base64 (~> 1.0) + - Facebook-iOS-SDK (3.12.0) - NSData+Base64 (1.0.0) - ReactiveCocoa (2.2.3): - ReactiveCocoa/Core @@ -13,11 +14,13 @@ PODS: DEPENDENCIES: - CMDQueryStringSerialization - cocoa-oauth + - Facebook-iOS-SDK (~> 3.12.0) - ReactiveCocoa SPEC CHECKSUMS: CMDQueryStringSerialization: f6118185b92689188026e648c4eb5e2c87b5835b cocoa-oauth: 8f4c8b77c77ac660de37f8125557c4ec09b0118a + Facebook-iOS-SDK: c09f7d80db5c48170ff834bc03ebe774c49d1f5b NSData+Base64: 4819562faee4544e3cf5703f2139820b09732b28 ReactiveCocoa: b3cb6ebd50f1a0f2e39c6fd7ce3c428b6efcb22b diff --git a/SimpleAuth.podspec b/SimpleAuth.podspec index 5522c5e..40bb0dd 100644 --- a/SimpleAuth.podspec +++ b/SimpleAuth.podspec @@ -32,6 +32,14 @@ Pod::Spec.new do |s| ss.frameworks = 'Accounts', 'Social' end + s.subspec 'FacebookSSO' do |ss| + ss.dependency 'SimpleAuth/Core' + ss.dependency 'Facebook-iOS-SDK' + + ss.source_files = 'Providers/FacebookSSO/**/*.{h,m}' + ss.frameworks = 'Accounts', 'Social', 'UIKit' + end + s.subspec 'FacebookWeb' do |ss| ss.dependency 'SimpleAuth/Core'