Integrate call links create/update/read apis.

This commit is contained in:
Alex Hart
2023-05-19 10:28:29 -03:00
committed by Nicholas Tinsley
parent 4d6d31d624
commit 5a38143987
60 changed files with 1986 additions and 191 deletions

View File

@@ -8,23 +8,29 @@ import com.google.protobuf.InvalidProtocolBufferException;
import org.signal.core.util.logging.Log;
import org.signal.libsignal.zkgroup.InvalidInputException;
import org.signal.libsignal.zkgroup.auth.AuthCredentialWithPniResponse;
import org.signal.libsignal.zkgroup.calllinks.CallLinkAuthCredentialResponse;
import org.signal.libsignal.zkgroup.internal.ByteArray;
import org.thoughtcrime.securesms.database.model.databaseprotos.TemporalAuthCredentialResponse;
import org.thoughtcrime.securesms.database.model.databaseprotos.TemporalAuthCredentialResponses;
import org.thoughtcrime.securesms.groups.GroupsV2Authorization;
import org.whispersystems.signalservice.api.groupsv2.GroupsV2Api;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.function.Function;
public final class GroupsV2AuthorizationSignalStoreCache implements GroupsV2Authorization.ValueCache {
private static final String TAG = Log.tag(GroupsV2AuthorizationSignalStoreCache.class);
private static final String ACI_PNI_PREFIX = "gv2:auth_token_cache";
private static final int ACI_PNI_VERSION = 3;
private static final String CALL_LINK_AUTH_PREFIX = "call_link_auth:";
private static final String ACI_PNI_PREFIX = "gv2:auth_token_cache";
private static final int ACI_PNI_VERSION = 3;
private final String key;
private final String callLinkAuthKey;
private final KeyValueStore store;
public static GroupsV2AuthorizationSignalStoreCache createAciCache(@NonNull KeyValueStore store) {
@@ -38,21 +44,30 @@ public final class GroupsV2AuthorizationSignalStoreCache implements GroupsV2Auth
}
private GroupsV2AuthorizationSignalStoreCache(@NonNull KeyValueStore store, @NonNull String key) {
this.store = store;
this.key = key;
this.store = store;
this.key = key;
this.callLinkAuthKey = CALL_LINK_AUTH_PREFIX + key;
}
@Override
public void clear() {
store.beginWrite()
.remove(key)
.remove(callLinkAuthKey)
.commit();
Log.i(TAG, "Cleared local response cache");
}
@Override
public @NonNull Map<Long, AuthCredentialWithPniResponse> read() {
public @NonNull GroupsV2Api.CredentialResponseMaps read() {
Map<Long, AuthCredentialWithPniResponse> credentials = read(key, AuthCredentialWithPniResponse::new);
Map<Long, CallLinkAuthCredentialResponse> callLinkCredentials = read(callLinkAuthKey, CallLinkAuthCredentialResponse::new);
return new GroupsV2Api.CredentialResponseMaps(credentials, callLinkCredentials);
}
public <T extends ByteArray> @NonNull Map<Long, T> read(@NonNull String key, @NonNull CredentialConstructor<T> factory) {
byte[] credentialBlob = store.getBlob(key, null);
if (credentialBlob == null) {
@@ -61,11 +76,11 @@ public final class GroupsV2AuthorizationSignalStoreCache implements GroupsV2Auth
}
try {
TemporalAuthCredentialResponses temporalCredentials = TemporalAuthCredentialResponses.parseFrom(credentialBlob);
HashMap<Long, AuthCredentialWithPniResponse> result = new HashMap<>(temporalCredentials.getCredentialResponseCount());
TemporalAuthCredentialResponses temporalCredentials = TemporalAuthCredentialResponses.parseFrom(credentialBlob);
HashMap<Long, T> result = new HashMap<>(temporalCredentials.getCredentialResponseCount());
for (TemporalAuthCredentialResponse credential : temporalCredentials.getCredentialResponseList()) {
result.put(credential.getDate(), new AuthCredentialWithPniResponse(credential.getAuthCredentialResponse().toByteArray()));
result.put(credential.getDate(), factory.apply(credential.getAuthCredentialResponse().toByteArray()));
}
Log.i(TAG, String.format(Locale.US, "Loaded %d credentials from local storage", result.size()));
@@ -77,10 +92,15 @@ public final class GroupsV2AuthorizationSignalStoreCache implements GroupsV2Auth
}
@Override
public void write(@NonNull Map<Long, AuthCredentialWithPniResponse> values) {
public void write(@NonNull GroupsV2Api.CredentialResponseMaps values) {
write(key, values.getAuthCredentialWithPniResponseHashMap());
write(callLinkAuthKey, values.getCallLinkAuthCredentialResponseHashMap());
}
private <T extends ByteArray> void write(@NonNull String key, @NonNull Map<Long, T> values) {
TemporalAuthCredentialResponses.Builder builder = TemporalAuthCredentialResponses.newBuilder();
for (Map.Entry<Long, AuthCredentialWithPniResponse> entry : values.entrySet()) {
for (Map.Entry<Long, T> entry : values.entrySet()) {
builder.addCredentialResponse(TemporalAuthCredentialResponse.newBuilder()
.setDate(entry.getKey())
.setAuthCredentialResponse(ByteString.copyFrom(entry.getValue().serialize())));
@@ -92,4 +112,8 @@ public final class GroupsV2AuthorizationSignalStoreCache implements GroupsV2Auth
Log.i(TAG, String.format(Locale.US, "Written %d credentials to local storage", values.size()));
}
private interface CredentialConstructor<T extends ByteArray> {
T apply(byte[] bytes) throws InvalidInputException;
}
}