1
0
mirror of https://github.com/etesync/android synced 2025-04-29 13:39:11 +00:00

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.
This commit is contained in:
Tom Hacohen 2017-04-12 17:51:08 +01:00
parent efe832ddb4
commit a4a32045e8
4 changed files with 35 additions and 17 deletions

View File

@ -227,7 +227,7 @@ public class App extends Application {
public EntityDataStore<Persistable> getData() { public EntityDataStore<Persistable> getData() {
if (dataStore == null) { if (dataStore == null) {
// override onUpgrade to handle migrating to a new version // 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(); Configuration configuration = source.getConfiguration();
dataStore = new EntityDataStore<>(configuration); dataStore = new EntityDataStore<>(configuration);
} }

View File

@ -130,6 +130,12 @@ public class JournalManager extends BaseManager {
} }
public static class Journal extends Base { public static class Journal extends Base {
@Getter
private String owner;
@Getter
private byte[] key;
@Getter @Getter
private int version = -1; private int version = -1;

View File

@ -30,6 +30,10 @@ public class JournalModel {
@Convert(CollectionInfoConverter.class) @Convert(CollectionInfoConverter.class)
CollectionInfo info; CollectionInfo info;
String owner;
byte[] encryptedKey;
long service; long service;
boolean deleted; boolean deleted;
@ -51,10 +55,15 @@ public class JournalModel {
this.service = info.serviceID; this.service = info.serviceID;
} }
public static List<JournalEntity> getJournals(EntityDataStore<Persistable> data, long service) {
return data.select(JournalEntity.class).where(JournalEntity.SERVICE.eq(service).and(JournalEntity.DELETED.eq(false))).get().toList();
}
public static List<CollectionInfo> getCollections(EntityDataStore<Persistable> data, long service) { public static List<CollectionInfo> getCollections(EntityDataStore<Persistable> data, long service) {
List<CollectionInfo> ret = new LinkedList<>(); List<CollectionInfo> ret = new LinkedList<>();
for (JournalEntity journal : data.select(JournalEntity.class).where(JournalEntity.SERVICE.eq(service).and(JournalEntity.DELETED.eq(false))).get()) { List<JournalEntity> journals = getJournals(data, service);
for (JournalEntity journal : journals) {
// FIXME: For some reason this isn't always being called, manually do it here. // FIXME: For some reason this isn't always being called, manually do it here.
journal.afterLoad(); journal.afterLoad();
ret.add(journal.getInfo()); ret.add(journal.getInfo());

View File

@ -28,6 +28,7 @@ import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat; import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.util.Pair;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
@ -153,7 +154,7 @@ public abstract class SyncAdapterService extends Service {
AccountSettings settings = new AccountSettings(context, account); AccountSettings settings = new AccountSettings(context, account);
JournalManager journalsManager = new JournalManager(httpClient, HttpUrl.get(settings.getUri())); JournalManager journalsManager = new JournalManager(httpClient, HttpUrl.get(settings.getUri()));
List<CollectionInfo> collections = new LinkedList<>(); List<Pair<JournalManager.Journal, CollectionInfo>> journals = new LinkedList<>();
for (JournalManager.Journal journal : journalsManager.getJournals(settings.password())) { for (JournalManager.Journal journal : journalsManager.getJournals(settings.password())) {
Crypto.CryptoManager crypto = new Crypto.CryptoManager(journal.getVersion(), settings.password(), journal.getUid()); 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); info.updateFromJournal(journal);
if (info.type.equals(serviceType)) { 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); CollectionInfo info = CollectionInfo.defaultForServiceType(serviceType);
info.uid = JournalManager.Journal.genUid(); info.uid = JournalManager.Journal.genUid();
Crypto.CryptoManager crypto = new Crypto.CryptoManager(info.version, settings.password(), info.uid); Crypto.CryptoManager crypto = new Crypto.CryptoManager(info.version, settings.password(), info.uid);
JournalManager.Journal journal = new JournalManager.Journal(crypto, info.toJson(), info.uid); JournalManager.Journal journal = new JournalManager.Journal(crypto, info.toJson(), info.uid);
journalsManager.putJournal(journal); journalsManager.putJournal(journal);
collections.add(info); journals.add(new Pair<>(journal, info));
} }
db.beginTransactionNonExclusive(); db.beginTransactionNonExclusive();
try { try {
saveCollections(db, collections); saveCollections(db, journals);
db.setTransactionSuccessful(); db.setTransactionSuccessful();
} finally { } finally {
db.endTransaction(); db.endTransaction();
@ -186,30 +187,32 @@ public abstract class SyncAdapterService extends Service {
} }
} }
private void saveCollections(SQLiteDatabase db, Iterable<CollectionInfo> collections) { private void saveCollections(SQLiteDatabase db, Iterable<Pair<JournalManager.Journal, CollectionInfo>> journals) {
Long service = dbHelper.getService(db, account, serviceType.toString()); Long service = dbHelper.getService(db, account, serviceType.toString());
EntityDataStore<Persistable> data = ((App) context.getApplicationContext()).getData(); EntityDataStore<Persistable> data = ((App) context.getApplicationContext()).getData();
Map<String, CollectionInfo> existing = new HashMap<>(); Map<String, JournalEntity> existing = new HashMap<>();
List<CollectionInfo> existingList = JournalEntity.getCollections(data, service); for (JournalEntity journalEntity : JournalEntity.getJournals(data, service)) {
for (CollectionInfo info : existingList) { existing.put(journalEntity.getUid(), journalEntity);
existing.put(info.uid, info);
} }
for (CollectionInfo collection : collections) { for (Pair<JournalManager.Journal, CollectionInfo> pair : journals) {
App.log.log(Level.FINE, "Saving collection", collection.uid); JournalManager.Journal journal = pair.first;
CollectionInfo collection = pair.second;
App.log.log(Level.FINE, "Saving collection", journal.getUid());
collection.serviceID = service; collection.serviceID = service;
JournalEntity journalEntity = JournalEntity.fetchOrCreate(data, collection); JournalEntity journalEntity = JournalEntity.fetchOrCreate(data, collection);
journalEntity.setOwner(journal.getOwner());
journalEntity.setEncryptedKey(journal.getKey());
data.upsert(journalEntity); data.upsert(journalEntity);
existing.remove(collection.uid); existing.remove(collection.uid);
} }
for (CollectionInfo collection : existing.values()) { for (JournalEntity journalEntity : existing.values()) {
App.log.log(Level.FINE, "Deleting collection", collection.uid); 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); journalEntity.setDeleted(true);
data.update(journalEntity); data.update(journalEntity);
} }