Refactor to remove ProfileKeyProvider and UnidentifiedAccessSenderCertificateProvider

This commit is contained in:
AsamK 2021-10-12 20:48:56 +02:00
parent 15c66684c1
commit e977f38bdd
5 changed files with 38 additions and 53 deletions

View file

@ -176,14 +176,13 @@ public class ManagerImpl implements Manager {
this.attachmentHelper = new AttachmentHelper(dependencies, attachmentStore); this.attachmentHelper = new AttachmentHelper(dependencies, attachmentStore);
this.pinHelper = new PinHelper(dependencies.getKeyBackupService()); this.pinHelper = new PinHelper(dependencies.getKeyBackupService());
final var unidentifiedAccessHelper = new UnidentifiedAccessHelper(account::getProfileKey, final var unidentifiedAccessHelper = new UnidentifiedAccessHelper(account,
account.getProfileStore()::getProfileKey, dependencies,
this::getRecipientProfile, account::getProfileKey,
this::getSenderCertificate); this::getRecipientProfile);
this.profileHelper = new ProfileHelper(account, this.profileHelper = new ProfileHelper(account,
dependencies, dependencies,
avatarStore, avatarStore,
account.getProfileStore()::getProfileKey,
unidentifiedAccessHelper::getAccessFor, unidentifiedAccessHelper::getAccessFor,
this::resolveSignalServiceAddress); this::resolveSignalServiceAddress);
final GroupV2Helper groupV2Helper = new GroupV2Helper(profileHelper::getRecipientProfileKeyCredential, final GroupV2Helper groupV2Helper = new GroupV2Helper(profileHelper::getRecipientProfileKeyCredential,
@ -782,22 +781,6 @@ public class ManagerImpl implements Manager {
} }
} }
private byte[] getSenderCertificate() {
byte[] certificate;
try {
if (account.isPhoneNumberShared()) {
certificate = dependencies.getAccountManager().getSenderCertificate();
} else {
certificate = dependencies.getAccountManager().getSenderCertificateForPhoneNumberPrivacy();
}
} catch (IOException e) {
logger.warn("Failed to get sender certificate, ignoring: {}", e.getMessage());
return null;
}
// TODO cache for a day
return certificate;
}
private RecipientId refreshRegisteredUser(RecipientId recipientId) throws IOException { private RecipientId refreshRegisteredUser(RecipientId recipientId) throws IOException {
final var address = resolveSignalServiceAddress(recipientId); final var address = resolveSignalServiceAddress(recipientId);
if (!address.getNumber().isPresent()) { if (!address.getNumber().isPresent()) {

View file

@ -45,7 +45,6 @@ public final class ProfileHelper {
private final SignalAccount account; private final SignalAccount account;
private final SignalDependencies dependencies; private final SignalDependencies dependencies;
private final AvatarStore avatarStore; private final AvatarStore avatarStore;
private final ProfileKeyProvider profileKeyProvider;
private final UnidentifiedAccessProvider unidentifiedAccessProvider; private final UnidentifiedAccessProvider unidentifiedAccessProvider;
private final SignalServiceAddressResolver addressResolver; private final SignalServiceAddressResolver addressResolver;
@ -53,14 +52,12 @@ public final class ProfileHelper {
final SignalAccount account, final SignalAccount account,
final SignalDependencies dependencies, final SignalDependencies dependencies,
final AvatarStore avatarStore, final AvatarStore avatarStore,
final ProfileKeyProvider profileKeyProvider,
final UnidentifiedAccessProvider unidentifiedAccessProvider, final UnidentifiedAccessProvider unidentifiedAccessProvider,
final SignalServiceAddressResolver addressResolver final SignalServiceAddressResolver addressResolver
) { ) {
this.account = account; this.account = account;
this.dependencies = dependencies; this.dependencies = dependencies;
this.avatarStore = avatarStore; this.avatarStore = avatarStore;
this.profileKeyProvider = profileKeyProvider;
this.unidentifiedAccessProvider = unidentifiedAccessProvider; this.unidentifiedAccessProvider = unidentifiedAccessProvider;
this.addressResolver = addressResolver; this.addressResolver = addressResolver;
} }
@ -296,7 +293,7 @@ public final class ProfileHelper {
RecipientId recipientId, SignalServiceProfile.RequestType requestType RecipientId recipientId, SignalServiceProfile.RequestType requestType
) { ) {
var unidentifiedAccess = getUnidentifiedAccess(recipientId); var unidentifiedAccess = getUnidentifiedAccess(recipientId);
var profileKey = Optional.fromNullable(profileKeyProvider.getProfileKey(recipientId)); var profileKey = Optional.fromNullable(account.getProfileStore().getProfileKey(recipientId));
final var address = addressResolver.resolveSignalServiceAddress(recipientId); final var address = addressResolver.resolveSignalServiceAddress(recipientId);
return retrieveProfile(address, profileKey, unidentifiedAccess, requestType); return retrieveProfile(address, profileKey, unidentifiedAccess, requestType);

View file

@ -1,9 +0,0 @@
package org.asamk.signal.manager.helper;
import org.asamk.signal.manager.storage.recipients.RecipientId;
import org.signal.zkgroup.profiles.ProfileKey;
public interface ProfileKeyProvider {
ProfileKey getProfileKey(RecipientId address);
}

View file

@ -1,11 +1,16 @@
package org.asamk.signal.manager.helper; package org.asamk.signal.manager.helper;
import org.asamk.signal.manager.SignalDependencies;
import org.asamk.signal.manager.storage.SignalAccount;
import org.asamk.signal.manager.storage.recipients.RecipientId; import org.asamk.signal.manager.storage.recipients.RecipientId;
import org.signal.libsignal.metadata.certificate.InvalidCertificateException; import org.signal.libsignal.metadata.certificate.InvalidCertificateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.whispersystems.libsignal.util.guava.Optional; import org.whispersystems.libsignal.util.guava.Optional;
import org.whispersystems.signalservice.api.crypto.UnidentifiedAccess; import org.whispersystems.signalservice.api.crypto.UnidentifiedAccess;
import org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair; import org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair;
import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -13,24 +18,39 @@ import static org.whispersystems.signalservice.internal.util.Util.getSecretBytes
public class UnidentifiedAccessHelper { public class UnidentifiedAccessHelper {
private final static Logger logger = LoggerFactory.getLogger(UnidentifiedAccessHelper.class);
private final SignalAccount account;
private final SignalDependencies dependencies;
private final SelfProfileKeyProvider selfProfileKeyProvider; private final SelfProfileKeyProvider selfProfileKeyProvider;
private final ProfileKeyProvider profileKeyProvider;
private final ProfileProvider profileProvider; private final ProfileProvider profileProvider;
private final UnidentifiedAccessSenderCertificateProvider senderCertificateProvider;
public UnidentifiedAccessHelper( public UnidentifiedAccessHelper(
final SignalAccount account,
final SignalDependencies dependencies,
final SelfProfileKeyProvider selfProfileKeyProvider, final SelfProfileKeyProvider selfProfileKeyProvider,
final ProfileKeyProvider profileKeyProvider, final ProfileProvider profileProvider
final ProfileProvider profileProvider,
final UnidentifiedAccessSenderCertificateProvider senderCertificateProvider
) { ) {
this.account = account;
this.dependencies = dependencies;
this.selfProfileKeyProvider = selfProfileKeyProvider; this.selfProfileKeyProvider = selfProfileKeyProvider;
this.profileKeyProvider = profileKeyProvider;
this.profileProvider = profileProvider; this.profileProvider = profileProvider;
this.senderCertificateProvider = senderCertificateProvider; }
private byte[] getSenderCertificate() {
byte[] certificate;
try {
if (account.isPhoneNumberShared()) {
certificate = dependencies.getAccountManager().getSenderCertificate();
} else {
certificate = dependencies.getAccountManager().getSenderCertificateForPhoneNumberPrivacy();
}
} catch (IOException e) {
logger.warn("Failed to get sender certificate, ignoring: {}", e.getMessage());
return null;
}
// TODO cache for a day
return certificate;
} }
private byte[] getSelfUnidentifiedAccessKey() { private byte[] getSelfUnidentifiedAccessKey() {
@ -45,7 +65,7 @@ public class UnidentifiedAccessHelper {
switch (targetProfile.getUnidentifiedAccessMode()) { switch (targetProfile.getUnidentifiedAccessMode()) {
case ENABLED: case ENABLED:
var theirProfileKey = profileKeyProvider.getProfileKey(recipient); var theirProfileKey = account.getProfileStore().getProfileKey(recipient);
if (theirProfileKey == null) { if (theirProfileKey == null) {
return null; return null;
} }
@ -60,7 +80,7 @@ public class UnidentifiedAccessHelper {
public Optional<UnidentifiedAccessPair> getAccessForSync() { public Optional<UnidentifiedAccessPair> getAccessForSync() {
var selfUnidentifiedAccessKey = getSelfUnidentifiedAccessKey(); var selfUnidentifiedAccessKey = getSelfUnidentifiedAccessKey();
var selfUnidentifiedAccessCertificate = senderCertificateProvider.getSenderCertificate(); var selfUnidentifiedAccessCertificate = getSenderCertificate();
if (selfUnidentifiedAccessKey == null || selfUnidentifiedAccessCertificate == null) { if (selfUnidentifiedAccessKey == null || selfUnidentifiedAccessCertificate == null) {
return Optional.absent(); return Optional.absent();
@ -82,7 +102,7 @@ public class UnidentifiedAccessHelper {
public Optional<UnidentifiedAccessPair> getAccessFor(RecipientId recipient) { public Optional<UnidentifiedAccessPair> getAccessFor(RecipientId recipient) {
var recipientUnidentifiedAccessKey = getTargetUnidentifiedAccessKey(recipient); var recipientUnidentifiedAccessKey = getTargetUnidentifiedAccessKey(recipient);
var selfUnidentifiedAccessKey = getSelfUnidentifiedAccessKey(); var selfUnidentifiedAccessKey = getSelfUnidentifiedAccessKey();
var selfUnidentifiedAccessCertificate = senderCertificateProvider.getSenderCertificate(); var selfUnidentifiedAccessCertificate = getSenderCertificate();
if (recipientUnidentifiedAccessKey == null if (recipientUnidentifiedAccessKey == null
|| selfUnidentifiedAccessKey == null || selfUnidentifiedAccessKey == null

View file

@ -1,6 +0,0 @@
package org.asamk.signal.manager.helper;
public interface UnidentifiedAccessSenderCertificateProvider {
byte[] getSenderCertificate();
}