Access all RecipientDatabase settings directly from Recipient

// FREEBIE
This commit is contained in:
Moxie Marlinspike
2017-08-22 10:44:04 -07:00
parent d1790dfe17
commit f17af19d09
29 changed files with 504 additions and 390 deletions

View File

@@ -803,19 +803,14 @@ public class PushDecryptJob extends ContextJob {
private void handleProfileKey(@NonNull SignalServiceEnvelope envelope,
@NonNull SignalServiceDataMessage message)
{
RecipientDatabase database = DatabaseFactory.getRecipientDatabase(context);
Address sourceAddress = Address.fromExternal(context, envelope.getSource());
Optional<RecipientSettings> settings = database.getRecipientSettings(sourceAddress);
RecipientDatabase database = DatabaseFactory.getRecipientDatabase(context);
Address sourceAddress = Address.fromExternal(context, envelope.getSource());
Recipient recipient = Recipient.from(context, sourceAddress, false);
if (!settings.isPresent() || settings.get().getProfileKey() == null ||
!MessageDigest.isEqual(message.getProfileKey().get(), settings.get().getProfileKey()))
{
database.setProfileKey(sourceAddress, message.getProfileKey().get());
Recipient recipient = Recipient.from(context, sourceAddress, true);
if (recipient.getProfileKey() == null || MessageDigest.isEqual(recipient.getProfileKey(), message.getProfileKey().get())) {
database.setProfileKey(recipient, message.getProfileKey().get());
ApplicationContext.getInstance(context).getJobManager().add(new RetrieveProfileJob(context, recipient));
}
}
private Optional<InsertResult> insertPlaceholder(@NonNull SignalServiceEnvelope envelope) {

View File

@@ -114,7 +114,7 @@ public class PushMediaSendJob extends PushSendJob implements InjectableType {
MediaConstraints mediaConstraints = MediaConstraints.getPushMediaConstraints();
List<Attachment> scaledAttachments = scaleAttachments(masterSecret, mediaConstraints, message.getAttachments());
List<SignalServiceAttachment> attachmentStreams = getAttachmentsFor(masterSecret, scaledAttachments);
Optional<byte[]> profileKey = getProfileKey(message.getRecipient().getAddress());
Optional<byte[]> profileKey = getProfileKey(message.getRecipient());
SignalServiceDataMessage mediaMessage = SignalServiceDataMessage.newBuilder()
.withBody(message.getBody())
.withAttachments(attachmentStreams)

View File

@@ -1,12 +1,14 @@
package org.thoughtcrime.securesms.jobs;
import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;
import org.thoughtcrime.securesms.ApplicationContext;
import org.thoughtcrime.securesms.database.Address;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.MessagingDatabase.SyncMessageId;
import org.thoughtcrime.securesms.database.RecipientDatabase;
import org.thoughtcrime.securesms.database.RecipientDatabase.RecipientSettings;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.service.KeyCachingService;
@@ -27,11 +29,11 @@ public abstract class PushReceivedJob extends ContextJob {
}
public void handle(SignalServiceEnvelope envelope, boolean sendExplicitReceipt) {
Address source = Address.fromExternal(context, envelope.getSource());
Address source = Address.fromExternal(context, envelope.getSource());
Recipient recipient = Recipient.from(context, source, false);
if (!isActiveNumber(context, source)) {
DatabaseFactory.getRecipientDatabase(context).setRegistered(Util.asList(source), new LinkedList<>());
Recipient recipient = Recipient.from(context, source, false);
if (!isActiveNumber(recipient)) {
DatabaseFactory.getRecipientDatabase(context).setRegistered(recipient, RecipientDatabase.RegisteredState.REGISTERED);
ApplicationContext.getInstance(context).getJobManager().add(new DirectoryRefreshJob(context, KeyCachingService.getMasterSecret(context), recipient));
}
@@ -68,9 +70,8 @@ public abstract class PushReceivedJob extends ContextJob {
envelope.getTimestamp()));
}
private boolean isActiveNumber(Context context, Address address) {
Optional<RecipientSettings> settings = DatabaseFactory.getRecipientDatabase(context).getRecipientSettings(address);
return settings.isPresent() && settings.get().isRegistered();
private boolean isActiveNumber(@NonNull Recipient recipient) {
return recipient.resolve().getRegistered() == RecipientDatabase.RegisteredState.REGISTERED;
}

View File

@@ -1,6 +1,7 @@
package org.thoughtcrime.securesms.jobs;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
@@ -64,17 +65,11 @@ public abstract class PushSendJob extends SendJob {
onPushSend(masterSecret);
}
protected Optional<byte[]> getProfileKey(Address address) {
protected Optional<byte[]> getProfileKey(@NonNull Recipient recipient) {
try {
Optional<RecipientSettings> recipientsPreferences = DatabaseFactory.getRecipientDatabase(context)
.getRecipientSettings(address);
if (!recipientsPreferences.isPresent()) return Optional.absent();
boolean isSystemContact = !TextUtils.isEmpty(recipientsPreferences.get().getSystemDisplayName());
boolean isApproved = recipientsPreferences.get().isProfileSharing();
if (!isSystemContact & !isApproved) return Optional.absent();
if (!recipient.resolve().isSystemContact() && !recipient.resolve().isProfileSharing()) {
return Optional.absent();
}
String profileKey = TextSecurePreferences.getProfileKey(context);

View File

@@ -102,7 +102,7 @@ public class PushTextSendJob extends PushSendJob implements InjectableType {
try {
SignalServiceAddress address = getPushAddress(message.getIndividualRecipient().getAddress());
SignalServiceMessageSender messageSender = messageSenderFactory.create();
Optional<byte[]> profileKey = getProfileKey(message.getIndividualRecipient().getAddress());
Optional<byte[]> profileKey = getProfileKey(message.getIndividualRecipient());
SignalServiceDataMessage textSecureMessage = SignalServiceDataMessage.newBuilder()
.withTimestamp(message.getDateSent())
.withBody(message.getBody().getBody())

View File

@@ -5,16 +5,16 @@ import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.contacts.avatars.ContactPhotoFactory;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.RecipientDatabase;
import org.thoughtcrime.securesms.database.RecipientDatabase.RecipientSettings;
import org.thoughtcrime.securesms.dependencies.InjectableType;
import org.thoughtcrime.securesms.profiles.AvatarHelper;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.jobqueue.JobParameters;
import org.whispersystems.jobqueue.requirements.NetworkRequirement;
import org.whispersystems.libsignal.util.guava.Optional;
import org.whispersystems.signalservice.api.SignalServiceMessageReceiver;
import org.whispersystems.signalservice.api.push.exceptions.PushNetworkException;
@@ -51,20 +51,15 @@ public class RetrieveProfileAvatarJob extends ContextJob implements InjectableTy
@Override
public void onRun() throws IOException {
RecipientDatabase database = DatabaseFactory.getRecipientDatabase(context);
Optional<RecipientSettings> recipientSettings = database.getRecipientSettings(recipient.getAddress());
RecipientDatabase database = DatabaseFactory.getRecipientDatabase(context);
byte[] profileKey = recipient.resolve().getProfileKey();
if (!recipientSettings.isPresent()) {
Log.w(TAG, "Recipient preference row is gone!");
return;
}
if (recipientSettings.get().getProfileKey() == null) {
if (profileKey == null) {
Log.w(TAG, "Recipient profile key is gone!");
return;
}
if (Util.equals(profileAvatar, recipientSettings.get().getProfileAvatar())) {
if (Util.equals(profileAvatar, recipient.resolve().getProfileAvatar())) {
Log.w(TAG, "Already retrieved profile avatar: " + profileAvatar);
return;
}
@@ -72,13 +67,14 @@ public class RetrieveProfileAvatarJob extends ContextJob implements InjectableTy
if (TextUtils.isEmpty(profileAvatar)) {
Log.w(TAG, "Removing profile avatar for: " + recipient.getAddress().serialize());
AvatarHelper.delete(context, recipient.getAddress());
database.setProfileAvatar(recipient, profileAvatar);
return;
}
File downloadDestination = File.createTempFile("avatar", "jpg", context.getCacheDir());
try {
InputStream avatarStream = receiver.retrieveProfileAvatar(profileAvatar, downloadDestination, recipientSettings.get().getProfileKey(), MAX_PROFILE_SIZE_BYTES);
InputStream avatarStream = receiver.retrieveProfileAvatar(profileAvatar, downloadDestination, profileKey, MAX_PROFILE_SIZE_BYTES);
File decryptDestination = File.createTempFile("avatar", "jpg", context.getCacheDir());
Util.copy(avatarStream, new FileOutputStream(decryptDestination));
@@ -87,8 +83,11 @@ public class RetrieveProfileAvatarJob extends ContextJob implements InjectableTy
if (downloadDestination != null) downloadDestination.delete();
}
database.setProfileAvatar(recipient.getAddress(), profileAvatar);
Recipient.clearCache(context);
database.setProfileAvatar(recipient, profileAvatar);
if (recipient.resolve().getContactPhoto().isGenerated()) {
recipient.setContactPhoto(ContactPhotoFactory.getSignalAvatarContactPhoto(context, recipient.getAddress(), recipient.getName(), context.getResources().getDimensionPixelSize(R.dimen.contact_photo_target_size)));
}
}
@Override

View File

@@ -71,13 +71,12 @@ public class RetrieveProfileJob extends ContextJob implements InjectableType {
private void handleIndividualRecipient(Recipient recipient)
throws IOException, InvalidKeyException, InvalidNumberException
{
String number = recipient.getAddress().toPhoneString();
SignalServiceProfile profile = retrieveProfile(number);
Optional<RecipientSettings> recipientSettings = DatabaseFactory.getRecipientDatabase(context).getRecipientSettings(recipient.getAddress());
String number = recipient.getAddress().toPhoneString();
SignalServiceProfile profile = retrieveProfile(number);
setIdentityKey(recipient, profile.getIdentityKey());
setProfileName(recipient, recipientSettings, profile.getName());
setProfileAvatar(recipient, recipientSettings, profile.getAvatar());
setProfileName(recipient, profile.getName());
setProfileAvatar(recipient, profile.getAvatar());
}
private void handleGroupRecipient(Recipient group)
@@ -127,32 +126,30 @@ public class RetrieveProfileJob extends ContextJob implements InjectableType {
}
}
private void setProfileName(Recipient recipient, Optional<RecipientSettings> recipientPreferences, String profileName) {
private void setProfileName(Recipient recipient, String profileName) {
try {
if (!recipientPreferences.isPresent()) return;
if (recipientPreferences.get().getProfileKey() == null) return;
byte[] profileKey = recipient.getProfileKey();
if (profileKey == null) return;
String plaintextProfileName = null;
if (profileName != null) {
ProfileCipher profileCipher = new ProfileCipher(recipientPreferences.get().getProfileKey());
ProfileCipher profileCipher = new ProfileCipher(profileKey);
plaintextProfileName = new String(profileCipher.decryptName(Base64.decode(profileName)));
}
if (!Util.equals(plaintextProfileName, recipientPreferences.get().getProfileName())) {
DatabaseFactory.getRecipientDatabase(context).setProfileName(recipient.getAddress(), plaintextProfileName);
Recipient.clearCache(context);
if (!Util.equals(plaintextProfileName, recipient.getProfileName())) {
DatabaseFactory.getRecipientDatabase(context).setProfileName(recipient, plaintextProfileName);
}
} catch (ProfileCipher.InvalidCiphertextException | IOException e) {
Log.w(TAG, e);
}
}
private void setProfileAvatar(Recipient recipient, Optional<RecipientSettings> recipientPreferences, String profileAvatar) {
if (!recipientPreferences.isPresent()) return;
if (recipientPreferences.get().getProfileKey() == null) return;
private void setProfileAvatar(Recipient recipient, String profileAvatar) {
if (recipient.getProfileKey() == null) return;
if (!Util.equals(profileAvatar, recipientPreferences.get().getProfileAvatar())) {
if (!Util.equals(profileAvatar, recipient.getProfileAvatar())) {
ApplicationContext.getInstance(context)
.getJobManager()
.add(new RetrieveProfileAvatarJob(context, recipient, profileAvatar));