Skip to content
This repository has been archived by the owner on Aug 16, 2023. It is now read-only.

Facebook sso #44

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Podfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
platform :ios, '6.0'

podspec :name => 'SimpleAuth'
podspec :name => 'SimpleAuth'

pod 'Facebook-iOS-SDK', '~> 3.12.0'
3 changes: 3 additions & 0 deletions Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
24 changes: 24 additions & 0 deletions Providers/FacebookSSO/SimpleAuthSingleSignOnFacebookProvider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// SimpleAuthSingleSignOnFacebookProvider.h
// SimpleAuth
//
// Created by Julien Seren-Rosso on 10/02/2014.
// Copyright (c) 2014 Byliner, Inc. All rights reserved.
//

#import "SimpleAuthProvider.h"
#import "SimpleAuthSingleSignOnProvider.h"

// Facebook
#import <FacebookSDK/FacebookSDK.h>

extern NSString *const SimpleAuthSingleSignOnFacebookProviderDomain;

enum {
SimpleAuthSingleSignOnFacebookProviderLoginFailed = 1,
SimpleAuthSingleSignOnFacebookProviderFacebookError
};

@interface SimpleAuthSingleSignOnFacebookProvider : SimpleAuthProvider <SimpleAuthSingleSignOnProvider>

@end
115 changes: 115 additions & 0 deletions Providers/FacebookSSO/SimpleAuthSingleSignOnFacebookProvider.m
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions SimpleAuth.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
14 changes: 14 additions & 0 deletions SimpleAuth.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
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 */; };
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 */
Expand Down Expand Up @@ -116,6 +117,8 @@
81A50A481892649300D5D8AC /* SimpleAuthLinkedInWebProvider.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SimpleAuthLinkedInWebProvider.m; sourceTree = "<group>"; };
81A50A4A189264C500D5D8AC /* SimpleAuthLinkedInWebLoginViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SimpleAuthLinkedInWebLoginViewController.h; sourceTree = "<group>"; };
81A50A4B189264C500D5D8AC /* SimpleAuthLinkedInWebLoginViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SimpleAuthLinkedInWebLoginViewController.m; sourceTree = "<group>"; };
F910228618A9444800A0216E /* SimpleAuthSingleSignOnFacebookProvider.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SimpleAuthSingleSignOnFacebookProvider.h; sourceTree = "<group>"; };
F910228718A9444800A0216E /* SimpleAuthSingleSignOnFacebookProvider.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SimpleAuthSingleSignOnFacebookProvider.m; sourceTree = "<group>"; };
F978D07E18AE382600017ED5 /* SimpleAuthSingleSignOnProvider.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SimpleAuthSingleSignOnProvider.h; sourceTree = "<group>"; };
F9BE4F33189120DE00AE303A /* SimpleAuthFoursquareWebProvider.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SimpleAuthFoursquareWebProvider.h; sourceTree = "<group>"; };
F9BE4F34189120DE00AE303A /* SimpleAuthFoursquareWebProvider.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SimpleAuthFoursquareWebProvider.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -146,6 +149,7 @@
3B52BB8D1887088400C73329 /* Providers */ = {
isa = PBXGroup;
children = (
F910228518A943F100A0216E /* FacebookSSO */,
3B52BB9218871F6200C73329 /* Facebook */,
3B66656918903F4000F3BF58 /* Facebook Web */,
3B52BB8E1887088400C73329 /* Twitter */,
Expand Down Expand Up @@ -371,6 +375,15 @@
path = LinkedIn;
sourceTree = "<group>";
};
F910228518A943F100A0216E /* FacebookSSO */ = {
isa = PBXGroup;
children = (
F910228618A9444800A0216E /* SimpleAuthSingleSignOnFacebookProvider.h */,
F910228718A9444800A0216E /* SimpleAuthSingleSignOnFacebookProvider.m */,
);
path = FacebookSSO;
sourceTree = "<group>";
};
F9BE4F32189120BE00AE303A /* Foursquare Web */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -519,6 +532,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 */,
Expand Down
11 changes: 11 additions & 0 deletions SimpleAuthDemo/SADAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -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"] = @{};
}


Expand All @@ -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
5 changes: 3 additions & 2 deletions SimpleAuthDemo/SADProviderListViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ + (NSArray *)providers {
@"tumblr",
@"foursquare-web",
@"dropbox-web",
@"linkedin-web"
@"linkedin-web",
@"facebook-sso"
];
});
return array;
Expand Down Expand Up @@ -85,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.",
Expand Down