Create PrivateGroupManager Facade and stub implementation

This commit is contained in:
Torsten Grote
2016-09-28 16:54:28 -03:00
parent 8b50cb1461
commit 6ece398a21
38 changed files with 691 additions and 146 deletions

View File

@@ -0,0 +1,8 @@
package org.briarproject.privategroup;
interface Constants {
// Database keys
String KEY_READ = "read";
}

View File

@@ -0,0 +1,43 @@
package org.briarproject.privategroup;
import org.briarproject.api.FormatException;
import org.briarproject.api.clients.ClientHelper;
import org.briarproject.api.data.BdfList;
import org.briarproject.api.identity.LocalAuthor;
import org.briarproject.api.privategroup.GroupMessage;
import org.briarproject.api.privategroup.GroupMessageFactory;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageId;
import org.jetbrains.annotations.NotNull;
import java.security.GeneralSecurityException;
import javax.inject.Inject;
class GroupMessageFactoryImpl implements GroupMessageFactory {
private final ClientHelper clientHelper;
@Inject
GroupMessageFactoryImpl(ClientHelper clientHelper) {
this.clientHelper = clientHelper;
}
@NotNull
@Override
public GroupMessage createGroupMessage(GroupId groupId, long timestamp,
MessageId parent, LocalAuthor author, String body)
throws FormatException, GeneralSecurityException {
// Generate the signature
byte[] sig = clientHelper.sign(new BdfList(), author.getPrivateKey());
// Compose the message
Message m =
clientHelper.createMessage(groupId, timestamp, new BdfList());
return new GroupMessage(m, parent, author);
}
}

View File

@@ -0,0 +1,43 @@
package org.briarproject.privategroup;
import org.briarproject.api.FormatException;
import org.briarproject.api.clients.BdfMessageContext;
import org.briarproject.api.clients.ClientHelper;
import org.briarproject.api.crypto.CryptoComponent;
import org.briarproject.api.data.BdfDictionary;
import org.briarproject.api.data.BdfList;
import org.briarproject.api.data.MetadataEncoder;
import org.briarproject.api.identity.AuthorFactory;
import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.InvalidMessageException;
import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageId;
import org.briarproject.api.system.Clock;
import org.briarproject.clients.BdfMessageValidator;
import java.util.Collection;
import java.util.Collections;
class GroupMessageValidator extends BdfMessageValidator {
private final CryptoComponent crypto;
private final AuthorFactory authorFactory;
GroupMessageValidator(CryptoComponent crypto, AuthorFactory authorFactory,
ClientHelper clientHelper, MetadataEncoder metadataEncoder,
Clock clock) {
super(clientHelper, metadataEncoder, clock);
this.crypto = crypto;
this.authorFactory = authorFactory;
}
@Override
protected BdfMessageContext validateMessage(Message m, Group g,
BdfList body) throws InvalidMessageException, FormatException {
BdfDictionary meta = new BdfDictionary();
Collection<MessageId> dependencies = Collections.emptyList();
return new BdfMessageContext(meta, dependencies);
}
}

View File

