1
0
mirror of https://github.com/etesync/android synced 2025-01-11 00:01:12 +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() {
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);
}

View File

@ -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;

View File

@ -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<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) {
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.
journal.afterLoad();
ret.add(journal.getInfo());

View File

@ -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<CollectionInfo> collections = new LinkedList<>();
List<Pair<JournalManager.Journal, CollectionInfo>> 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<CollectionInfo> collections) {
private void saveCollections(SQLiteDatabase db, Iterable<Pair<JournalManager.Journal, CollectionInfo>> journals) {
Long service = dbHelper.getService(db, account, serviceType.toString());
EntityDataStore<Persistable> data = ((App) context.getApplicationContext()).getData();
Map<String, CollectionInfo> existing = new HashMap<>();
List<CollectionInfo> existingList = JournalEntity.getCollections(data, service);
for (CollectionInfo info : existingList) {
existing.put(info.uid, info);
Map<String, JournalEntity> 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<JournalManager.Journal, CollectionInfo> 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);
}