Skip to content
This repository was archived by the owner on Jan 16, 2021. It is now read-only.

Commit d6fd99e

Browse files
committed
Refactor FacebookAuthenticationProvider into Mobile/Device to support tvOS.
1 parent 6bfa512 commit d6fd99e

16 files changed

+481
-218
lines changed

ParseFacebookUtils.xcodeproj/project.pbxproj

+56-18
Large diffs are not rendered by default.

ParseFacebookUtils/Internal/PFFacebookAuthenticationProvider.h renamed to ParseFacebookUtils/Internal/AuthenticationProvider/PFFacebookAuthenticationProvider.h

+6-7
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,17 @@
99

1010
#import <Foundation/Foundation.h>
1111

12-
#import <FBSDKLoginKit/FBSDKLoginManager.h>
12+
#import <Bolts/BFTask.h>
1313

1414
#import <Parse/PFConstants.h>
1515
#import <Parse/PFUserAuthenticationDelegate.h>
1616

17-
@class BFTask PF_GENERIC(__covariant BFGenericType);
18-
1917
NS_ASSUME_NONNULL_BEGIN
2018

2119
extern NSString *const PFFacebookUserAuthenticationType;
2220

2321
@interface PFFacebookAuthenticationProvider : NSObject <PFUserAuthenticationDelegate>
2422

25-
@property (nonatomic, strong, readonly) FBSDKLoginManager *loginManager;
26-
2723
///--------------------------------------
2824
/// @name Init
2925
///--------------------------------------
@@ -40,8 +36,11 @@ extern NSString *const PFFacebookUserAuthenticationType;
4036
/// @name Authenticate
4137
///--------------------------------------
4238

43-
- (BFTask *)authenticateAsyncWithReadPermissions:(nullable NSArray PF_GENERIC(NSString *) *)readPermissions
44-
publishPermissions:(nullable NSArray PF_GENERIC(NSString *) *)publishPermissions;
39+
- (BFTask PF_GENERIC(NSDictionary<NSString *, NSString *>*)*)authenticateAsyncWithReadPermissions:(nullable NSArray PF_GENERIC(NSString *)*)readPermissions
40+
publishPermissions:(nullable NSArray PF_GENERIC(NSString *)*)publishPermissions;
41+
- (BFTask PF_GENERIC(NSDictionary<NSString *, NSString *>*)*)authenticateAsyncWithReadPermissions:(nullable NSArray PF_GENERIC(NSString *)*)readPermissions
42+
publishPermissions:(nullable NSArray PF_GENERIC(NSString *)*)publishPermissions
43+
fromViewComtroller:(UIViewController *)viewController;
4544

