From 2a13ff3268afec8e0dc864bc967462ff4efcb0ec Mon Sep 17 00:00:00 2001 From: John Lewis Date: Tue, 28 Nov 2017 14:23:31 -0700 Subject: [PATCH] Fix issues with decryption inside of DisplayRecord. Remove mods. --- src/io/forsta/ccsm/ForstaPreferences.java | 4 ++-- src/io/forsta/ccsm/api/model/ForstaJWT.java | 12 +++--------- .../ccsm/messaging/ForstaMessageManager.java | 2 +- .../forsta/securesms/ConversationFragment.java | 10 ++++++---- src/io/forsta/securesms/ConversationItem.java | 15 +++++++++++---- .../forsta/securesms/ConversationListItem.java | 3 ++- .../database/model/DisplayRecord.java | 18 ------------------ .../database/model/MediaMmsMessageRecord.java | 4 +++- .../securesms/database/model/ThreadRecord.java | 3 +-- .../notifications/MessageNotifier.java | 4 +++- 10 files changed, 32 insertions(+), 43 deletions(-) diff --git a/src/io/forsta/ccsm/ForstaPreferences.java b/src/io/forsta/ccsm/ForstaPreferences.java index f2609257a..8a63d3403 100644 --- a/src/io/forsta/ccsm/ForstaPreferences.java +++ b/src/io/forsta/ccsm/ForstaPreferences.java @@ -56,7 +56,7 @@ public static boolean isRegisteredForsta(Context context) { public static void setRegisteredForsta(Context context, String value) { setStringPreference(context, API_KEY, value); ForstaJWT jwt = new ForstaJWT(value); - TextSecurePreferences.setLocalNumber(context, jwt.getUserInfo().uid); + TextSecurePreferences.setLocalNumber(context, jwt.getUid()); } public static String getRegisteredKey(Context context) { @@ -72,7 +72,7 @@ public static Date getTokenExpireDate(Context context) { public static String getUserId(Context context) { String token = getStringPreference(context, API_KEY); ForstaJWT jwt = new ForstaJWT(token); - return jwt.getUserInfo().uid; + return jwt.getUid(); } public static void setCCSMDebug(Context context, boolean value) { diff --git a/src/io/forsta/ccsm/api/model/ForstaJWT.java b/src/io/forsta/ccsm/api/model/ForstaJWT.java index 0ba113dc4..3eb20585e 100644 --- a/src/io/forsta/ccsm/api/model/ForstaJWT.java +++ b/src/io/forsta/ccsm/api/model/ForstaJWT.java @@ -59,24 +59,18 @@ public Date getExpireDate() { return expireDate; } - public ForstaUser getUserInfo() { - ForstaUser user = new ForstaUser(); + public String getUid() { String payload = getPayload(); try { byte[] payloadBytes = Base64.decodeWithoutPadding(payload); String payloadString = new String(payloadBytes, "UTF-8"); JSONObject obj = new JSONObject(payloadString); - user.org_id = obj.getString("org_id"); - user.uid = obj.getString("user_id"); - user.email = obj.getString("email"); - user.slug = user.username = obj.getString("username"); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); + return obj.getString("user_id"); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } - return user; + return null; } } diff --git a/src/io/forsta/ccsm/messaging/ForstaMessageManager.java b/src/io/forsta/ccsm/messaging/ForstaMessageManager.java index 9d909edf4..f48df9539 100644 --- a/src/io/forsta/ccsm/messaging/ForstaMessageManager.java +++ b/src/io/forsta/ccsm/messaging/ForstaMessageManager.java @@ -99,7 +99,7 @@ public static ForstaMessage fromJsonString(String messageBody) { } catch (InvalidMessagePayloadException e) { Log.e(TAG, "Invalid message payload: " + e.getMessage()); Log.e(TAG, messageBody); - forstaMessage.setTextBody("Invalid message format"); + forstaMessage.setTextBody("Invalid message body:" + messageBody); } return forstaMessage; } diff --git a/src/io/forsta/securesms/ConversationFragment.java b/src/io/forsta/securesms/ConversationFragment.java index a32f4a3cc..051648ba8 100644 --- a/src/io/forsta/securesms/ConversationFragment.java +++ b/src/io/forsta/securesms/ConversationFragment.java @@ -46,6 +46,8 @@ import android.view.Window; import android.widget.Toast; +import io.forsta.ccsm.api.model.ForstaMessage; +import io.forsta.ccsm.messaging.ForstaMessageManager; import io.forsta.securesms.crypto.MasterSecret; import io.forsta.securesms.database.DatabaseFactory; import io.forsta.securesms.database.MmsSmsDatabase; @@ -324,11 +326,11 @@ private void handleDisplayDetails(MessageRecord message) { } private String getMessageBody(MessageRecord messageRecord) { - if (messageRecord.hasHtmlBody()) { - return messageRecord.getForstaHtmlBody().toString(); - } else { - return messageRecord.getForstaPlainTextBody(); + ForstaMessage forstaMessage = ForstaMessageManager.fromJsonString(messageRecord.getDisplayBody().toString()); + if (!TextUtils.isEmpty(forstaMessage.getHtmlBody())) { + return forstaMessage.getHtmlBody().toString(); } + return forstaMessage.getTextBody(); } private void handleForwardMessage(MessageRecord message) { diff --git a/src/io/forsta/securesms/ConversationItem.java b/src/io/forsta/securesms/ConversationItem.java index b599da4cf..d39317cb8 100644 --- a/src/io/forsta/securesms/ConversationItem.java +++ b/src/io/forsta/securesms/ConversationItem.java @@ -307,10 +307,17 @@ private void setBodyText(MessageRecord messageRecord) { if (isCaptionlessMms(messageRecord)) { bodyText.setVisibility(View.GONE); } else { - if (messageRecord.hasHtmlBody()) { - bodyText.setText(messageRecord.getForstaHtmlBody()); - } else { - bodyText.setText(messageRecord.getForstaPlainTextBody()); + + try { + ForstaMessage forstaMessage = ForstaMessageManager.fromMessagBodyString(messageRecord.getDisplayBody().toString()); + if (!TextUtils.isEmpty(forstaMessage.getHtmlBody())) { + bodyText.setText(forstaMessage.getHtmlBody()); + } else { + bodyText.setText(forstaMessage.getTextBody()); + } + } catch (InvalidMessagePayloadException e) { + Log.w(TAG, "Invalid message payload in conversation: " + e.getMessage()); + bodyText.setText("Invalid message format."); } bodyText.setVisibility(View.VISIBLE); } diff --git a/src/io/forsta/securesms/ConversationListItem.java b/src/io/forsta/securesms/ConversationListItem.java index 2eeca7045..ca0500d82 100644 --- a/src/io/forsta/securesms/ConversationListItem.java +++ b/src/io/forsta/securesms/ConversationListItem.java @@ -141,7 +141,8 @@ public void bind(@NonNull final MasterSecret masterSecret, @NonNull ThreadRecord this.forstaThreadTitle = thread.getTitle(); this.threadColor = thread.getColor(); - this.subjectView.setText(thread.getForstaPlainTextBody()); + ForstaMessage forstaMessage = ForstaMessageManager.fromJsonString(thread.getDisplayBody().toString()); + subjectView.setText(forstaMessage.getTextBody()); this.subjectView.setTypeface(read ? LIGHT_TYPEFACE : BOLD_TYPEFACE); if (thread.getDate() > 0) { diff --git a/src/io/forsta/securesms/database/model/DisplayRecord.java b/src/io/forsta/securesms/database/model/DisplayRecord.java index 1de4e8f5c..5fce24394 100644 --- a/src/io/forsta/securesms/database/model/DisplayRecord.java +++ b/src/io/forsta/securesms/database/model/DisplayRecord.java @@ -51,7 +51,6 @@ public abstract class DisplayRecord { private final Body body; private final int deliveryStatus; private final int receiptCount; - public final ForstaMessage forstaMessagePayload; public DisplayRecord(Context context, Body body, Recipients recipients, long dateSent, long dateReceived, long threadId, int deliveryStatus, int receiptCount, long type) @@ -65,7 +64,6 @@ public DisplayRecord(Context context, Body body, Recipients recipients, long dat this.body = body; this.receiptCount = receiptCount; this.deliveryStatus = deliveryStatus; - this.forstaMessagePayload = ForstaMessageManager.fromJsonString(body.getBody()); } public Body getBody() { @@ -89,22 +87,6 @@ public boolean isOutgoing() { public abstract SpannableString getDisplayBody(); - public Spanned getForstaHtmlBody() { - return forstaMessagePayload.getHtmlBody(); - } - - public String getForstaPlainTextBody() { - return forstaMessagePayload.getTextBody(); - } - - public boolean hasHtmlBody() { - return forstaMessagePayload.hasHtmlBody(); - } - - public List getForstaMessageAttachments() { - return forstaMessagePayload.getAttachments(); - } - public Recipients getRecipients() { return recipients; } diff --git a/src/io/forsta/securesms/database/model/MediaMmsMessageRecord.java b/src/io/forsta/securesms/database/model/MediaMmsMessageRecord.java index bbef3e781..4407c348e 100644 --- a/src/io/forsta/securesms/database/model/MediaMmsMessageRecord.java +++ b/src/io/forsta/securesms/database/model/MediaMmsMessageRecord.java @@ -22,6 +22,7 @@ import android.text.TextUtils; import io.forsta.ccsm.api.model.ForstaMessage; +import io.forsta.ccsm.messaging.ForstaMessageManager; import io.forsta.securesms.R; import io.forsta.securesms.database.MmsDatabase; import io.forsta.securesms.database.documents.IdentityKeyMismatch; @@ -125,7 +126,8 @@ public SpannableString getDisplayBody() { public String getDocumentAttachmentFileName() { DocumentSlide documentSlide = getSlideDeck().getDocumentSlide(); String fileName = documentSlide.getFileName().or(context.getString(R.string.DocumentView_unknown_file)); - for (ForstaMessage.ForstaAttachment attachment : getForstaMessageAttachments()) { + ForstaMessage forstaMessage = ForstaMessageManager.fromJsonString(getDisplayBody().toString()); + for (ForstaMessage.ForstaAttachment attachment : forstaMessage.getAttachments()) { if (documentSlide.getContentType().equals(attachment.getType())) { fileName = !TextUtils.isEmpty(attachment.getName()) ? attachment.getName() : fileName; break; diff --git a/src/io/forsta/securesms/database/model/ThreadRecord.java b/src/io/forsta/securesms/database/model/ThreadRecord.java index 7ef102da6..07ecf42f4 100644 --- a/src/io/forsta/securesms/database/model/ThreadRecord.java +++ b/src/io/forsta/securesms/database/model/ThreadRecord.java @@ -98,8 +98,7 @@ public SpannableString getDisplayBody() { } else if (MmsSmsColumns.Types.isLegacyType(type)) { return emphasisAdded(context.getString(R.string.MessageRecord_message_encrypted_with_a_legacy_protocol_version_that_is_no_longer_supported)); } else if (MmsSmsColumns.Types.isDraftMessageType(type)) { - String draftText = context.getString(R.string.ThreadRecord_draft); - return emphasisAdded(draftText + " " + getBody().getBody(), 0, draftText.length()); + return new SpannableString(getBody().getBody()); } else if (SmsDatabase.Types.isOutgoingCall(type)) { return emphasisAdded(context.getString(io.forsta.securesms.R.string.ThreadRecord_called)); } else if (SmsDatabase.Types.isIncomingCall(type)) { diff --git a/src/io/forsta/securesms/notifications/MessageNotifier.java b/src/io/forsta/securesms/notifications/MessageNotifier.java index 4637e64ed..645f6124a 100644 --- a/src/io/forsta/securesms/notifications/MessageNotifier.java +++ b/src/io/forsta/securesms/notifications/MessageNotifier.java @@ -374,7 +374,9 @@ private static NotificationState constructNotificationState(@NonNull Context co Recipient recipient = record.getIndividualRecipient(); Recipients recipients = record.getRecipients(); long threadId = record.getThreadId(); - CharSequence body = record.getForstaPlainTextBody(); + CharSequence body = record.getDisplayBody(); + ForstaMessage forstaMessage = ForstaMessageManager.fromJsonString(body.toString()); + body = forstaMessage.getTextBody(); Recipients threadRecipients = null; SlideDeck slideDeck = null; long timestamp = record.getTimestamp();