Skip to content

Commit 13fabd2

Browse files
Brian Clouserfacebook-github-bot
authored andcommitted
FBSDK token fixes
Summary: Adding optional optimizations (default is YES) when fetching access token and authentication token. Reviewed By: joesus Differential Revision: D27856662 fbshipit-source-id: dce247bb338a245cb3283fbd8c58e1cb2e3c3345
1 parent 8138f8c commit 13fabd2

8 files changed

Lines changed: 74 additions & 13 deletions

File tree

FBSDKCoreKit/FBSDKCoreKit/FBSDKApplicationDelegate.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ - (void)configureDependencies
486486
requestProvider:graphRequestProvider
487487
connectionProvider:connectionProvider
488488
store:store];
489-
FBSDKTokenCache *tokenCache = [FBSDKTokenCache new];
489+
FBSDKTokenCache *tokenCache = [[FBSDKTokenCache alloc] initWithSettings:FBSDKSettings.sharedSettings];
490490
[FBSDKAccessToken setTokenCache:tokenCache];
491491
[FBSDKAccessToken setConnectionFactory:connectionProvider];
492492
[FBSDKAuthenticationToken setTokenCache:tokenCache];

FBSDKCoreKit/FBSDKCoreKit/FBSDKSettings.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ - (void)SETTER:(TYPE *)value { \
8484
static NSString *const FBSDKSettingsInstallTimestamp = @"com.facebook.sdk:FBSDKSettingsInstallTimestamp";
8585
static NSString *const FBSDKSettingsSetAdvertiserTrackingEnabledTimestamp = @"com.facebook.sdk:FBSDKSettingsSetAdvertiserTrackingEnabledTimestamp";
8686
static NSString *const FBSDKSettingsUseCachedValuesForExpensiveMetadata = @"com.facebook.sdk:FBSDKSettingsUseCachedValuesForExpensiveMetadata";
87+
static NSString *const FBSDKSettingsUseTokenOptimizations = @"com.facebook.sdk.FBSDKSettingsUseTokenOptimizations";
8788
static BOOL g_disableErrorRecovery;
8889
static NSString *g_userAgentSuffix;
8990
static NSString *g_defaultGraphAPIVersion;
@@ -410,6 +411,20 @@ + (void)setShouldUseCachedValuesForExpensiveMetadata:(BOOL)shouldUseCachedValues
410411
[self.store setObject:@(shouldUseCachedValuesForExpensiveMetadata) forKey:FBSDKSettingsUseCachedValuesForExpensiveMetadata];
411412
}
412413

