Use ClientHelper in ForumPostFactoryImpl.

This commit is contained in:
akwizgran
2016-03-01 10:48:03 +00:00
parent cc7ffee28d
commit d342594313
3 changed files with 24 additions and 62 deletions

View File

@@ -23,6 +23,7 @@ import org.briarproject.android.identity.LocalAuthorItemComparator;
import org.briarproject.android.identity.LocalAuthorSpinnerAdapter; import org.briarproject.android.identity.LocalAuthorSpinnerAdapter;
import org.briarproject.android.util.CommonLayoutParams; import org.briarproject.android.util.CommonLayoutParams;
import org.briarproject.android.util.LayoutUtils; import org.briarproject.android.util.LayoutUtils;
import org.briarproject.api.FormatException;
import org.briarproject.api.crypto.CryptoComponent; import org.briarproject.api.crypto.CryptoComponent;
import org.briarproject.api.crypto.CryptoExecutor; import org.briarproject.api.crypto.CryptoExecutor;
import org.briarproject.api.crypto.KeyParser; import org.briarproject.api.crypto.KeyParser;
@@ -39,7 +40,6 @@ import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.MessageId; import org.briarproject.api.sync.MessageId;
import org.briarproject.util.StringUtils; import org.briarproject.util.StringUtils;
import java.io.IOException;
import java.security.GeneralSecurityException; import java.security.GeneralSecurityException;
import java.util.Collection; import java.util.Collection;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
@@ -281,7 +281,7 @@ implements OnItemSelectedListener, OnClickListener {
} }
} catch (GeneralSecurityException e) { } catch (GeneralSecurityException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} catch (IOException e) { } catch (FormatException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
storePost(p); storePost(p);

View File

@@ -1,21 +1,21 @@
package org.briarproject.api.forum; package org.briarproject.api.forum;
import org.briarproject.api.FormatException;
import org.briarproject.api.crypto.PrivateKey; import org.briarproject.api.crypto.PrivateKey;
import org.briarproject.api.identity.Author; import org.briarproject.api.identity.Author;
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 java.io.IOException;
import java.security.GeneralSecurityException; import java.security.GeneralSecurityException;
public interface ForumPostFactory { public interface ForumPostFactory {
ForumPost createAnonymousPost(GroupId groupId, long timestamp, ForumPost createAnonymousPost(GroupId groupId, long timestamp,
MessageId parent, String contentType, byte[] body) MessageId parent, String contentType, byte[] body)
throws IOException, GeneralSecurityException; throws FormatException;
ForumPost createPseudonymousPost(GroupId groupId, long timestamp, ForumPost createPseudonymousPost(GroupId groupId, long timestamp,
MessageId parent, Author author, String contentType, byte[] body, MessageId parent, Author author, String contentType, byte[] body,
PrivateKey privateKey) throws IOException, PrivateKey privateKey) throws FormatException,
GeneralSecurityException; GeneralSecurityException;
} }

View File

@@ -1,21 +1,19 @@
package org.briarproject.forum; 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.CryptoComponent;
import org.briarproject.api.crypto.PrivateKey; import org.briarproject.api.crypto.PrivateKey;
import org.briarproject.api.crypto.Signature; import org.briarproject.api.crypto.Signature;
import org.briarproject.api.data.BdfWriter; import org.briarproject.api.data.BdfList;
import org.briarproject.api.data.BdfWriterFactory;
import org.briarproject.api.forum.ForumPost; import org.briarproject.api.forum.ForumPost;
import org.briarproject.api.forum.ForumPostFactory; import org.briarproject.api.forum.ForumPostFactory;
import org.briarproject.api.identity.Author; import org.briarproject.api.identity.Author;
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.MessageFactory;
import org.briarproject.api.sync.MessageId; import org.briarproject.api.sync.MessageId;
import org.briarproject.util.StringUtils; import org.briarproject.util.StringUtils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.GeneralSecurityException; import java.security.GeneralSecurityException;
import javax.inject.Inject; import javax.inject.Inject;
@@ -26,46 +24,33 @@ import static org.briarproject.api.forum.ForumConstants.MAX_FORUM_POST_BODY_LENG
class ForumPostFactoryImpl implements ForumPostFactory { class ForumPostFactoryImpl implements ForumPostFactory {
private final CryptoComponent crypto; private final CryptoComponent crypto;
private final MessageFactory messageFactory; private final ClientHelper clientHelper;
private final BdfWriterFactory bdfWriterFactory;
@Inject @Inject
ForumPostFactoryImpl(CryptoComponent crypto, MessageFactory messageFactory, ForumPostFactoryImpl(CryptoComponent crypto, ClientHelper clientHelper) {
BdfWriterFactory bdfWriterFactory) {
this.crypto = crypto; this.crypto = crypto;
this.messageFactory = messageFactory; this.clientHelper = clientHelper;
this.bdfWriterFactory = bdfWriterFactory;
} }
@Override @Override
public ForumPost createAnonymousPost(GroupId groupId, long timestamp, public ForumPost createAnonymousPost(GroupId groupId, long timestamp,
MessageId parent, String contentType, byte[] body) MessageId parent, String contentType, byte[] body)
throws IOException, GeneralSecurityException { throws FormatException {
// Validate the arguments // Validate the arguments
if (StringUtils.toUtf8(contentType).length > MAX_CONTENT_TYPE_LENGTH) if (StringUtils.toUtf8(contentType).length > MAX_CONTENT_TYPE_LENGTH)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
if (body.length > MAX_FORUM_POST_BODY_LENGTH) if (body.length > MAX_FORUM_POST_BODY_LENGTH)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
// Serialise the message to a buffer // Serialise the message
ByteArrayOutputStream out = new ByteArrayOutputStream(); BdfList message = BdfList.of(parent, null, contentType, body, null);
BdfWriter w = bdfWriterFactory.createWriter(out); Message m = clientHelper.createMessage(groupId, timestamp, message);
w.writeListStart();
if (parent == null) w.writeNull();
else w.writeRaw(parent.getBytes());
w.writeNull(); // No author
w.writeString(contentType);
w.writeRaw(body);
w.writeNull(); // No signature
w.writeListEnd();
Message m = messageFactory.createMessage(groupId, timestamp,
out.toByteArray());
return new ForumPost(m, parent, null, contentType); return new ForumPost(m, parent, null, contentType);
} }
@Override @Override
public ForumPost createPseudonymousPost(GroupId groupId, long timestamp, public ForumPost createPseudonymousPost(GroupId groupId, long timestamp,
MessageId parent, Author author, String contentType, byte[] body, MessageId parent, Author author, String contentType, byte[] body,
PrivateKey privateKey) throws IOException, PrivateKey privateKey) throws FormatException,
GeneralSecurityException { GeneralSecurityException {
// Validate the arguments // Validate the arguments
if (StringUtils.toUtf8(contentType).length > MAX_CONTENT_TYPE_LENGTH) if (StringUtils.toUtf8(contentType).length > MAX_CONTENT_TYPE_LENGTH)
@@ -73,42 +58,19 @@ class ForumPostFactoryImpl implements ForumPostFactory {
if (body.length > MAX_FORUM_POST_BODY_LENGTH) if (body.length > MAX_FORUM_POST_BODY_LENGTH)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
// Serialise the data to be signed // Serialise the data to be signed
ByteArrayOutputStream out = new ByteArrayOutputStream(); BdfList authorList = BdfList.of(author.getName(),
BdfWriter w = bdfWriterFactory.createWriter(out); author.getPublicKey());
w.writeListStart(); BdfList signed = BdfList.of(groupId, timestamp, parent, authorList,
w.writeRaw(groupId.getBytes()); contentType, body);
w.writeLong(timestamp);
if (parent == null) w.writeNull();
else w.writeRaw(parent.getBytes());
writeAuthor(w, author);
w.writeString(contentType);
w.writeRaw(body);
w.writeListEnd();
// Generate the signature // Generate the signature
Signature signature = crypto.getSignature(); Signature signature = crypto.getSignature();
signature.initSign(privateKey); signature.initSign(privateKey);
signature.update(out.toByteArray()); signature.update(clientHelper.toByteArray(signed));
byte[] sig = signature.sign(); byte[] sig = signature.sign();
// Serialise the signed message // Serialise the signed message
out.reset(); BdfList message = BdfList.of(parent, authorList, contentType, body,
w = bdfWriterFactory.createWriter(out); sig);
w.writeListStart(); Message m = clientHelper.createMessage(groupId, timestamp, message);
if (parent == null) w.writeNull();
else w.writeRaw(parent.getBytes());
writeAuthor(w, author);
w.writeString(contentType);
w.writeRaw(body);
w.writeRaw(sig);
w.writeListEnd();
Message m = messageFactory.createMessage(groupId, timestamp,
out.toByteArray());
return new ForumPost(m, parent, author, contentType); return new ForumPost(m, parent, author, contentType);
} }
private void writeAuthor(BdfWriter w, Author a) throws IOException {
w.writeListStart();
w.writeString(a.getName());
w.writeRaw(a.getPublicKey());
w.writeListEnd();
}
} }