Skip to content

Commit

Permalink
Merge pull request #231 from jlewis13/message_payload_fix
Browse files Browse the repository at this point in the history
Fix issues with decryption inside of DisplayRecord. JWT payload changes.
  • Loading branch information
jlewis13 authored Nov 29, 2017
2 parents 726e32d + 2a13ff3 commit 84d10e8
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 43 deletions.
4 changes: 2 additions & 2 deletions src/io/forsta/ccsm/ForstaPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
12 changes: 3 additions & 9 deletions src/io/forsta/ccsm/api/model/ForstaJWT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion src/io/forsta/ccsm/messaging/ForstaMessageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 6 additions & 4 deletions src/io/forsta/securesms/ConversationFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
15 changes: 11 additions & 4 deletions src/io/forsta/securesms/ConversationItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion src/io/forsta/securesms/ConversationListItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
18 changes: 0 additions & 18 deletions src/io/forsta/securesms/database/model/DisplayRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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() {
Expand All @@ -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<ForstaMessage.ForstaAttachment> getForstaMessageAttachments() {
return forstaMessagePayload.getAttachments();
}

public Recipients getRecipients() {
return recipients;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/io/forsta/securesms/database/model/ThreadRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
4 changes: 3 additions & 1 deletion src/io/forsta/securesms/notifications/MessageNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 84d10e8

Please sign in to comment.