Cache group credentials in memory

This commit is contained in:
AsamK 2021-12-05 11:00:22 +01:00
parent e83f43065e
commit e92d37e3a5

View file

@ -21,6 +21,7 @@ import org.signal.storageservice.protos.groups.local.DecryptedGroupJoinInfo;
import org.signal.storageservice.protos.groups.local.DecryptedPendingMember; import org.signal.storageservice.protos.groups.local.DecryptedPendingMember;
import org.signal.zkgroup.InvalidInputException; import org.signal.zkgroup.InvalidInputException;
import org.signal.zkgroup.VerificationFailedException; import org.signal.zkgroup.VerificationFailedException;
import org.signal.zkgroup.auth.AuthCredentialResponse;
import org.signal.zkgroup.groups.GroupMasterKey; import org.signal.zkgroup.groups.GroupMasterKey;
import org.signal.zkgroup.groups.GroupSecretParams; import org.signal.zkgroup.groups.GroupSecretParams;
import org.signal.zkgroup.groups.UuidCiphertext; import org.signal.zkgroup.groups.UuidCiphertext;
@ -43,6 +44,7 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.HashMap;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -53,17 +55,14 @@ public class GroupV2Helper {
private final static Logger logger = LoggerFactory.getLogger(GroupV2Helper.class); private final static Logger logger = LoggerFactory.getLogger(GroupV2Helper.class);
private final ProfileKeyCredentialProvider profileKeyCredentialProvider; private final ProfileKeyCredentialProvider profileKeyCredentialProvider;
private final ProfileProvider profileProvider; private final ProfileProvider profileProvider;
private final SelfRecipientIdProvider selfRecipientIdProvider; private final SelfRecipientIdProvider selfRecipientIdProvider;
private final GroupsV2Operations groupsV2Operations; private final GroupsV2Operations groupsV2Operations;
private final GroupsV2Api groupsV2Api; private final GroupsV2Api groupsV2Api;
private final SignalServiceAddressResolver addressResolver; private final SignalServiceAddressResolver addressResolver;
private HashMap<Integer, AuthCredentialResponse> groupApiCredentials;
public GroupV2Helper( public GroupV2Helper(
final ProfileKeyCredentialProvider profileKeyCredentialProvider, final ProfileKeyCredentialProvider profileKeyCredentialProvider,
final ProfileProvider profileProvider, final ProfileProvider profileProvider,
@ -508,10 +507,12 @@ public class GroupV2Helper {
final GroupSecretParams groupSecretParams final GroupSecretParams groupSecretParams
) throws IOException { ) throws IOException {
final var today = currentTimeDays(); final var today = currentTimeDays();
if (groupApiCredentials == null || !groupApiCredentials.containsKey(today)) {
// Returns credentials for the next 7 days // Returns credentials for the next 7 days
final var credentials = groupsV2Api.getCredentials(today); groupApiCredentials = groupsV2Api.getCredentials(today);
// TODO cache credentials until they expire // TODO cache credentials on disk until they expire
var authCredentialResponse = credentials.get(today); }
var authCredentialResponse = groupApiCredentials.get(today);
final var aci = getSelfAci(); final var aci = getSelfAci();
try { try {
return groupsV2Api.getGroupsV2AuthorizationString(aci, today, groupSecretParams, authCredentialResponse); return groupsV2Api.getGroupsV2AuthorizationString(aci, today, groupSecretParams, authCredentialResponse);