Implement New Member and Join Announcements in GroupMessageFactory

This commit is contained in:
Torsten Grote
2016-10-19 18:28:35 -02:00
parent a6e3827127
commit e06726b2f9
4 changed files with 127 additions and 26 deletions

View File

@@ -3,18 +3,25 @@ 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.identity.LocalAuthor;
import org.briarproject.api.nullsafety.NotNullByDefault;
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 org.jetbrains.annotations.Nullable;
import java.security.GeneralSecurityException;
import javax.inject.Inject;
import static org.briarproject.api.privategroup.MessageType.JOIN;
import static org.briarproject.api.privategroup.MessageType.NEW_MEMBER;
import static org.briarproject.api.privategroup.MessageType.POST;
@NotNullByDefault
class GroupMessageFactoryImpl implements GroupMessageFactory {
private final ClientHelper clientHelper;
@@ -24,20 +31,78 @@ class GroupMessageFactoryImpl implements GroupMessageFactory {
this.clientHelper = clientHelper;
}
@NotNull
@Override
public GroupMessage createNewMemberMessage(GroupId groupId, long timestamp,
LocalAuthor creator, Author member) {
try {
// Generate the signature
BdfList toSign = BdfList.of(groupId, timestamp, member.getName(),
member.getPublicKey());
byte[] signature =
clientHelper.sign(toSign, creator.getPrivateKey());
// Compose the message
BdfList body =
BdfList.of(NEW_MEMBER.getInt(), member.getName(),
member.getPublicKey(), signature);
Message m = clientHelper.createMessage(groupId, timestamp, body);
return new GroupMessage(m, null, member);
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
} catch (FormatException e) {
throw new RuntimeException(e);
}
}
@Override
public GroupMessage createJoinMessage(GroupId groupId, long timestamp,
LocalAuthor member, MessageId newMemberId) {
try {
// Generate the signature
BdfList toSign = BdfList.of(groupId, timestamp, member.getName(),
member.getPublicKey(), newMemberId);
byte[] signature =
clientHelper.sign(toSign, member.getPrivateKey());
// Compose the message
BdfList body =
BdfList.of(JOIN.getInt(), member.getName(),
member.getPublicKey(), newMemberId, signature);
Message m = clientHelper.createMessage(groupId, timestamp, body);
return new GroupMessage(m, null, member);
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
} catch (FormatException e) {
throw new RuntimeException(e);
}
}
@Override
public GroupMessage createGroupMessage(GroupId groupId, long timestamp,
MessageId parent, LocalAuthor author, String body)
throws FormatException, GeneralSecurityException {
@Nullable MessageId parentId, LocalAuthor author, String content,
MessageId previousMsgId) {
try {
// Generate the signature
BdfList toSign = BdfList.of(groupId, timestamp, author.getName(),
author.getPublicKey(), parentId, previousMsgId, content);
byte[] signature =
clientHelper.sign(toSign, author.getPrivateKey());
// Generate the signature
byte[] sig = clientHelper.sign(new BdfList(), author.getPrivateKey());
// Compose the message
BdfList body =
BdfList.of(POST.getInt(), author.getName(),
author.getPublicKey(), parentId, previousMsgId,
content, signature);
Message m = clientHelper.createMessage(groupId, timestamp, body);
// Compose the message
Message m =
clientHelper.createMessage(groupId, timestamp, new BdfList());
return new GroupMessage(m, parent, author);
return new GroupMessage(m, parentId, author);
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
} catch (FormatException e) {
throw new RuntimeException(e);
}
}
}