414+
- (BOOL)shouldUseTokenOptimizations
415+
{
416+
NSNumber *storedValue = [self.store objectForKey:FBSDKSettingsUseTokenOptimizations];
417+
if (storedValue == nil) {
418+
return YES;
419+
}
420+
return storedValue.boolValue;
421+
}
422+
423+
- (void)setShouldUseTokenOptimizations:(BOOL)shouldUseTokenOptimizations
424+
{
425+
[self.store setObject:@(shouldUseTokenOptimizations) forKey:FBSDKSettingsUseTokenOptimizations];
426+
}
427+
413428
+ (NSSet<FBSDKLoggingBehavior> *)loggingBehaviors
414429
{
415430
if (!g_loggingBehaviors) {

FBSDKCoreKit/FBSDKCoreKit/Internal/FBSDKSettings+Internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
@property (class, nullable, nonatomic, copy) NSString *userAgentSuffix;
4343

4444
@property (class, nonnull, readonly) FBSDKSettings *sharedSettings;
45+
@property (nonatomic) BOOL shoudUseTokenOptimizations;
4546

4647
+ (void)configureWithStore:(nonnull id<FBSDKDataPersisting>)store
4748
appEventsConfigurationProvider:(nonnull Class<FBSDKAppEventsConfigurationProviding>)provider

FBSDKCoreKit/FBSDKCoreKit/Internal/FBSDKSettingsProtocol.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,6 @@ NS_SWIFT_NAME(SettingsProtocol)
3737
@property (nonatomic, readonly, nullable) NSDate* installTimestamp;
3838
@property (nonatomic, readonly, nullable) NSDate* advertiserTrackingEnabledTimestamp;
3939
@property (nonatomic, readonly) BOOL shouldLimitEventAndDataUsage;
40+
@property (nonatomic) BOOL shouldUseTokenOptimizations;
4041

4142
@end

FBSDKCoreKit/FBSDKCoreKit/Internal/TokenCaching/FBSDKTokenCache.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@
2626

2727
#import "FBSDKTokenCaching.h"
2828

29+
@protocol FBSDKSettings;
30+
2931
NS_SWIFT_NAME(TokenCache)
3032
@interface FBSDKTokenCache : NSObject<FBSDKTokenCaching>
3133

34+
- (instancetype)initWithSettings:(id<FBSDKSettings>)settings;
35+
3236
@end

FBSDKCoreKit/FBSDKCoreKit/Internal/TokenCaching/FBSDKTokenCache.m

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#import "FBSDKAuthenticationToken+Internal.h"
2222
#import "FBSDKDynamicFrameworkLoader.h"
2323
#import "FBSDKKeychainStore.h"
24+
#import "FBSDKSettingsProtocol.h"
2425
#import "FBSDKUnarchiverProvider.h"
2526

2627
static NSString *const kFBSDKAccessTokenUserDefaultsKey = @"com.facebook.sdk.v4.FBSDKAccessTokenInformationKey";
@@ -35,13 +36,15 @@
3536
@implementation FBSDKTokenCache
3637
{
3738
FBSDKKeychainStore *_keychainStore;
39+
id<FBSDKSettings> _settings;
3840
}
3941

40-
- (instancetype)init
42+
- (instancetype)initWithSettings:(id<FBSDKSettings>)settings
4143
{
4244
if ((self = [super init])) {
4345
NSString *keyChainServiceIdentifier = [NSString stringWithFormat:@"com.facebook.sdk.tokencache.%@", [NSBundle mainBundle].bundleIdentifier];
4446
_keychainStore = [[FBSDKKeychainStore alloc] initWithService:keyChainServiceIdentifier accessGroup:nil];
47+
_settings = settings;
4548
}
4649
return self;
4750
}
@@ -55,18 +58,20 @@ - (FBSDKAccessToken *)accessToken
5558
NSString *uuid = [defaults objectForKey:kFBSDKAccessTokenUserDefaultsKey];
5659
NSDictionary<NSString *, id> *dict = [_keychainStore dictionaryForKey:kFBSDKAccessTokenKeychainKey];
5760

58-
if (!uuid && !dict) {
59-
return nil;
60-
}
61+
if (_settings.shouldUseTokenOptimizations) {
62+
if (!uuid && !dict) {
63+
return nil;
64+
}
6165

62-
if (!uuid) {
63-
[self clearAccessTokenCache];
64-
return nil;
65-
}
66+
if (!uuid) {
67+
[self clearAccessTokenCache];
68+
return nil;
69+
}
6670

67-
if (!dict) {
68-
[defaults setObject:nil forKey:kFBSDKAccessTokenUserDefaultsKey];
69-
return nil;
71+
if (!dict) {
72+
[defaults setObject:nil forKey:kFBSDKAccessTokenUserDefaultsKey];
73+
return nil;
74+
}
7075
}
7176

7277
if ([dict[kFBSDKTokenUUIDKey] isKindOfClass:[NSString class]]) {
@@ -120,8 +125,24 @@ - (FBSDKAuthenticationToken *)authenticationToken
120125
{
121126
NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;
122127
NSString *uuid = [defaults objectForKey:kFBSDKAuthenticationTokenUserDefaultsKey];
123-
124128
NSDictionary<NSString *, id> *dict = [_keychainStore dictionaryForKey:kFBSDKAuthenticationTokenKeychainKey];
129+
130+
if (_settings.shouldUseTokenOptimizations) {
131+
if (!uuid && !dict) {
132+
return nil;
133+
}
134+
135+
if (!uuid) {
136+
[self clearAuthenticationTokenCache];
137+
return nil;
138+
}
139+
140+
if (!dict) {
141+
[defaults setObject:nil forKey:kFBSDKAuthenticationTokenKeychainKey];
142+
return nil;
143+
}
144+
}
145+
125146
if ([dict[kFBSDKTokenUUIDKey] isKindOfClass:[NSString class]]) {
126147
// there is a bug while running on simulator that the uuid stored in dict can be NSData,
127148
// do a type check to make sure it is NSString

FBSDKCoreKit/FBSDKCoreKitTests/Internal/FBSDKSettingsTests.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#import "FBSDKCoreKitTests-Swift.h"
2525
#import "FBSDKSettings.h"
2626
#import "FBSDKSettings+Internal.h"
27+
#import "FBSDKSettingsProtocol.h"
2728
#import "FBSDKTestCase.h"
2829
#import "NSUserDefaults+FBSDKDataPersisting.h"
2930
#import "UserDefaultsSpy.h"
@@ -1309,6 +1310,21 @@ - (void)testSetUseCachedValuesForExpensiveMetadata
13091310
);
13101311
}
13111312

1313+
- (void)testSetUseTokenOptimizations
1314+
{
1315+
FBSDKSettings.sharedSettings.shouldUseTokenOptimizations = NO;
1316+
1317+
XCTAssertEqualObjects(
1318+
userDefaultsSpy.capturedValues[@"com.facebook.sdk.FBSDKSettingsUseTokenOptimizations"],
1319+
@NO,
1320+
"Should store whether or not to use token optimizations"
1321+
);
1322+
XCTAssertFalse(
1323+
FBSDKSettings.sharedSettings.shouldUseTokenOptimizations,
1324+
"Should use token optimizations"
1325+
);
1326+
}
1327+
13121328
- (void)testSetLimitEventAndDataUsageWithEmptyCache
13131329
{
13141330
FBSDKSettings.limitEventAndDataUsage = YES;

FBSDKCoreKit/FBSDKCoreKitTests/Internal/Helpers/TestSettings.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
@objcMembers
2020
class TestSettings: NSObject, SettingsProtocol, SettingsLogging {
21+
22+
2123
static var appID: String?
2224
static var clientToken: String?
2325
static var userAgentSuffix: String?
@@ -38,6 +40,7 @@ class TestSettings: NSObject, SettingsProtocol, SettingsLogging {
3840
var stubbedIsSetATETimeExceedsInstallTime = false
3941
var stubbedIsSKAdNetworkReportEnabled = false
4042
var stubbedLimitEventAndDataUsage = false
43+
var shouldUseTokenOptimizations = true
4144

4245
var isDataProcessingRestricted: Bool {
4346
return stubbedIsDataProcessingRestricted

0 commit comments

Comments
 (0)