4645
@end
4746

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* Copyright (c) 2015-present, Parse, LLC.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#import "PFFacebookAuthenticationProvider.h"
11+
12+
#import <FBSDKCoreKit/FBSDKAccessToken.h>
13+
#import <FBSDKCoreKit/FBSDKApplicationDelegate.h>
14+
15+
#import "PFFacebookPrivateUtilities.h"
16+
17+
NSString *const PFFacebookUserAuthenticationType = @"facebook";
18+
19+
@implementation PFFacebookAuthenticationProvider
20+
21+
///--------------------------------------
22+
#pragma mark - Init
23+
///--------------------------------------
24+
25+
- (instancetype)initWithApplication:(UIApplication *)application
26+
launchOptions:(nullable NSDictionary *)launchOptions {
27+
self = [super init];
28+
if (!self) return nil;
29+
30+
[[FBSDKApplicationDelegate sharedInstance] application:[UIApplication sharedApplication]
31+
didFinishLaunchingWithOptions:launchOptions];
32+
33+
return self;
34+
}
35+
36+
+ (instancetype)providerWithApplication:(UIApplication *)application
37+
launchOptions:(nullable NSDictionary *)launchOptions {
38+
return [[self alloc] initWithApplication:application launchOptions:launchOptions];
39+
}
40+
41+
///--------------------------------------
42+
#pragma mark - Authenticate
43+
///--------------------------------------
44+
45+
- (BFTask PF_GENERIC(NSDictionary<NSString *, NSString *>*)*)authenticateAsyncWithReadPermissions:(nullable NSArray PF_GENERIC(NSString *)*)readPermissions
46+
publishPermissions:(nullable NSArray PF_GENERIC(NSString *)*)publishPermissions {
47+
return [self authenticateAsyncWithReadPermissions:readPermissions
48+
publishPermissions:publishPermissions
49+
fromViewComtroller:[PFFacebookPrivateUtilities applicationTopViewController]];
50+
}
51+
52+
- (BFTask PF_GENERIC(NSDictionary<NSString *, NSString *>*)*)authenticateAsyncWithReadPermissions:(nullable NSArray PF_GENERIC(NSString *)*)readPermissions
53+
publishPermissions:(nullable NSArray PF_GENERIC(NSString *)*)publishPermissions
54+
fromViewComtroller:(UIViewController *)viewController {
55+
return [BFTask taskWithError:[NSError pffb_invalidFacebookSessionError]];
56+
}
57+
58+
///--------------------------------------
59+
#pragma mark - PFUserAuthenticationDelegate
60+
///--------------------------------------
61+
62+
- (BOOL)restoreAuthenticationWithAuthData:(nullable NSDictionary PF_GENERIC(NSString *, NSString *)*)authData {
63+
FBSDKAccessToken *token = [PFFacebookPrivateUtilities facebookAccessTokenFromUserAuthenticationData:authData];
64+
if (!token) {
65+
return !authData; // Only deauthenticate if authData was nil, otherwise - return failure (`NO`).
66+
}
67+
68+
FBSDKAccessToken *currentToken = [FBSDKAccessToken currentAccessToken];
69+
// Do not reset the current token if we have the same token already set.
70+
if (![currentToken.userID isEqualToString:token.userID] ||
71+
![currentToken.tokenString isEqualToString:token.tokenString]) {
72+
[FBSDKAccessToken setCurrentAccessToken:token];
73+
}
74+
75+
return YES;
76+
}
77+
78+
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright (c) 2015-present, Parse, LLC.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#import <Foundation/Foundation.h>
11+
12+
#import <FBSDKLoginKit/FBSDKLoginManager.h>
13+
14+
#import <Parse/PFConstants.h>
15+
#import <Parse/PFUser.h>
16+
17+
#import "PFFacebookAuthenticationProvider.h"
18+
19+
@class BFTask PF_GENERIC(__covariant BFGenericType);
20+
21+
NS_ASSUME_NONNULL_BEGIN
22+
23+
@interface PFFacebookMobileAuthenticationProvider : PFFacebookAuthenticationProvider
24+
25+
@property (nonatomic, strong, readonly) FBSDKLoginManager *loginManager;
26+
27+
@end
28+
29+
NS_ASSUME_NONNULL_END
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* Copyright (c) 2015-present, Parse, LLC.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#import "PFFacebookMobileAuthenticationProvider.h"
11+
#import "PFFacebookMobileAuthenticationProvider_Private.h"
12+
13+
#import <Bolts/BFTask.h>
14+
#import <Bolts/BFTaskCompletionSource.h>
15+
16+
#import <FBSDKCoreKit/FBSDKAccessToken.h>
17+
#import <FBSDKCoreKit/FBSDKSettings.h>
18+
19+
#import <FBSDKLoginKit/FBSDKLoginManagerLoginResult.h>
20+
21+
#import <Parse/PFConstants.h>
22+
23+
#import "PFFacebookPrivateUtilities.h"
24+
25+
@implementation PFFacebookMobileAuthenticationProvider
26+
27+
///--------------------------------------
28+
#pragma mark - Init
29+
///--------------------------------------
30+
31+
- (instancetype)initWithApplication:(UIApplication *)application
32+
launchOptions:(nullable NSDictionary *)launchOptions {
33+
self = [super initWithApplication:application launchOptions:launchOptions];
34+
if (!self) return nil;
35+
36+
_loginManager = [[FBSDKLoginManager alloc] init];
37+
38+
return self;
39+
}
40+
41+
///--------------------------------------
42+
#pragma mark - Authenticate
43+
///--------------------------------------
44+
45+
- (BFTask PF_GENERIC(NSDictionary<NSString *, NSString *>*)*)authenticateAsyncWithReadPermissions:(nullable NSArray PF_GENERIC(NSString *) *)readPermissions
46+
publishPermissions:(nullable NSArray PF_GENERIC(NSString *) *)publishPermissions
47+
fromViewComtroller:(UIViewController *)viewController {
48+
if (readPermissions && publishPermissions) {
49+
NSException *exception = [NSException exceptionWithName:NSInvalidArgumentException
50+
reason:@"Read permissions are not permitted to be requested with publish permissions."
51+
userInfo:nil];
52+
return [BFTask taskWithException:exception];
53+
}
54+
55+
BFTaskCompletionSource *taskCompletionSource = [BFTaskCompletionSource taskCompletionSource];
56+
FBSDKLoginManagerRequestTokenHandler resultHandler = ^(FBSDKLoginManagerLoginResult *result, NSError *error) {
57+
if (result.isCancelled) {
58+
[taskCompletionSource cancel];
59+
} else if (error) {
60+
taskCompletionSource.error = error;
61+
} else {
62+
taskCompletionSource.result = [PFFacebookPrivateUtilities userAuthenticationDataFromAccessToken:result.token];
63+
}
64+
};
65+
if (publishPermissions) {
66+
[self.loginManager logInWithPublishPermissions:publishPermissions
67+
fromViewController:viewController
68+
handler:resultHandler];
69+
} else {
70+
[self.loginManager logInWithReadPermissions:readPermissions
71+
fromViewController:viewController
72+
handler:resultHandler];
73+
}
74+
return taskCompletionSource.task;
75+
}
76+
77+
///--------------------------------------
78+
#pragma mark - PFUserAuthenticationDelegate
79+
///--------------------------------------
80+
81+
- (BOOL)restoreAuthenticationWithAuthData:(nullable NSDictionary PF_GENERIC(NSString *, NSString *) *)authData {
82+
if (!authData) {
83+
[self.loginManager logOut];
84+
}
85+
return [super restoreAuthenticationWithAuthData:authData];
86+
}
87+
88+
@end

