Remove forum content type and move bodies to string

Also removes support for anonymous forum posts.
Closes #698
This commit is contained in:
Torsten Grote
2016-10-27 11:24:33 -02:00
parent c0aa255bb6
commit 5ce8b1978d
10 changed files with 81 additions and 124 deletions

View File

@@ -131,7 +131,7 @@ class ForumManagerImpl extends BdfIncomingMessageHook implements ForumManager {
ForumPost p;
try {
p = forumPostFactory
.createPseudonymousPost(groupId, timestamp, parentId,
.createPost(groupId, timestamp, parentId,
author, body);
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
@@ -213,11 +213,11 @@ class ForumManagerImpl extends BdfIncomingMessageHook implements ForumManager {
}
@Override
public byte[] getPostBody(MessageId m) throws DbException {
public String getPostBody(MessageId m) throws DbException {
try {
// Parent ID, author, content type, forum post body, signature
// Parent ID, author, forum post body, signature
BdfList message = clientHelper.getMessageAsList(m);
return message.getRaw(3);
return message.getString(2);
} catch (FormatException e) {
throw new DbException(e);
}

View File

@@ -40,9 +40,9 @@ public class ForumModule {
}
@Provides
ForumPostFactory provideForumPostFactory(CryptoComponent crypto,
ClientHelper clientHelper) {
return new ForumPostFactoryImpl(crypto, clientHelper);
ForumPostFactory provideForumPostFactory(
ForumPostFactoryImpl forumPostFactory) {
return forumPostFactory;
}
@Provides

View File

@@ -2,10 +2,6 @@ package org.briarproject.forum;
import org.briarproject.api.FormatException;
import org.briarproject.api.clients.ClientHelper;
import org.briarproject.api.crypto.CryptoComponent;
import org.briarproject.api.crypto.KeyParser;
import org.briarproject.api.crypto.PrivateKey;
import org.briarproject.api.crypto.Signature;
import org.briarproject.api.data.BdfList;
import org.briarproject.api.forum.ForumPost;
import org.briarproject.api.forum.ForumPostFactory;
@@ -19,64 +15,35 @@ import java.security.GeneralSecurityException;
import javax.inject.Inject;
import static org.briarproject.api.forum.ForumConstants.MAX_CONTENT_TYPE_LENGTH;
import static org.briarproject.api.forum.ForumConstants.MAX_FORUM_POST_BODY_LENGTH;
class ForumPostFactoryImpl implements ForumPostFactory {
private final CryptoComponent crypto;
private final ClientHelper clientHelper;
@Inject
ForumPostFactoryImpl(CryptoComponent crypto, ClientHelper clientHelper) {
this.crypto = crypto;
ForumPostFactoryImpl(ClientHelper clientHelper) {
this.clientHelper = clientHelper;
}
@Override
public ForumPost createAnonymousPost(GroupId groupId, long timestamp,
MessageId parent, String contentType, byte[] body)
throws FormatException {
// Validate the arguments
if (StringUtils.toUtf8(contentType).length > MAX_CONTENT_TYPE_LENGTH)
throw new IllegalArgumentException();
if (body.length > MAX_FORUM_POST_BODY_LENGTH)
throw new IllegalArgumentException();
// Serialise the message
BdfList message = BdfList.of(parent, null, contentType, body, null);
Message m = clientHelper.createMessage(groupId, timestamp, message);
return new ForumPost(m, parent, null);
}
@Override
public ForumPost createPseudonymousPost(GroupId groupId, long timestamp,
MessageId parent, LocalAuthor author, String bodyStr)
public ForumPost createPost(GroupId groupId, long timestamp,
MessageId parent, LocalAuthor author, String body)
throws FormatException, GeneralSecurityException {
String contentType = "text/plain";
byte[] body = StringUtils.toUtf8(bodyStr);
// Validate the arguments
if (StringUtils.toUtf8(contentType).length > MAX_CONTENT_TYPE_LENGTH)
throw new IllegalArgumentException();
if (body.length > MAX_FORUM_POST_BODY_LENGTH)
if (StringUtils.isTooLong(body, MAX_FORUM_POST_BODY_LENGTH))
throw new IllegalArgumentException();
// Serialise the data to be signed
BdfList authorList = BdfList.of(author.getName(),
author.getPublicKey());
BdfList authorList =
BdfList.of(author.getName(), author.getPublicKey());
BdfList signed = BdfList.of(groupId, timestamp, parent, authorList,
contentType, body);
// Get private key
KeyParser keyParser = crypto.getSignatureKeyParser();
byte[] k = author.getPrivateKey();
PrivateKey privateKey = keyParser.parsePrivateKey(k);
// Generate the signature
Signature signature = crypto.getSignature();
signature.initSign(privateKey);
signature.update(clientHelper.toByteArray(signed));
byte[] sig = signature.sign();
body);
// Sign the data
byte[] sig = clientHelper.sign(signed, author.getPrivateKey());
// Serialise the signed message
BdfList message = BdfList.of(parent, authorList, contentType, body,
sig);
BdfList message = BdfList.of(parent, authorList, body, sig);
Message m = clientHelper.createMessage(groupId, timestamp, message);
return new ForumPost(m, parent, author);
}
}

View File

@@ -20,7 +20,6 @@ import java.security.GeneralSecurityException;
import java.util.Collection;
import java.util.Collections;
import static org.briarproject.api.forum.ForumConstants.MAX_CONTENT_TYPE_LENGTH;
import static org.briarproject.api.forum.ForumConstants.MAX_FORUM_POST_BODY_LENGTH;
import static org.briarproject.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
import static org.briarproject.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
@@ -40,50 +39,38 @@ class ForumPostValidator extends BdfMessageValidator {
protected BdfMessageContext validateMessage(Message m, Group g,
BdfList body) throws InvalidMessageException, FormatException {
// Parent ID, author, content type, forum post body, signature
checkSize(body, 5);
checkSize(body, 4);
// Parent ID is optional
byte[] parent = body.getOptionalRaw(0);
checkLength(parent, UniqueId.LENGTH);
// Author is optional
Author author = null;
BdfList authorList = body.getOptionalList(1);
if (authorList != null) {
// Name, public key
checkSize(authorList, 2);
String name = authorList.getString(0);
checkLength(name, 1, MAX_AUTHOR_NAME_LENGTH);
byte[] publicKey = authorList.getRaw(1);
checkLength(publicKey, 0, MAX_PUBLIC_KEY_LENGTH);
author = authorFactory.createAuthor(name, publicKey);
}
// Content type
String contentType = body.getString(2);
checkLength(contentType, 0, MAX_CONTENT_TYPE_LENGTH);
// Author
BdfList authorList = body.getList(1);
// Name, public key
checkSize(authorList, 2);
String name = authorList.getString(0);
checkLength(name, 1, MAX_AUTHOR_NAME_LENGTH);
byte[] publicKey = authorList.getRaw(1);
checkLength(publicKey, 0, MAX_PUBLIC_KEY_LENGTH);
Author author = authorFactory.createAuthor(name, publicKey);
// Forum post body
byte[] forumPostBody = body.getRaw(3);
String forumPostBody = body.getString(2);
checkLength(forumPostBody, 0, MAX_FORUM_POST_BODY_LENGTH);
// Signature is optional
byte[] sig = body.getOptionalRaw(4);
// Signature
byte[] sig = body.getRaw(3);
checkLength(sig, 0, MAX_SIGNATURE_LENGTH);
// If there's an author there must be a signature and vice versa
if (author != null && sig == null) {
throw new InvalidMessageException("Author without signature");
}
if (author == null && sig != null) {
throw new InvalidMessageException("Signature without author");
}
// Verify the signature, if any
if (author != null) {
// Serialise the data to be verified
BdfList signed = BdfList.of(g.getId(), m.getTimestamp(), parent,
authorList, contentType, forumPostBody);
try {
clientHelper
.verifySignature(sig, author.getPublicKey(), signed);
} catch (GeneralSecurityException e) {
throw new InvalidMessageException(e);
}
// Verify the signature
BdfList signed = BdfList.of(g.getId(), m.getTimestamp(), parent,
authorList, forumPostBody);
try {
clientHelper.verifySignature(sig, author.getPublicKey(), signed);
} catch (GeneralSecurityException e) {
throw new InvalidMessageException(e);
}
// Return the metadata and dependencies
BdfDictionary meta = new BdfDictionary();
Collection<MessageId> dependencies = Collections.emptyList();
@@ -92,14 +79,11 @@ class ForumPostValidator extends BdfMessageValidator {
meta.put("parent", parent);
dependencies = Collections.singletonList(new MessageId(parent));
}
if (author != null) {
BdfDictionary authorMeta = new BdfDictionary();
authorMeta.put("id", author.getId());
authorMeta.put("name", author.getName());
authorMeta.put("publicKey", author.getPublicKey());
meta.put("author", authorMeta);
}
meta.put("contentType", contentType);
BdfDictionary authorMeta = new BdfDictionary();
authorMeta.put("id", author.getId());
authorMeta.put("name", author.getName());
authorMeta.put("publicKey", author.getPublicKey());
meta.put("author", authorMeta);
meta.put("read", false);
return new BdfMessageContext(meta, dependencies);
}