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,7 +3,6 @@ package org.briarproject.api.clients;
import org.briarproject.api.nullsafety.NotNullByDefault;
import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageId;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.annotation.concurrent.Immutable;

View File

@@ -5,7 +5,6 @@ import org.briarproject.api.identity.Author;
import org.briarproject.api.nullsafety.NotNullByDefault;
import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageId;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.annotation.concurrent.Immutable;
@@ -14,16 +13,16 @@ import javax.annotation.concurrent.Immutable;
@NotNullByDefault
public class GroupMessage extends BaseMessage {
private final Author author;
private final Author member;
public GroupMessage(Message message, @Nullable MessageId parent,
Author author) {
Author member) {
super(message, parent);
this.author = author;
this.member = member;
}
public Author getAuthor() {
return author;
public Author getMember() {
return member;
}
}

View File

@@ -1,20 +1,58 @@
package org.briarproject.api.privategroup;
import org.briarproject.api.FormatException;
import org.briarproject.api.crypto.PrivateKey;
import org.briarproject.api.crypto.CryptoExecutor;
import org.briarproject.api.identity.Author;
import org.briarproject.api.identity.LocalAuthor;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.MessageId;
import org.jetbrains.annotations.NotNull;
import java.security.GeneralSecurityException;
import org.jetbrains.annotations.Nullable;
public interface GroupMessageFactory {
@NotNull
/**
* Creates a new member announcement that contains the joiner's identity
* and is signed by the creator.
* <p>
* When a new member accepts an invitation to the group,
* the creator sends this new member announcement to the group.
*
* @param groupId The ID of the group the new member joined
* @param timestamp The current timestamp
* @param creator The creator of the group with {@param groupId}
* @param member The new member that has just accepted an invitation
*/
@CryptoExecutor
GroupMessage createNewMemberMessage(GroupId groupId, long timestamp,
LocalAuthor creator, Author member);
/**
* Creates a join announcement message
* that depends on a previous new member announcement.
*
* @param groupId The ID of the Group that is being joined
* @param timestamp Must be equal to the timestamp of the new member message
* @param member Our own LocalAuthor
* @param newMemberId The MessageId of the new member message
*/
@CryptoExecutor
GroupMessage createJoinMessage(GroupId groupId, long timestamp,
LocalAuthor member, MessageId newMemberId);
/**
* Creates a group message
*
* @param groupId The ID of the Group that is posted in
* @param timestamp Must be greater than the timestamps of the parentId
* post, if any, and the member's previous message
* @param parentId The ID of the message that is replied to
* @param author The author of the group message
* @param body The content of the group message
* @param previousMsgId The ID of the author's previous message
* in this group
*/
@CryptoExecutor
GroupMessage createGroupMessage(GroupId groupId, long timestamp,
MessageId parent, LocalAuthor author, String body)
throws FormatException, GeneralSecurityException;
@Nullable MessageId parentId, LocalAuthor author, String body,
MessageId previousMsgId);
}

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);
}
}
}