From 9f0b492c42b9a073dea0886895da92cb9a06ba5d Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Sun, 10 Mar 2019 18:53:11 +0000 Subject: [PATCH] Fix collection editing following an encryption password change. A lot of the code dealing with collections assumes that the editing is done by the owner (true assumption, for now), and therefore the encryption key is derived from the master key (not true anymore, as it could be a stored version of the old key). This commit removes this wrong assumption. --- .../ui/CreateCollectionFragment.kt | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/etesync/syncadapter/ui/CreateCollectionFragment.kt b/app/src/main/java/com/etesync/syncadapter/ui/CreateCollectionFragment.kt index 5bb33a53..4976927a 100644 --- a/app/src/main/java/com/etesync/syncadapter/ui/CreateCollectionFragment.kt +++ b/app/src/main/java/com/etesync/syncadapter/ui/CreateCollectionFragment.kt @@ -96,30 +96,36 @@ class CreateCollectionFragment : DialogFragment(), LoaderManager.LoaderCallbacks } val serviceEntity = JournalModel.Service.fetch(data, account.name, info.type) + info.serviceID = serviceEntity.id val settings = AccountSettings(context, account) val principal = HttpUrl.get(settings.uri!!) val journalManager = JournalManager(HttpClient.create(context, settings), principal!!) var uid = info.uid + if (uid == null) { uid = JournalManager.Journal.genUid() info.uid = uid val crypto = Crypto.CryptoManager(info.version, settings.password(), uid) val journal = JournalManager.Journal(crypto, info.toJson(), uid) journalManager.create(journal) + + val journalEntity = JournalEntity.fetchOrCreate(data, info) + data.upsert(journalEntity) } else { - val crypto = Crypto.CryptoManager(info.version, settings.password(), uid) + val crypto: Crypto.CryptoManager + val journalEntity = JournalEntity.fetch(data, serviceEntity, uid) + + if (journalEntity.encryptedKey != null) { + crypto = Crypto.CryptoManager(info.version, settings.keyPair!!, journalEntity.encryptedKey) + } else { + crypto = Crypto.CryptoManager(info.version, settings.password(), uid) + } val journal = JournalManager.Journal(crypto, info.toJson(), uid) journalManager.update(journal) } - // 2. add collection to service - info.serviceID = serviceEntity.id - val journalEntity = JournalEntity.fetchOrCreate(data, info) - data.upsert(journalEntity) - - requestSync(authority) } catch (e: IllegalStateException) { return e