mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 13:19:52 +01:00
Implement New Member and Join Announcements in GroupMessageFactory
This commit is contained in:
@@ -3,7 +3,6 @@ package org.briarproject.api.clients;
|
|||||||
import org.briarproject.api.nullsafety.NotNullByDefault;
|
import org.briarproject.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.api.sync.Message;
|
import org.briarproject.api.sync.Message;
|
||||||
import org.briarproject.api.sync.MessageId;
|
import org.briarproject.api.sync.MessageId;
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import javax.annotation.concurrent.Immutable;
|
import javax.annotation.concurrent.Immutable;
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import org.briarproject.api.identity.Author;
|
|||||||
import org.briarproject.api.nullsafety.NotNullByDefault;
|
import org.briarproject.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.api.sync.Message;
|
import org.briarproject.api.sync.Message;
|
||||||
import org.briarproject.api.sync.MessageId;
|
import org.briarproject.api.sync.MessageId;
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import javax.annotation.concurrent.Immutable;
|
import javax.annotation.concurrent.Immutable;
|
||||||
@@ -14,16 +13,16 @@ import javax.annotation.concurrent.Immutable;
|
|||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
public class GroupMessage extends BaseMessage {
|
public class GroupMessage extends BaseMessage {
|
||||||
|
|
||||||
private final Author author;
|
private final Author member;
|
||||||
|
|
||||||
public GroupMessage(Message message, @Nullable MessageId parent,
|
public GroupMessage(Message message, @Nullable MessageId parent,
|
||||||
Author author) {
|
Author member) {
|
||||||
super(message, parent);
|
super(message, parent);
|
||||||
this.author = author;
|
this.member = member;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Author getAuthor() {
|
public Author getMember() {
|
||||||
return author;
|
return member;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,20 +1,58 @@
|
|||||||
package org.briarproject.api.privategroup;
|
package org.briarproject.api.privategroup;
|
||||||
|
|
||||||
import org.briarproject.api.FormatException;
|
import org.briarproject.api.crypto.CryptoExecutor;
|
||||||
import org.briarproject.api.crypto.PrivateKey;
|
|
||||||
import org.briarproject.api.identity.Author;
|
import org.briarproject.api.identity.Author;
|
||||||
import org.briarproject.api.identity.LocalAuthor;
|
import org.briarproject.api.identity.LocalAuthor;
|
||||||
import org.briarproject.api.sync.GroupId;
|
import org.briarproject.api.sync.GroupId;
|
||||||
import org.briarproject.api.sync.MessageId;
|
import org.briarproject.api.sync.MessageId;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.security.GeneralSecurityException;
|
|
||||||
|
|
||||||
public interface GroupMessageFactory {
|
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,
|
GroupMessage createGroupMessage(GroupId groupId, long timestamp,
|
||||||
MessageId parent, LocalAuthor author, String body)
|
@Nullable MessageId parentId, LocalAuthor author, String body,
|
||||||
throws FormatException, GeneralSecurityException;
|
MessageId previousMsgId);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,18 +3,25 @@ package org.briarproject.privategroup;
|
|||||||
import org.briarproject.api.FormatException;
|
import org.briarproject.api.FormatException;
|
||||||
import org.briarproject.api.clients.ClientHelper;
|
import org.briarproject.api.clients.ClientHelper;
|
||||||
import org.briarproject.api.data.BdfList;
|
import org.briarproject.api.data.BdfList;
|
||||||
|
import org.briarproject.api.identity.Author;
|
||||||
import org.briarproject.api.identity.LocalAuthor;
|
import org.briarproject.api.identity.LocalAuthor;
|
||||||
|
import org.briarproject.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.api.privategroup.GroupMessage;
|
import org.briarproject.api.privategroup.GroupMessage;
|
||||||
import org.briarproject.api.privategroup.GroupMessageFactory;
|
import org.briarproject.api.privategroup.GroupMessageFactory;
|
||||||
import org.briarproject.api.sync.GroupId;
|
import org.briarproject.api.sync.GroupId;
|
||||||
import org.briarproject.api.sync.Message;
|
import org.briarproject.api.sync.Message;
|
||||||
import org.briarproject.api.sync.MessageId;
|
import org.briarproject.api.sync.MessageId;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.security.GeneralSecurityException;
|
import java.security.GeneralSecurityException;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
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 {
|
class GroupMessageFactoryImpl implements GroupMessageFactory {
|
||||||
|
|
||||||
private final ClientHelper clientHelper;
|
private final ClientHelper clientHelper;
|
||||||
@@ -24,20 +31,78 @@ class GroupMessageFactoryImpl implements GroupMessageFactory {
|
|||||||
this.clientHelper = clientHelper;
|
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
|
@Override
|
||||||
public GroupMessage createGroupMessage(GroupId groupId, long timestamp,
|
public GroupMessage createGroupMessage(GroupId groupId, long timestamp,
|
||||||
MessageId parent, LocalAuthor author, String body)
|
@Nullable MessageId parentId, LocalAuthor author, String content,
|
||||||
throws FormatException, GeneralSecurityException {
|
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
|
// Compose the message
|
||||||
byte[] sig = clientHelper.sign(new BdfList(), author.getPrivateKey());
|
BdfList body =
|
||||||
|
BdfList.of(POST.getInt(), author.getName(),
|
||||||
|
author.getPublicKey(), parentId, previousMsgId,
|
||||||
|
content, signature);
|
||||||
|
Message m = clientHelper.createMessage(groupId, timestamp, body);
|
||||||
|
|
||||||
// Compose the message
|
return new GroupMessage(m, parentId, author);
|
||||||
Message m =
|
} catch (GeneralSecurityException e) {
|
||||||
clientHelper.createMessage(groupId, timestamp, new BdfList());
|
throw new RuntimeException(e);
|
||||||
|
} catch (FormatException e) {
|
||||||
return new GroupMessage(m, parent, author);
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user