From ca862096b4a9c36ab95e197060a6cfe9bf8a7a99 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Thu, 22 Jul 2021 14:52:10 +1000 Subject: [PATCH] =?UTF-8?q?Rename=20profilePictureEncryptionKey=20?= =?UTF-8?q?=E2=86=92=20profileEncryptionKey?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SessionMessagingKit/Contacts/Contact.swift | 12 +++---- .../MessageReceiver+Handling.swift | 6 ++-- .../Sending & Receiving/MessageSender.swift | 4 +-- SessionMessagingKit/Utilities/ProtoUtils.m | 2 +- .../ConfigurationMessage+Convenience.swift | 4 +-- SignalUtilitiesKit/To Do/OWSProfileManager.m | 36 ++++++++++--------- 6 files changed, 34 insertions(+), 30 deletions(-) diff --git a/SessionMessagingKit/Contacts/Contact.swift b/SessionMessagingKit/Contacts/Contact.swift index f909a6888..ada7fc479 100644 --- a/SessionMessagingKit/Contacts/Contact.swift +++ b/SessionMessagingKit/Contacts/Contact.swift @@ -6,8 +6,8 @@ public class Contact : NSObject, NSCoding { // NSObject/NSCoding conformance is @objc public var profilePictureURL: String? /// The file name of the contact's profile picture on local storage. @objc public var profilePictureFileName: String? - /// The key with which the profile picture is encrypted. - @objc public var profilePictureEncryptionKey: OWSAES256Key? + /// The key with which the profile is encrypted. + @objc public var profileEncryptionKey: OWSAES256Key? /// The ID of the thread associated with this contact. @objc public var threadID: String? /// This flag is used to determine whether we should auto-download files sent by this contact. @@ -49,8 +49,8 @@ public class Contact : NSObject, NSCoding { // NSObject/NSCoding conformance is // MARK: Validation public var isValid: Bool { - if profilePictureURL != nil { return (profilePictureEncryptionKey != nil) } - if profilePictureEncryptionKey != nil { return (profilePictureURL != nil) } + if profilePictureURL != nil { return (profileEncryptionKey != nil) } + if profileEncryptionKey != nil { return (profilePictureURL != nil) } return true } @@ -63,7 +63,7 @@ public class Contact : NSObject, NSCoding { // NSObject/NSCoding conformance is if let nickname = coder.decodeObject(forKey: "nickname") as! String? { self.nickname = nickname } if let profilePictureURL = coder.decodeObject(forKey: "profilePictureURL") as! String? { self.profilePictureURL = profilePictureURL } if let profilePictureFileName = coder.decodeObject(forKey: "profilePictureFileName") as! String? { self.profilePictureFileName = profilePictureFileName } - if let profilePictureEncryptionKey = coder.decodeObject(forKey: "profilePictureEncryptionKey") as! OWSAES256Key? { self.profilePictureEncryptionKey = profilePictureEncryptionKey } + if let profileEncryptionKey = coder.decodeObject(forKey: "profilePictureEncryptionKey") as! OWSAES256Key? { self.profileEncryptionKey = profileEncryptionKey } if let threadID = coder.decodeObject(forKey: "threadID") as! String? { self.threadID = threadID } } @@ -73,7 +73,7 @@ public class Contact : NSObject, NSCoding { // NSObject/NSCoding conformance is coder.encode(nickname, forKey: "nickname") coder.encode(profilePictureURL, forKey: "profilePictureURL") coder.encode(profilePictureFileName, forKey: "profilePictureFileName") - coder.encode(profilePictureEncryptionKey, forKey: "profilePictureEncryptionKey") + coder.encode(profileEncryptionKey, forKey: "profilePictureEncryptionKey") coder.encode(threadID, forKey: "threadID") coder.encode(isTrusted, forKey: "isTrusted") } diff --git a/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift b/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift index 5fc0a968b..b11b8a3ad 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift @@ -195,7 +195,7 @@ extension MessageReceiver { for contactInfo in message.contacts { let sessionID = contactInfo.publicKey! let contact = Contact(sessionID: sessionID) - contact.profilePictureEncryptionKey = given(contactInfo.profileKey) { OWSAES256Key(data: $0)! } + contact.profileEncryptionKey = given(contactInfo.profileKey) { OWSAES256Key(data: $0)! } contact.profilePictureURL = contactInfo.profilePictureURL contact.name = contactInfo.displayName Storage.shared.setContact(contact, using: transaction) @@ -321,7 +321,7 @@ extension MessageReceiver { } // Profile picture & profile key if let profileKey = profileKey, let profilePictureURL = profilePictureURL, profileKey.keyData.count == kAES256_KeyByteLength, - profileKey != contact.profilePictureEncryptionKey { + profileKey != contact.profileEncryptionKey { let shouldUpdate: Bool if isCurrentUser { shouldUpdate = given(userDefaults[.lastProfilePictureUpdate]) { sentTimestamp > UInt64($0.timeIntervalSince1970 * 1000) } ?? true @@ -333,7 +333,7 @@ extension MessageReceiver { userDefaults[.lastProfilePictureUpdate] = Date(timeIntervalSince1970: TimeInterval(sentTimestamp / 1000)) } contact.profilePictureURL = profilePictureURL - contact.profilePictureEncryptionKey = profileKey + contact.profileEncryptionKey = profileKey } } // Persist changes diff --git a/SessionMessagingKit/Sending & Receiving/MessageSender.swift b/SessionMessagingKit/Sending & Receiving/MessageSender.swift index 089ea6a34..587587108 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageSender.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageSender.swift @@ -145,7 +145,7 @@ public final class MessageSender : NSObject { // Attach the user's profile if needed if let message = message as? VisibleMessage { guard let name = storage.getUser()?.name else { handleFailure(with: Error.noUsername, using: transaction); return promise } - if let profileKey = storage.getUser()?.profilePictureEncryptionKey?.keyData, let profilePictureURL = storage.getUser()?.profilePictureURL { + if let profileKey = storage.getUser()?.profileEncryptionKey?.keyData, let profilePictureURL = storage.getUser()?.profilePictureURL { message.profile = VisibleMessage.Profile(displayName: name, profileKey: profileKey, profilePictureURL: profilePictureURL) } else { message.profile = VisibleMessage.Profile(displayName: name) @@ -285,7 +285,7 @@ public final class MessageSender : NSObject { guard message.isValid else { handleFailure(with: Error.invalidMessage, using: transaction); return promise } // Attach the user's profile guard let name = storage.getUser()?.name else { handleFailure(with: Error.noUsername, using: transaction); return promise } - if let profileKey = storage.getUser()?.profilePictureEncryptionKey?.keyData, let profilePictureURL = storage.getUser()?.profilePictureURL { + if let profileKey = storage.getUser()?.profileEncryptionKey?.keyData, let profilePictureURL = storage.getUser()?.profilePictureURL { message.profile = VisibleMessage.Profile(displayName: name, profileKey: profileKey, profilePictureURL: profilePictureURL) } else { message.profile = VisibleMessage.Profile(displayName: name) diff --git a/SessionMessagingKit/Utilities/ProtoUtils.m b/SessionMessagingKit/Utilities/ProtoUtils.m index e1a4d4bcf..0a1ac6874 100644 --- a/SessionMessagingKit/Utilities/ProtoUtils.m +++ b/SessionMessagingKit/Utilities/ProtoUtils.m @@ -21,7 +21,7 @@ NS_ASSUME_NONNULL_BEGIN + (OWSAES256Key *)localProfileKey { - return [[LKStorage.shared getUser] profilePictureEncryptionKey]; + return [[LKStorage.shared getUser] profileEncryptionKey]; } #pragma mark - diff --git a/SignalUtilitiesKit/Messaging/ConfigurationMessage+Convenience.swift b/SignalUtilitiesKit/Messaging/ConfigurationMessage+Convenience.swift index 3e20fd601..ac4d6da5d 100644 --- a/SignalUtilitiesKit/Messaging/ConfigurationMessage+Convenience.swift +++ b/SignalUtilitiesKit/Messaging/ConfigurationMessage+Convenience.swift @@ -6,7 +6,7 @@ extension ConfigurationMessage { guard let user = storage.getUser() else { return nil } let displayName = user.name let profilePictureURL = user.profilePictureURL - let profileKey = user.profilePictureEncryptionKey?.keyData + let profileKey = user.profileEncryptionKey?.keyData var closedGroups: Set = [] var openGroups: Set = [] var contacts: Set = [] @@ -39,7 +39,7 @@ extension ConfigurationMessage { guard let thread = TSContactThread.fetch(uniqueId: threadID, transaction: transaction), thread.shouldBeVisible && !SSKEnvironment.shared.blockingManager.isRecipientIdBlocked(publicKey) else { return } let profilePictureURL = contact.profilePictureURL - let profileKey = contact.profilePictureEncryptionKey?.keyData + let profileKey = contact.profileEncryptionKey?.keyData let contact = ConfigurationMessage.Contact(publicKey: publicKey, displayName: contact.name ?? publicKey, profilePictureURL: profilePictureURL, profileKey: profileKey) contacts.insert(contact) diff --git a/SignalUtilitiesKit/To Do/OWSProfileManager.m b/SignalUtilitiesKit/To Do/OWSProfileManager.m index 5395455dd..729049c36 100644 --- a/SignalUtilitiesKit/To Do/OWSProfileManager.m +++ b/SignalUtilitiesKit/To Do/OWSProfileManager.m @@ -275,7 +275,7 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error); [NSUserDefaults.standardUserDefaults setObject:[NSDate new] forKey:@"lastProfilePictureUpload"]; SNContact *user = [LKStorage.shared getUser]; - user.profilePictureEncryptionKey = newProfileKey; + user.profileEncryptionKey = newProfileKey; [LKStorage writeWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [LKStorage.shared setContact:user usingTransaction:transaction]; } completion:^{ @@ -288,7 +288,7 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error); // is a quick and dirty workaround. if ([result isKindOfClass:NSString.class]) { SNContact *user = [LKStorage.shared getUser]; - user.profilePictureEncryptionKey = newProfileKey; + user.profileEncryptionKey = newProfileKey; [LKStorage writeWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [LKStorage.shared setContact:user usingTransaction:transaction]; } completion:^{ @@ -301,7 +301,7 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error); } else { // Update our profile key and set the url to nil if avatar data is nil SNContact *user = [LKStorage.shared getUser]; - user.profilePictureEncryptionKey = newProfileKey; + user.profileEncryptionKey = newProfileKey; user.profilePictureURL = nil; user.profilePictureFileName = nil; [LKStorage writeWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { @@ -363,7 +363,7 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error); { NSString *userPublicKey = [SNGeneralUtilities getUserPublicKey]; SNContact *contact = [LKStorage.shared getContactWithSessionID:userPublicKey]; - contact.profilePictureEncryptionKey = [OWSAES256Key generateRandomKey]; + contact.profileEncryptionKey = [OWSAES256Key generateRandomKey]; contact.profilePictureURL = nil; contact.profilePictureFileName = nil; [LKStorage writeWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { @@ -387,12 +387,12 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error); SNContact *contact = [LKStorage.shared getContactWithSessionID:recipientId]; OWSAssertDebug(contact); - if (contact.profilePictureEncryptionKey != nil && [contact.profilePictureEncryptionKey.keyData isEqual:profileKey.keyData]) { + if (contact.profileEncryptionKey != nil && [contact.profileEncryptionKey.keyData isEqual:profileKey.keyData]) { // Ignore redundant update. return; } - contact.profilePictureEncryptionKey = profileKey; + contact.profileEncryptionKey = profileKey; contact.profilePictureURL = nil; contact.profilePictureFileName = nil; @@ -426,7 +426,7 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error); SNContact *contact = [LKStorage.shared getContactWithSessionID:recipientId]; OWSAssertDebug(contact); - return contact.profilePictureEncryptionKey; + return contact.profileEncryptionKey; } - (nullable UIImage *)profileAvatarForRecipientId:(NSString *)recipientId @@ -478,12 +478,12 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error); } NSString *_Nullable avatarUrlPathAtStart = contact.profilePictureURL; - BOOL hasProfileEncryptionKey = (contact.profilePictureEncryptionKey != nil && contact.profilePictureEncryptionKey.keyData.length > 0); + BOOL hasProfileEncryptionKey = (contact.profileEncryptionKey != nil && contact.profileEncryptionKey.keyData.length > 0); if (!hasProfileEncryptionKey || !hasProfilePictureURL) { return; } - OWSAES256Key *profileKeyAtStart = contact.profilePictureEncryptionKey; + OWSAES256Key *profileKeyAtStart = contact.profileEncryptionKey; NSString *fileName = [self generateAvatarFilename]; NSString *filePath = [OWSUserProfile profileAvatarFilepathWithFilename:fileName]; @@ -522,9 +522,9 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error); SNContact *latestContact = [LKStorage.shared getContactWithSessionID:contact.sessionID]; - BOOL hasProfileEncryptionKey = (latestContact.profilePictureEncryptionKey != nil - && latestContact.profilePictureEncryptionKey.keyData.length > 0); - if (!hasProfileEncryptionKey || ![latestContact.profilePictureEncryptionKey isEqual:contact.profilePictureEncryptionKey]) { + BOOL hasProfileEncryptionKey = (latestContact.profileEncryptionKey != nil + && latestContact.profileEncryptionKey.keyData.length > 0); + if (!hasProfileEncryptionKey || ![latestContact.profileEncryptionKey isEqual:contact.profileEncryptionKey]) { OWSLogWarn(@"Ignoring avatar download for obsolete user profile."); } else if (![avatarUrlPathAtStart isEqualToString:latestContact.profilePictureURL]) { OWSLogInfo(@"avatar url has changed during download"); @@ -563,13 +563,17 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error); dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ SNContact *contact = [LKStorage.shared getContactWithSessionID:recipientId]; - if (!contact.profilePictureEncryptionKey) { return; } + if (!contact.profileEncryptionKey) { return; } NSString *_Nullable profileName = - [self decryptProfileNameData:profileNameEncrypted profileKey:contact.profilePictureEncryptionKey]; + [self decryptProfileNameData:profileNameEncrypted profileKey:contact.profileEncryptionKey]; contact.name = profileName; contact.profilePictureURL = avatarUrlPath; + + [LKStorage writeWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + [LKStorage.shared setContact:contact usingTransaction:transaction]; + }]; // Whenever we change avatarUrlPath, OWSUserProfile clears avatarFileName. // So if avatarUrlPath is set and avatarFileName is not set, we should to @@ -659,7 +663,7 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error); - (nullable NSData *)encryptProfileData:(nullable NSData *)data { - OWSAES256Key *localProfileKey = [LKStorage.shared getUser].profilePictureEncryptionKey; + OWSAES256Key *localProfileKey = [LKStorage.shared getUser].profileEncryptionKey; return [self encryptProfileData:data profileKey:localProfileKey]; } @@ -688,7 +692,7 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error); [paddedNameData increaseLengthBy:paddingByteCount]; OWSAssertDebug(paddedNameData.length == kOWSProfileManager_NameDataLength); - OWSAES256Key *localProfileKey = [LKStorage.shared getUser].profilePictureEncryptionKey; + OWSAES256Key *localProfileKey = [LKStorage.shared getUser].profileEncryptionKey; return [self encryptProfileData:[paddedNameData copy] profileKey:localProfileKey]; }