From a4a32045e8777d6ff45626297e18497e09f92e36 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Wed, 12 Apr 2017 17:51:08 +0100 Subject: [PATCH] Journal: get and persist owner and key. The server was changed so the owner of the journal, and the encrypted key (if a shared journal) would be exposed. This change fetches it, and saves it. --- .../java/com/etesync/syncadapter/App.java | 2 +- .../journalmanager/JournalManager.java | 6 ++++ .../syncadapter/model/JournalModel.java | 11 ++++++- .../syncadapter/SyncAdapterService.java | 33 ++++++++++--------- 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/etesync/syncadapter/App.java b/app/src/main/java/com/etesync/syncadapter/App.java index e567f09c..3b956d7b 100644 --- a/app/src/main/java/com/etesync/syncadapter/App.java +++ b/app/src/main/java/com/etesync/syncadapter/App.java @@ -227,7 +227,7 @@ public class App extends Application { public EntityDataStore getData() { if (dataStore == null) { // override onUpgrade to handle migrating to a new version - DatabaseSource source = new DatabaseSource(this, Models.DEFAULT, 1); + DatabaseSource source = new DatabaseSource(this, Models.DEFAULT, 2); Configuration configuration = source.getConfiguration(); dataStore = new EntityDataStore<>(configuration); } diff --git a/app/src/main/java/com/etesync/syncadapter/journalmanager/JournalManager.java b/app/src/main/java/com/etesync/syncadapter/journalmanager/JournalManager.java index de5b2095..f190b449 100644 --- a/app/src/main/java/com/etesync/syncadapter/journalmanager/JournalManager.java +++ b/app/src/main/java/com/etesync/syncadapter/journalmanager/JournalManager.java @@ -130,6 +130,12 @@ public class JournalManager extends BaseManager { } public static class Journal extends Base { + @Getter + private String owner; + + @Getter + private byte[] key; + @Getter private int version = -1; diff --git a/app/src/main/java/com/etesync/syncadapter/model/JournalModel.java b/app/src/main/java/com/etesync/syncadapter/model/JournalModel.java index f4320bb1..96f8b415 100644 --- a/app/src/main/java/com/etesync/syncadapter/model/JournalModel.java +++ b/app/src/main/java/com/etesync/syncadapter/model/JournalModel.java @@ -30,6 +30,10 @@ public class JournalModel { @Convert(CollectionInfoConverter.class) CollectionInfo info; + String owner; + + byte[] encryptedKey; + long service; boolean deleted; @@ -51,10 +55,15 @@ public class JournalModel { this.service = info.serviceID; } + public static List getJournals(EntityDataStore data, long service) { + return data.select(JournalEntity.class).where(JournalEntity.SERVICE.eq(service).and(JournalEntity.DELETED.eq(false))).get().toList(); + } + public static List getCollections(EntityDataStore data, long service) { List ret = new LinkedList<>(); - for (JournalEntity journal : data.select(JournalEntity.class).where(JournalEntity.SERVICE.eq(service).and(JournalEntity.DELETED.eq(false))).get()) { + List journals = getJournals(data, service); + for (JournalEntity journal : journals) { // FIXME: For some reason this isn't always being called, manually do it here. journal.afterLoad(); ret.add(journal.getInfo()); diff --git a/app/src/main/java/com/etesync/syncadapter/syncadapter/SyncAdapterService.java b/app/src/main/java/com/etesync/syncadapter/syncadapter/SyncAdapterService.java index 85269e78..f639615e 100644 --- a/app/src/main/java/com/etesync/syncadapter/syncadapter/SyncAdapterService.java +++ b/app/src/main/java/com/etesync/syncadapter/syncadapter/SyncAdapterService.java @@ -28,6 +28,7 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationManagerCompat; +import android.support.v4.util.Pair; import java.util.HashMap; import java.util.LinkedList; @@ -153,7 +154,7 @@ public abstract class SyncAdapterService extends Service { AccountSettings settings = new AccountSettings(context, account); JournalManager journalsManager = new JournalManager(httpClient, HttpUrl.get(settings.getUri())); - List collections = new LinkedList<>(); + List> journals = new LinkedList<>(); for (JournalManager.Journal journal : journalsManager.getJournals(settings.password())) { Crypto.CryptoManager crypto = new Crypto.CryptoManager(journal.getVersion(), settings.password(), journal.getUid()); @@ -161,22 +162,22 @@ public abstract class SyncAdapterService extends Service { info.updateFromJournal(journal); if (info.type.equals(serviceType)) { - collections.add(info); + journals.add(new Pair<>(journal, info)); } } - if (collections.isEmpty()) { + if (journals.isEmpty()) { CollectionInfo info = CollectionInfo.defaultForServiceType(serviceType); info.uid = JournalManager.Journal.genUid(); Crypto.CryptoManager crypto = new Crypto.CryptoManager(info.version, settings.password(), info.uid); JournalManager.Journal journal = new JournalManager.Journal(crypto, info.toJson(), info.uid); journalsManager.putJournal(journal); - collections.add(info); + journals.add(new Pair<>(journal, info)); } db.beginTransactionNonExclusive(); try { - saveCollections(db, collections); + saveCollections(db, journals); db.setTransactionSuccessful(); } finally { db.endTransaction(); @@ -186,30 +187,32 @@ public abstract class SyncAdapterService extends Service { } } - private void saveCollections(SQLiteDatabase db, Iterable collections) { + private void saveCollections(SQLiteDatabase db, Iterable> journals) { Long service = dbHelper.getService(db, account, serviceType.toString()); EntityDataStore data = ((App) context.getApplicationContext()).getData(); - Map existing = new HashMap<>(); - List existingList = JournalEntity.getCollections(data, service); - for (CollectionInfo info : existingList) { - existing.put(info.uid, info); + Map existing = new HashMap<>(); + for (JournalEntity journalEntity : JournalEntity.getJournals(data, service)) { + existing.put(journalEntity.getUid(), journalEntity); } - for (CollectionInfo collection : collections) { - App.log.log(Level.FINE, "Saving collection", collection.uid); + for (Pair pair : journals) { + JournalManager.Journal journal = pair.first; + CollectionInfo collection = pair.second; + App.log.log(Level.FINE, "Saving collection", journal.getUid()); collection.serviceID = service; JournalEntity journalEntity = JournalEntity.fetchOrCreate(data, collection); + journalEntity.setOwner(journal.getOwner()); + journalEntity.setEncryptedKey(journal.getKey()); data.upsert(journalEntity); existing.remove(collection.uid); } - for (CollectionInfo collection : existing.values()) { - App.log.log(Level.FINE, "Deleting collection", collection.uid); + for (JournalEntity journalEntity : existing.values()) { + App.log.log(Level.FINE, "Deleting collection", journalEntity.getUid()); - JournalEntity journalEntity = data.select(JournalEntity.class).where(JournalEntity.UID.eq(collection.uid)).limit(1).get().first(); journalEntity.setDeleted(true); data.update(journalEntity); }