mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 11:19:04 +01:00
Refactor and create a RestoreAccount class which does the combining
This commit is contained in:
@@ -6,10 +6,10 @@ import java.util.List;
|
||||
|
||||
public class SocialBackup {
|
||||
private Identity identity;
|
||||
private List<ContactData> contacts;
|
||||
private List<org.briarproject.briar.api.socialbackup.ContactData> contacts;
|
||||
private int version;
|
||||
|
||||
SocialBackup (Identity identity, List<ContactData> contacts, int version) {
|
||||
SocialBackup (Identity identity, List<org.briarproject.briar.api.socialbackup.ContactData> contacts, int version) {
|
||||
this.identity = identity;
|
||||
this.contacts = contacts;
|
||||
this.version = version;
|
||||
@@ -19,7 +19,7 @@ public class SocialBackup {
|
||||
return identity;
|
||||
}
|
||||
|
||||
public List<ContactData> getContacts() {
|
||||
public List<org.briarproject.briar.api.socialbackup.ContactData> getContacts() {
|
||||
return contacts;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
package org.briarproject.briar.api.socialbackup.recovery;
|
||||
|
||||
public interface RestoreAccount {
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import org.briarproject.bramble.api.FormatException;
|
||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.briar.api.socialbackup.BackupPayload;
|
||||
import org.briarproject.briar.api.socialbackup.SocialBackup;
|
||||
|
||||
import java.security.GeneralSecurityException;
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ import org.briarproject.bramble.api.properties.TransportProperties;
|
||||
import org.briarproject.briar.api.socialbackup.BackupPayload;
|
||||
import org.briarproject.briar.api.socialbackup.MessageParser;
|
||||
import org.briarproject.briar.api.socialbackup.Shard;
|
||||
import org.briarproject.briar.api.socialbackup.SocialBackup;
|
||||
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.SecureRandom;
|
||||
@@ -54,7 +55,7 @@ public class BackupPayloadDecoderImpl implements BackupPayloadDecoder {
|
||||
this.messageParser = messageParser;
|
||||
}
|
||||
|
||||
public SocialBackup decodeBackupPayload(
|
||||
public org.briarproject.briar.api.socialbackup.SocialBackup decodeBackupPayload(
|
||||
SecretKey secret,
|
||||
BackupPayload backupPayload)
|
||||
throws FormatException, GeneralSecurityException {
|
||||
@@ -103,7 +104,7 @@ public class BackupPayloadDecoderImpl implements BackupPayloadDecoder {
|
||||
handShakePrivateKey, created);
|
||||
LOG.info("New identity created");
|
||||
|
||||
List<ContactData> contactDataList = new ArrayList();
|
||||
List<org.briarproject.briar.api.socialbackup.ContactData> contactDataList = new ArrayList();
|
||||
|
||||
for (int i = 0; i < bdfContactData.size(); i++) {
|
||||
BdfList bdfData = bdfContactData.getList(i);
|
||||
@@ -139,8 +140,8 @@ public class BackupPayloadDecoderImpl implements BackupPayloadDecoder {
|
||||
Contact contact =
|
||||
new Contact(contactId, author, author.getId(), alias,
|
||||
contactHandshakePublicKey, false);
|
||||
ContactData contactData =
|
||||
new ContactData(contact, properties, shard);
|
||||
org.briarproject.briar.api.socialbackup.ContactData contactData =
|
||||
new org.briarproject.briar.api.socialbackup.ContactData(contact, properties, shard);
|
||||
contactDataList.add(contactData);
|
||||
LOG.info("Contact added");
|
||||
}
|
||||
|
||||
@@ -10,5 +10,5 @@ import java.util.List;
|
||||
interface BackupPayloadEncoder {
|
||||
|
||||
org.briarproject.briar.api.socialbackup.BackupPayload encodeBackupPayload(SecretKey secret, Identity identity,
|
||||
List<ContactData> contactData, int version);
|
||||
List<org.briarproject.briar.api.socialbackup.ContactData> contactData, int version);
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ class BackupPayloadEncoderImpl implements BackupPayloadEncoder {
|
||||
|
||||
@Override
|
||||
public org.briarproject.briar.api.socialbackup.BackupPayload encodeBackupPayload(SecretKey secret,
|
||||
Identity identity, List<ContactData> contactData, int version) {
|
||||
Identity identity, List<org.briarproject.briar.api.socialbackup.ContactData> contactData, int version) {
|
||||
// Encode the local identity
|
||||
BdfList bdfIdentity = new BdfList();
|
||||
LocalAuthor localAuthor = identity.getLocalAuthor();
|
||||
@@ -56,7 +56,7 @@ class BackupPayloadEncoderImpl implements BackupPayloadEncoder {
|
||||
bdfIdentity.add(identity.getHandshakePrivateKey().getEncoded());
|
||||
// Encode the contact data
|
||||
BdfList bdfContactData = new BdfList();
|
||||
for (ContactData cd : contactData) {
|
||||
for (org.briarproject.briar.api.socialbackup.ContactData cd : contactData) {
|
||||
BdfList bdfData = new BdfList();
|
||||
Contact contact = cd.getContact();
|
||||
bdfData.add(clientHelper.toList(contact.getAuthor()));
|
||||
|
||||
@@ -193,12 +193,12 @@ class SocialBackupManagerImpl extends ConversationClientImpl
|
||||
// Add the shard to our backup, if any
|
||||
if (localBackupExists(txn)) {
|
||||
Shard shard = messageParser.parseShardMessage(body);
|
||||
List<ContactData> contactData = loadContactData(txn);
|
||||
ListIterator<ContactData> it = contactData.listIterator();
|
||||
List<org.briarproject.briar.api.socialbackup.ContactData> contactData = loadContactData(txn);
|
||||
ListIterator<org.briarproject.briar.api.socialbackup.ContactData> it = contactData.listIterator();
|
||||
while (it.hasNext()) {
|
||||
ContactData cd = it.next();
|
||||
org.briarproject.briar.api.socialbackup.ContactData cd = it.next();
|
||||
if (cd.getContact().getId().equals(contactId)) {
|
||||
it.set(new ContactData(cd.getContact(),
|
||||
it.set(new org.briarproject.briar.api.socialbackup.ContactData(cd.getContact(),
|
||||
cd.getProperties(), shard));
|
||||
updateBackup(txn, contactData);
|
||||
break;
|
||||
@@ -278,7 +278,7 @@ class SocialBackupManagerImpl extends ConversationClientImpl
|
||||
}
|
||||
// Create the encrypted backup payload
|
||||
SecretKey secret = crypto.generateSecretKey();
|
||||
List<ContactData> contactData = loadContactData(txn);
|
||||
List<org.briarproject.briar.api.socialbackup.ContactData> contactData = loadContactData(txn);
|
||||
BackupPayload payload =
|
||||
createBackupPayload(txn, secret, contactData, 0);
|
||||
// Create the shards
|
||||
@@ -415,17 +415,17 @@ class SocialBackupManagerImpl extends ConversationClientImpl
|
||||
}
|
||||
|
||||
private BackupPayload createBackupPayload(Transaction txn,
|
||||
SecretKey secret, List<ContactData> contactData, int version)
|
||||
SecretKey secret, List<org.briarproject.briar.api.socialbackup.ContactData> contactData, int version)
|
||||
throws DbException {
|
||||
Identity identity = identityManager.getIdentity(txn);
|
||||
return backupPayloadEncoder.encodeBackupPayload(secret, identity,
|
||||
contactData, version);
|
||||
}
|
||||
|
||||
private List<ContactData> loadContactData(Transaction txn)
|
||||
private List<org.briarproject.briar.api.socialbackup.ContactData> loadContactData(Transaction txn)
|
||||
throws DbException {
|
||||
Collection<Contact> contacts = contactManager.getContacts(txn);
|
||||
List<ContactData> contactData = new ArrayList<>();
|
||||
List<org.briarproject.briar.api.socialbackup.ContactData> contactData = new ArrayList<>();
|
||||
for (Contact c : contacts) {
|
||||
// Skip contacts that are in the process of being removed
|
||||
Group contactGroup = getContactGroup(c);
|
||||
@@ -433,7 +433,7 @@ class SocialBackupManagerImpl extends ConversationClientImpl
|
||||
Map<TransportId, TransportProperties> props =
|
||||
getTransportProperties(txn, c.getId());
|
||||
Shard shard = getRemoteShard(txn, contactGroup.getId());
|
||||
contactData.add(new ContactData(c, props, shard));
|
||||
contactData.add(new org.briarproject.briar.api.socialbackup.ContactData(c, props, shard));
|
||||
}
|
||||
return contactData;
|
||||
}
|
||||
@@ -513,7 +513,7 @@ class SocialBackupManagerImpl extends ConversationClientImpl
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
private void updateBackup(Transaction txn, List<ContactData> contactData)
|
||||
private void updateBackup(Transaction txn, List<org.briarproject.briar.api.socialbackup.ContactData> contactData)
|
||||
throws DbException {
|
||||
BackupMetadata backupMetadata = requireNonNull(getBackupMetadata(txn));
|
||||
int newVersion = backupMetadata.getVersion() + 1;
|
||||
|
||||
@@ -11,8 +11,10 @@ import org.briarproject.briar.api.conversation.ConversationManager;
|
||||
import org.briarproject.briar.api.socialbackup.SocialBackupExchangeManager;
|
||||
import org.briarproject.briar.api.socialbackup.SocialBackupManager;
|
||||
import org.briarproject.briar.api.socialbackup.recovery.CustodianTask;
|
||||
import org.briarproject.briar.api.socialbackup.recovery.RestoreAccount;
|
||||
import org.briarproject.briar.api.socialbackup.recovery.SecretOwnerTask;
|
||||
import org.briarproject.briar.socialbackup.recovery.CustodianTaskImpl;
|
||||
import org.briarproject.briar.socialbackup.recovery.RestoreAccountImpl;
|
||||
import org.briarproject.briar.socialbackup.recovery.SecretOwnerTaskImpl;
|
||||
|
||||
import javax.inject.Inject;
|
||||
@@ -119,4 +121,9 @@ public class SocialBackupModule {
|
||||
CustodianTask custodianTask(CustodianTaskImpl custodianTask) {
|
||||
return custodianTask;
|
||||
}
|
||||
|
||||
@Provides
|
||||
RestoreAccount restoreAccount(RestoreAccountImpl restoreAccount) {
|
||||
return restoreAccount;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
package org.briarproject.briar.socialbackup.recovery;
|
||||
|
||||
import org.briarproject.bramble.api.FormatException;
|
||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.briar.api.socialbackup.BackupPayload;
|
||||
import org.briarproject.briar.api.socialbackup.DarkCrystal;
|
||||
import org.briarproject.briar.api.socialbackup.ReturnShardPayload;
|
||||
import org.briarproject.briar.api.socialbackup.Shard;
|
||||
import org.briarproject.briar.api.socialbackup.recovery.RestoreAccount;
|
||||
import org.briarproject.briar.socialbackup.BackupPayloadDecoder;
|
||||
import org.briarproject.briar.api.socialbackup.SocialBackup;
|
||||
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class RestoreAccountImpl implements RestoreAccount {
|
||||
private ArrayList<ReturnShardPayload> recoveredShards = new ArrayList<>();
|
||||
private final DarkCrystal darkCrystal;
|
||||
private SecretKey secretKey;
|
||||
private final BackupPayloadDecoder backupPayloadDecoder;
|
||||
private SocialBackup socialBackup;
|
||||
|
||||
@Inject
|
||||
RestoreAccountImpl(DarkCrystal darkCrystal, BackupPayloadDecoder backupPayloadDecoder) {
|
||||
this.darkCrystal = darkCrystal;
|
||||
this.backupPayloadDecoder = backupPayloadDecoder;
|
||||
}
|
||||
|
||||
public int getNumberOfShards() { return recoveredShards.size(); }
|
||||
|
||||
// TODO figure out how to actually use a hash set for these objects
|
||||
public boolean addReturnShardPayload(ReturnShardPayload toAdd) {
|
||||
boolean found = false;
|
||||
for (ReturnShardPayload returnShardPayload : recoveredShards) {
|
||||
if (toAdd.equals(returnShardPayload)) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) recoveredShards.add(toAdd);
|
||||
return !found;
|
||||
}
|
||||
|
||||
public boolean canRecover() {
|
||||
ArrayList<Shard> shards = new ArrayList();
|
||||
for (ReturnShardPayload returnShardPayload : recoveredShards) {
|
||||
// TODO check shards all have same secret id
|
||||
shards.add(returnShardPayload.getShard());
|
||||
}
|
||||
try {
|
||||
secretKey = darkCrystal.combineShards(shards);
|
||||
} catch (GeneralSecurityException e) {
|
||||
// TODO handle error message
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public int recover() throws FormatException, GeneralSecurityException {
|
||||
if (secretKey == null) throw new GeneralSecurityException();
|
||||
// TODO find backup with highest version number
|
||||
BackupPayload backupPayload = recoveredShards.get(0).getBackupPayload();
|
||||
socialBackup = backupPayloadDecoder.decodeBackupPayload(secretKey, backupPayload);
|
||||
int version = socialBackup.getVersion();
|
||||
return version;
|
||||
}
|
||||
|
||||
public SocialBackup getSocialBackup() {
|
||||
return socialBackup;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user