ParseFacebookUtils/Internal/PFFacebookAuthenticationProvider_Private.h renamed to ParseFacebookUtils/Internal/AuthenticationProvider/iOS/PFFacebookMobileAuthenticationProvider_Private.h

+2-7
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,12 @@
99

1010
#import <FBSDKLoginKit/FBSDKLoginManager.h>
1111

12-
#import "PFFacebookAuthenticationProvider.h"
12+
#import "PFFacebookMobileAuthenticationProvider.h"
1313

1414
@class FBSDKAccessToken;
1515

16-
@interface PFFacebookAuthenticationProvider ()
16+
@interface PFFacebookMobileAuthenticationProvider ()
1717

1818
@property (nonatomic, strong, readwrite) FBSDKLoginManager *loginManager;
1919

20-
+ (NSDictionary *)_userAuthenticationDataWithFacebookUserId:(NSString *)userId
21-
accessToken:(NSString *)accessToken
22-
expirationDate:(NSDate *)expirationDate;
23-
+ (NSDictionary *)_userAuthenticationDataFromAccessToken:(FBSDKAccessToken *)token;
24-
2520
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Copyright (c) 2015-present, Parse, LLC.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#import <Foundation/Foundation.h>
11+
12+
#import "PFFacebookAuthenticationProvider.h"
13+
14+
@interface PFFacebookDeviceAuthenticationProvider : PFFacebookAuthenticationProvider
15+
16+
@end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* Copyright (c) 2015-present, Parse, LLC.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
#import "PFFacebookDeviceAuthenticationProvider.h"
11+
12+
#import <Bolts/BFExecutor.h>
13+
#import <Bolts/BFTaskCompletionSource.h>
14+
15+
#import <FBSDKCoreKit/FBSDKApplicationDelegate.h>
16+
#import <FBSDKCoreKit/FBSDKAccessToken.h>
17+
#import <FBSDKCoreKit/FBSDKConstants.h>
18+
#import <FBSDKTVOSKit/FBSDKDeviceLoginViewController.h>
19+
20+
#import "PFFacebookPrivateUtilities.h"
21+
22+
@interface PFFacebookDeviceAuthenticationProvider () <FBSDKDeviceLoginViewControllerDelegate> {
23+
BFTaskCompletionSource *_loginTaskCompletionSource;
24+
FBSDKDeviceLoginViewController *_loginViewController;
25+
}
26+
27+
@end
28+
29+
@implementation PFFacebookDeviceAuthenticationProvider
30+
31+
///--------------------------------------
32+
#pragma mark - PFFacebookAuthenticationProvider
33+
///--------------------------------------
34+
35+
- (BFTask PF_GENERIC(NSDictionary<NSString *, NSString *>*)*)authenticateAsyncWithReadPermissions:(nullable NSArray PF_GENERIC(NSString *)*)readPermissions
36+
publishPermissions:(nullable NSArray PF_GENERIC(NSString *)*)publishPermissions
37+
fromViewComtroller:(UIViewController *)viewController {
38+
return [BFTask taskFromExecutor:[BFExecutor mainThreadExecutor] withBlock:^id _Nonnull{
39+
if (_loginTaskCompletionSource) {
40+
return [NSError errorWithDomain:FBSDKErrorDomain
41+
code:FBSDKDialogUnavailableErrorCode
42+
userInfo:@{ NSLocalizedDescriptionKey : @"Another login attempt is already in progress." }];
43+
}
44+
_loginTaskCompletionSource = [BFTaskCompletionSource taskCompletionSource];
45+
_loginViewController = [[FBSDKDeviceLoginViewController alloc] init];
46+
_loginViewController.delegate = self;
47+
_loginViewController.readPermissions = readPermissions;
48+
_loginViewController.publishPermissions = publishPermissions;
49+
50+
[viewController presentViewController:_loginViewController animated:YES completion:nil];
51+
52+
return _loginTaskCompletionSource.task;
53+
}];
54+
}
55+
56+
///--------------------------------------
57+
#pragma mark - PFUserAuthenticationDelegate
58+
///--------------------------------------
59+
60+
- (BOOL)restoreAuthenticationWithAuthData:(nullable NSDictionary PF_GENERIC(NSString *, NSString *)*)authData {
61+
if (!authData) {
62+
[FBSDKAccessToken setCurrentAccessToken:nil];
63+
}
64+
return [super restoreAuthenticationWithAuthData:authData];
65+
}
66+
67+
///--------------------------------------
68+
#pragma mark - FBSDKDeviceLoginViewController
69+
///--------------------------------------
70+
71+
- (void)deviceLoginViewControllerDidCancel:(FBSDKDeviceLoginViewController *)viewController {
72+
[_loginTaskCompletionSource trySetCancelled];
73+
_loginViewController = nil;
74+
_loginTaskCompletionSource = nil;
75+
}
76+
77+
- (void)deviceLoginViewControllerDidFinish:(FBSDKDeviceLoginViewController *)viewController {
78+
FBSDKAccessToken *accessToken = [FBSDKAccessToken currentAccessToken];
79+
NSDictionary PF_GENERIC(NSString *,NSString*) *result = [PFFacebookPrivateUtilities userAuthenticationDataFromAccessToken:accessToken];
80+
[_loginTaskCompletionSource trySetResult:result];
81+
_loginViewController = nil;
82+
_loginTaskCompletionSource = nil;
83+
}
84+
85+
- (void)deviceLoginViewControllerDidFail:(FBSDKDeviceLoginViewController *)viewController error:(NSError *)error {
86+
[_loginTaskCompletionSource trySetError:error];
87+
_loginViewController = nil;
88+
_loginTaskCompletionSource = nil;
89+
}
90+
91+
@end

0 commit comments

Comments
 (0)