RemoteWipeManager builds messages using the message encoder

This commit is contained in:
ameba23
2021-05-06 17:48:56 +02:00
parent fe79a18061
commit a93cbf852b
7 changed files with 175 additions and 8 deletions

View File

@@ -1,4 +1,7 @@
package org.briarproject.briar.api.remotewipe;
public interface MessageEncoder {
byte[] encodeSetupMessage();
byte[] encodeWipeMessage();
}

View File

@@ -1,4 +1,10 @@
package org.briarproject.briar.api.remotewipe;
import org.briarproject.bramble.api.FormatException;
import org.briarproject.bramble.api.data.BdfList;
public interface MessageParser {
void parseSetupMessage(BdfList body) throws FormatException;
void parseWipeMessage(BdfList body) throws FormatException;
}

View File

@@ -1,4 +1,45 @@
package org.briarproject.briar.remotewipe;
public class MessageEncoderImpl {
import org.briarproject.bramble.api.FormatException;
import org.briarproject.bramble.api.client.ClientHelper;
import org.briarproject.bramble.api.data.BdfList;
import org.briarproject.briar.api.remotewipe.MessageEncoder;
import javax.inject.Inject;
import static org.briarproject.briar.remotewipe.MessageType.SETUP;
import static org.briarproject.briar.remotewipe.MessageType.WIPE;
public class MessageEncoderImpl implements MessageEncoder {
private final ClientHelper clientHelper;
@Inject
MessageEncoderImpl(ClientHelper clientHelper) {
this.clientHelper = clientHelper;
}
@Override
public byte[] encodeSetupMessage() {
BdfList body = BdfList.of(
SETUP.getValue()
);
return encodeBody(body);
}
@Override
public byte[] encodeWipeMessage() {
BdfList body = BdfList.of(
WIPE.getValue()
);
return encodeBody(body);
}
private byte[] encodeBody(BdfList body) {
try {
return clientHelper.toByteArray(body);
} catch (FormatException e) {
throw new AssertionError(e);
}
}
}

View File

@@ -1,4 +1,24 @@
package org.briarproject.briar.remotewipe;
public class MessageParserImpl {
import org.briarproject.bramble.api.FormatException;
import org.briarproject.bramble.api.data.BdfList;
import org.briarproject.briar.api.remotewipe.MessageParser;
import javax.inject.Inject;
public class MessageParserImpl implements MessageParser {
@Inject
MessageParserImpl () {
}
@Override
public void parseSetupMessage(BdfList body) throws FormatException {
}
@Override
public void parseWipeMessage(BdfList body) throws FormatException {
}
}

View File

@@ -25,6 +25,8 @@ import org.briarproject.briar.api.attachment.AttachmentHeader;
import org.briarproject.briar.api.client.MessageTracker;
import org.briarproject.briar.api.conversation.ConversationMessageHeader;
import org.briarproject.briar.api.conversation.DeletionResult;
import org.briarproject.briar.api.remotewipe.MessageEncoder;
import org.briarproject.briar.api.remotewipe.MessageParser;
import org.briarproject.briar.api.remotewipe.RemoteWipeManager;
import org.briarproject.briar.api.remotewipe.RemoteWipeMessageHeader;
import org.briarproject.briar.client.ConversationClientImpl;
@@ -54,6 +56,8 @@ public class RemoteWipeManagerImpl extends ConversationClientImpl
private final Clock clock;
private final ContactGroupFactory contactGroupFactory;
private final ContactManager contactManager;
private final MessageEncoder messageEncoder;
private final MessageParser messageParser;
@Inject
protected RemoteWipeManagerImpl(
@@ -62,6 +66,8 @@ public class RemoteWipeManagerImpl extends ConversationClientImpl
MetadataParser metadataParser,
MessageTracker messageTracker,
Clock clock,
MessageEncoder messageEncoder,
MessageParser messageParser,
ContactManager contactManager,
ClientVersioningManager clientVersioningManager,
ContactGroupFactory contactGroupFactory) {
@@ -70,6 +76,8 @@ public class RemoteWipeManagerImpl extends ConversationClientImpl
this.contactGroupFactory = contactGroupFactory;
this.contactManager = contactManager;
this.clientVersioningManager = clientVersioningManager;
this.messageEncoder = messageEncoder;
this.messageParser = messageParser;
localGroup =
contactGroupFactory.createLocalGroup(CLIENT_ID, MAJOR_VERSION);
}
@@ -138,7 +146,7 @@ public class RemoteWipeManagerImpl extends ConversationClientImpl
if (!db.containsGroup(txn, g)) db.addGroup(txn, group);
long timestamp = clock.currentTimeMillis();
byte[] body = "setup message".getBytes(); // TODO
byte[] body = messageEncoder.encodeSetupMessage();
Message m = clientHelper.createMessage(g, timestamp, body);
// TODO remote-wipe versions of MESSAGE_KEY
@@ -160,7 +168,7 @@ public class RemoteWipeManagerImpl extends ConversationClientImpl
if (!db.containsGroup(txn, g)) db.addGroup(txn, group);
long timestamp = clock.currentTimeMillis();
byte[] body = "wipe message".getBytes(); // TODO
byte[] body = messageEncoder.encodeWipeMessage();
Message m = clientHelper.createMessage(g, timestamp, body);
// TODO remote-wipe versions of MESSAGE_KEY

View File

@@ -1,10 +1,15 @@
package org.briarproject.briar.remotewipe;
import org.briarproject.bramble.api.client.ClientHelper;
import org.briarproject.bramble.api.contact.ContactManager;
import org.briarproject.bramble.api.data.MetadataEncoder;
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
import org.briarproject.bramble.api.sync.validation.ValidationManager;
import org.briarproject.bramble.api.system.Clock;
import org.briarproject.bramble.api.versioning.ClientVersioningManager;
import org.briarproject.briar.api.conversation.ConversationManager;
import org.briarproject.briar.api.remotewipe.MessageEncoder;
import org.briarproject.briar.api.remotewipe.MessageParser;
import org.briarproject.briar.api.remotewipe.RemoteWipeManager;
import javax.inject.Inject;
@@ -13,15 +18,17 @@ import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import static org.briarproject.briar.api.socialbackup.SocialBackupManager.CLIENT_ID;
import static org.briarproject.briar.api.socialbackup.SocialBackupManager.MAJOR_VERSION;
import static org.briarproject.briar.api.socialbackup.SocialBackupManager.MINOR_VERSION;
import static org.briarproject.briar.api.remotewipe.RemoteWipeManager.CLIENT_ID;
import static org.briarproject.briar.api.remotewipe.RemoteWipeManager.MAJOR_VERSION;
import static org.briarproject.briar.api.remotewipe.RemoteWipeManager.MINOR_VERSION;
@Module
public class RemoteWipeModule {
public static class EagerSingletons {
@Inject
RemoteWipeManager remoteWipeManager;
@Inject
RemoteWipeValidator remoteWipeValidator;
}
@Provides
@@ -44,4 +51,28 @@ public class RemoteWipeModule {
conversationManager.registerConversationClient(remoteWipeManager);
return remoteWipeManager;
}
@Provides
@Singleton
RemoteWipeValidator remoteWipeValidator(
ValidationManager validationManager,
ClientHelper clientHelper,
MetadataEncoder metadataEncoder,
Clock clock) {
RemoteWipeValidator validator =
new RemoteWipeValidator(clientHelper, metadataEncoder, clock);
validationManager.registerMessageValidator(CLIENT_ID, MAJOR_VERSION,
validator);
return validator;
}
@Provides
MessageEncoder messageEncoder(MessageEncoderImpl messageEncoder) {
return messageEncoder;
}
@Provides
MessageParser messageParser(MessageParserImpl messageParser) {
return messageParser;
}
}

View File

@@ -1,4 +1,62 @@
package org.briarproject.briar.remotewipe;
public class RemoteWipeValidator {
import org.briarproject.bramble.api.FormatException;
import org.briarproject.bramble.api.client.BdfMessageContext;
import org.briarproject.bramble.api.client.BdfMessageValidator;
import org.briarproject.bramble.api.client.ClientHelper;
import org.briarproject.bramble.api.data.BdfDictionary;
import org.briarproject.bramble.api.data.BdfEntry;
import org.briarproject.bramble.api.data.BdfList;
import org.briarproject.bramble.api.data.MetadataEncoder;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.bramble.api.sync.Group;
import org.briarproject.bramble.api.sync.Message;
import org.briarproject.bramble.api.system.Clock;
import javax.annotation.concurrent.Immutable;
import javax.inject.Inject;
import static org.briarproject.bramble.util.ValidationUtils.checkSize;
import static org.briarproject.briar.remotewipe.MessageType.SETUP;
import static org.briarproject.briar.remotewipe.MessageType.WIPE;
import static org.briarproject.briar.socialbackup.SocialBackupConstants.MSG_KEY_LOCAL;
import static org.briarproject.briar.socialbackup.SocialBackupConstants.MSG_KEY_MESSAGE_TYPE;
@Immutable
@NotNullByDefault
class RemoteWipeValidator extends BdfMessageValidator {
@Inject
RemoteWipeValidator(ClientHelper clientHelper,
MetadataEncoder metadataEncoder, Clock clock) {
super(clientHelper, metadataEncoder, clock);
}
@Override
protected BdfMessageContext validateMessage(Message m, Group g,
BdfList body) throws FormatException {
MessageType type = MessageType
.fromValue(body.getLong(0).intValue());
if (type == SETUP) return validateSetupMessage(body);
else if (type == WIPE) return validateWipeMessage(body);
else throw new AssertionError();
}
private BdfMessageContext validateSetupMessage(BdfList body)
throws FormatException {
checkSize(body, 1);
BdfDictionary meta = BdfDictionary.of(
new BdfEntry(MSG_KEY_MESSAGE_TYPE, SETUP.getValue()),
new BdfEntry(MSG_KEY_LOCAL, false));
return new BdfMessageContext(meta);
}
private BdfMessageContext validateWipeMessage(BdfList body)
throws FormatException {
checkSize(body, 1);
BdfDictionary meta = BdfDictionary.of(
new BdfEntry(MSG_KEY_MESSAGE_TYPE, WIPE.getValue()),
new BdfEntry(MSG_KEY_LOCAL, false));
return new BdfMessageContext(meta);
}
}