@@ -0,0 +1,69 @@
package org.briarproject.privategroup;
import org.briarproject.api.FormatException;
import org.briarproject.api.clients.ClientHelper;
import org.briarproject.api.data.BdfList;
import org.briarproject.api.identity.Author;
import org.briarproject.api.privategroup.PrivateGroup;
import org.briarproject.api.privategroup.PrivateGroupFactory;
import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.GroupFactory;
import org.briarproject.util.StringUtils;
import org.jetbrains.annotations.NotNull;
import java.security.SecureRandom;
import javax.inject.Inject;
import static org.briarproject.api.privategroup.PrivateGroupConstants.GROUP_SALT_LENGTH;
import static org.briarproject.api.privategroup.PrivateGroupConstants.MAX_GROUP_NAME_LENGTH;
class PrivateGroupFactoryImpl implements PrivateGroupFactory {
private final GroupFactory groupFactory;
private final ClientHelper clientHelper;
private final SecureRandom random;
@Inject
PrivateGroupFactoryImpl(GroupFactory groupFactory,
ClientHelper clientHelper, SecureRandom random) {
this.groupFactory = groupFactory;
this.clientHelper = clientHelper;
this.random = random;
}
@NotNull
@Override
public PrivateGroup createPrivateGroup(String name, Author author) {
int length = StringUtils.toUtf8(name).length;
if (length == 0) throw new IllegalArgumentException("Group name empty");
if (length > MAX_GROUP_NAME_LENGTH)
throw new IllegalArgumentException(
"Group name exceeds maximum length");
byte[] salt = new byte[GROUP_SALT_LENGTH];
random.nextBytes(salt);
return createPrivateGroup(name, author, salt);
}
@NotNull
@Override
public PrivateGroup createPrivateGroup(String name, Author author,
byte[] salt) {
try {
BdfList group = BdfList.of(
name,
author.getName(),
author.getPublicKey(),
salt
);
byte[] descriptor = clientHelper.toByteArray(group);
Group g = groupFactory
.createGroup(PrivateGroupManagerImpl.CLIENT_ID, descriptor);
return new PrivateGroup(g, name, author, salt);
} catch (FormatException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -0,0 +1,125 @@
package org.briarproject.privategroup;
import org.briarproject.api.FormatException;
import org.briarproject.api.clients.ClientHelper;
import org.briarproject.api.data.BdfDictionary;
import org.briarproject.api.data.BdfList;
import org.briarproject.api.data.MetadataParser;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
import org.briarproject.api.db.Transaction;
import org.briarproject.api.identity.Author;
import org.briarproject.api.identity.IdentityManager;
import org.briarproject.api.privategroup.GroupMessage;
import org.briarproject.api.privategroup.GroupMessageHeader;
import org.briarproject.api.privategroup.PrivateGroup;
import org.briarproject.api.privategroup.PrivateGroupFactory;
import org.briarproject.api.privategroup.PrivateGroupManager;
import org.briarproject.api.sync.ClientId;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageId;
import org.briarproject.clients.BdfIncomingMessageHook;
import org.briarproject.util.StringUtils;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.Collections;
import javax.inject.Inject;
import static org.briarproject.privategroup.Constants.KEY_READ;
public class PrivateGroupManagerImpl extends BdfIncomingMessageHook implements
PrivateGroupManager {
static final ClientId CLIENT_ID = new ClientId(
StringUtils.fromHexString("5072697661746547726f75704d616e61"
+ "67657220627920546f727374656e2047"));
private final DatabaseComponent db;
private final IdentityManager identityManager;
private final PrivateGroupFactory privateGroupFactory;
@Inject
PrivateGroupManagerImpl(ClientHelper clientHelper,
MetadataParser metadataParser, DatabaseComponent db,
IdentityManager identityManager,
PrivateGroupFactory privateGroupFactory) {
super(clientHelper, metadataParser);
this.db = db;
this.identityManager = identityManager;
this.privateGroupFactory = privateGroupFactory;
}
@NotNull
@Override
public ClientId getClientId() {
return CLIENT_ID;
}
@Override
public void addLocalMessage(GroupMessage m) throws DbException {
try {
BdfDictionary meta = new BdfDictionary();
clientHelper.addLocalMessage(m.getMessage(), meta, true);
} catch (FormatException e) {
throw new DbException(e);
}
}
@NotNull
@Override
public PrivateGroup getPrivateGroup(GroupId g) throws DbException {
Author a = identityManager.getLocalAuthor();
return privateGroupFactory.createPrivateGroup("todo", a);
}
@NotNull
@Override
public PrivateGroup getPrivateGroup(Transaction txn, GroupId g)
throws DbException {
Author a = identityManager.getLocalAuthor(txn);
return privateGroupFactory.createPrivateGroup("todo", a);
}
@NotNull
@Override
public Collection<PrivateGroup> getPrivateGroups() throws DbException {
return Collections.emptyList();
}
@NotNull
@Override
public String getMessageBody(MessageId m) throws DbException {
return "empty";
}
@NotNull
@Override
public Collection<GroupMessageHeader> getHeaders(GroupId g)
throws DbException {
return Collections.emptyList();
}
@Override
public void setReadFlag(MessageId m, boolean read) throws DbException {
try {
BdfDictionary meta = new BdfDictionary();
meta.put(KEY_READ, read);
clientHelper.mergeMessageMetadata(m, meta);
} catch (FormatException e) {
throw new RuntimeException(e);
}
}
@Override
protected boolean incomingMessage(Transaction txn, Message m, BdfList body,
BdfDictionary meta) throws DbException, FormatException {
return true;
}
}

View File

@@ -0,0 +1,68 @@
package org.briarproject.privategroup;
import org.briarproject.api.clients.ClientHelper;
import org.briarproject.api.crypto.CryptoComponent;
import org.briarproject.api.data.MetadataEncoder;
import org.briarproject.api.identity.AuthorFactory;
import org.briarproject.api.privategroup.GroupMessageFactory;
import org.briarproject.api.privategroup.PrivateGroupFactory;
import org.briarproject.api.privategroup.PrivateGroupManager;
import org.briarproject.api.sync.GroupFactory;
import org.briarproject.api.sync.ValidationManager;
import org.briarproject.api.system.Clock;
import java.security.SecureRandom;
import javax.inject.Inject;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
@Module
public class PrivateGroupModule {
public static class EagerSingletons {
@Inject
GroupMessageValidator groupMessageValidator;
}
@Provides
@Singleton
PrivateGroupManager provideForumManager(
PrivateGroupManagerImpl groupManager,
ValidationManager validationManager) {
validationManager
.registerIncomingMessageHook(groupManager.getClientId(),
groupManager);
return groupManager;
}
@Provides
PrivateGroupFactory providePrivateGroupFactory(
PrivateGroupFactoryImpl privateGroupFactory) {
return privateGroupFactory;
}
@Provides
GroupMessageFactory provideGroupMessageFactory(
GroupMessageFactoryImpl groupMessageFactory) {
return groupMessageFactory;
}
@Provides
@Singleton
GroupMessageValidator provideGroupMessageValidator(
ValidationManager validationManager, CryptoComponent crypto,
AuthorFactory authorFactory, ClientHelper clientHelper,
MetadataEncoder metadataEncoder, Clock clock) {
GroupMessageValidator validator = new GroupMessageValidator(crypto,
authorFactory, clientHelper, metadataEncoder, clock);
validationManager.registerMessageValidator(
PrivateGroupManagerImpl.CLIENT_ID, validator);
return validator;
}
}