Skip to content

Commit

Permalink
Fix smart store upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
bbirman committed Jul 19, 2020
1 parent 9a68a63 commit 1b87874
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions libs/SmartStore/SmartStore/Classes/SFSmartStoreUpgrade.m
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ + (void)updateEncryption
[SFSDKSmartStoreLogger i:[SFSmartStoreUpgrade class] format:@"Updating encryption method for all stores, where necessary."];
NSArray *allStoreNames = [[SFSmartStoreDatabaseManager sharedManager] allStoreNames];
[SFSDKSmartStoreLogger i:[SFSmartStoreUpgrade class] format:@"Number of stores to update: %d", [allStoreNames count]];
[SFSmartStoreUpgrade updateEncryptionUserKeys];

// Encryption updates will only apply to the current user. Multi-user comes concurrently with these encryption updates.
SFUserAccount *currentUser = [SFUserAccountManager sharedInstance].currentUser;
Expand All @@ -114,6 +115,44 @@ + (void)updateEncryption
}
}

+ (void)updateEncryptionUserKeys {
// The userID length changed in 8.2, update the user key used to track key store encryption
NSUserDefaults *userDefaults = [NSUserDefaults msdkUserDefaults];
NSMutableDictionary *keyStoreDict = [[userDefaults objectForKey:kKeyStoreEncryptedStoresKey] mutableCopy];
BOOL updateDictionary = NO;
for (NSString *key in [keyStoreDict allKeys]) {
NSString *legacyUserId = [SFSmartStoreUpgrade legacyUserIdFromKey:key];
if (legacyUserId){
// Create new key with 18 character ID
NSString *newUserId = [legacyUserId entityId18];
NSString *newKey = [key stringByReplacingOccurrencesOfString:legacyUserId withString:newUserId];

// Move value from old key to new key
NSDictionary *userKeyStoreDict = keyStoreDict[key];
keyStoreDict[newKey] = userKeyStoreDict;
[keyStoreDict removeObjectForKey:key];
updateDictionary = YES;
}
}

if (updateDictionary) {
[SFSDKSmartStoreLogger i:[SFSmartStoreUpgrade class] format:@"Updating encrypted key stores dictionary to use 18-character user ID"];
[userDefaults setObject:keyStoreDict forKey:kKeyStoreEncryptedStoresKey];
[userDefaults synchronize];
}
}

+ (NSString *)legacyUserIdFromKey:(NSString *)key {
NSString *pattern = @"00D[a-zA-Z0-9]{15}-(005[a-zA-Z0-9]{12})-"; // OrgId-UserId-
NSRegularExpression *userIdExpression = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
NSTextCheckingResult *match = [userIdExpression firstMatchInString:key options:0 range:NSMakeRange(0, key.length)];
if (match) {
NSRange matchRange = [match rangeAtIndex:1];
return [key substringWithRange:matchRange];
}
return nil;
}

+ (void)updateEncryptionSalt
{

Expand Down

0 comments on commit 1b87874

Please sign in to comment.