mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-12-02 12:06:53 +00:00
store account profile current version as byte array, not hex
This commit is contained in:
1 parent
022f2b874d
commit
06d96a04df
11 files changed
+104
-68
No files matched your search
+3
-3
@@ -216,7 +216,7 @@ public class ProfileController {
|
||||
.orElseGet(a::getBadges);
|
||||
|
||||
a.setBadges(clock, updatedBadges);
|
||||
a.setCurrentProfileVersion(request.version());
|
||||
a.setCurrentProfileVersion(HexFormat.of().parseHex(request.version()));
|
||||
});
|
||||
|
||||
if (request.getAvatarChange() == CreateProfileRequest.AvatarChange.UPDATE) {
|
||||
@@ -436,7 +436,7 @@ public class ProfileController {
|
||||
|
||||
final ExpiringProfileKeyCredentialResponse expiringProfileKeyCredentialResponse;
|
||||
|
||||
if (account.getCurrentProfileVersion().map(version::equals).orElse(false)) {
|
||||
if (account.getCurrentProfileVersion().map(v -> HexFormat.of().formatHex(v).equals(version)).orElse(false)) {
|
||||
expiringProfileKeyCredentialResponse = profilesManager.getV1(account.getUuid(), version)
|
||||
.map(profile -> {
|
||||
final ExpiringProfileKeyCredentialResponse profileKeyCredentialResponse;
|
||||
@@ -488,7 +488,7 @@ public class ProfileController {
|
||||
// Allow requests where either the version matches the latest version on Account or the latest version on Account
|
||||
// is empty to read the payment address.
|
||||
final byte[] paymentAddress = maybeProfile
|
||||
.filter(p -> account.getCurrentProfileVersion().map(v -> v.equals(p.version())).orElse(true))
|
||||
.filter(p -> account.getCurrentProfileVersion().map(v -> HexFormat.of().formatHex(v).equals(p.version())).orElse(true))
|
||||
.map(VersionedProfileV1::paymentAddress)
|
||||
.orElse(null);
|
||||
|
||||
|
||||
@@ -95,7 +95,7 @@ public class ProfileGrpcHelper {
|
||||
|
||||
// Include payment address if the version matches the latest version on Account or the latest version on Account
|
||||
// is empty
|
||||
if (account.getCurrentProfileVersion().map(v -> v.equals(HexFormat.of().formatHex(requestVersion))).orElse(true)) {
|
||||
if (account.getCurrentProfileVersion().map(v -> Arrays.equals(v, requestVersion)).orElse(true)) {
|
||||
final Optional<byte []> paymentAddress = profile.map(VersionedProfile::paymentAddress).or(() -> v1Profile.map(VersionedProfileV1::paymentAddress));
|
||||
|
||||
if (paymentAddress.isPresent()) {
|
||||
|
||||
@@ -8,6 +8,7 @@ package org.whispersystems.textsecuregcm.grpc;
|
||||
import com.google.protobuf.ByteString;
|
||||
import java.time.Clock;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Arrays;
|
||||
import java.util.HexFormat;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -107,11 +108,8 @@ public class ProfileGrpcService extends SimpleProfileGrpc.ProfileImplBase {
|
||||
|
||||
validateRequest(request);
|
||||
|
||||
final String expectedCurrentVersionHex = HexFormat.of().formatHex(request.getExpectedCurrentVersion().toByteArray());
|
||||
|
||||
final boolean currentVersionMatchesExpected = account.getCurrentProfileVersion().isEmpty()
|
||||
? request.getExpectedCurrentVersion().isEmpty()
|
||||
: account.getCurrentProfileVersion().get().equals(expectedCurrentVersionHex);
|
||||
final byte[] expectedCurrentVersion = request.getExpectedCurrentVersion().toByteArray();
|
||||
final boolean currentVersionMatchesExpected = Arrays.equals(account.getCurrentProfileVersion().orElse(new byte[0]), expectedCurrentVersion);
|
||||
|
||||
if (!currentVersionMatchesExpected) {
|
||||
return SetProfileResponse.newBuilder().setExpectedVersionWriteConflict(FailedPrecondition.newBuilder()
|
||||
@@ -177,7 +175,7 @@ public class ProfileGrpcService extends SimpleProfileGrpc.ProfileImplBase {
|
||||
}
|
||||
|
||||
try {
|
||||
accountsManager.updateCurrentProfileVersion(account.getIdentifier(IdentityType.ACI), version, expectedCurrentVersionHex, a -> {
|
||||
accountsManager.updateCurrentProfileVersion(account.getIdentifier(IdentityType.ACI), version, expectedCurrentVersion, a -> {
|
||||
|
||||
final List<AccountBadge> updatedBadges = Optional.of(request.getBadgeIdsList())
|
||||
.map(badges -> ProfileHelper.mergeBadgeIdsWithExistingAccountBadges(clock, badgeConfigurationMap, badges,
|
||||
|
||||
@@ -8,12 +8,21 @@ package org.whispersystems.textsecuregcm.storage;
|
||||
import com.fasterxml.jackson.annotation.JsonFilter;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import java.io.IOException;
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.Collections;
|
||||
import java.util.HexFormat;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
@@ -80,7 +89,9 @@ public class Account {
|
||||
private IdentityKey phoneNumberIdentityKey;
|
||||
|
||||
@JsonProperty("cpv")
|
||||
private String currentProfileVersion;
|
||||
@JsonSerialize(using = ProfileKeyAdapter.Serializing.class)
|
||||
@JsonDeserialize(using = ProfileKeyAdapter.Deserializing.class)
|
||||
private byte[] currentProfileVersion;
|
||||
|
||||
@JsonProperty
|
||||
private List<AccountBadge> badges = new ArrayList<>();
|
||||
@@ -323,13 +334,13 @@ public class Account {
|
||||
.orElse(0L);
|
||||
}
|
||||
|
||||
public Optional<String> getCurrentProfileVersion() {
|
||||
public Optional<byte[]> getCurrentProfileVersion() {
|
||||
requireNotStale();
|
||||
|
||||
return Optional.ofNullable(currentProfileVersion);
|
||||
}
|
||||
|
||||
public void setCurrentProfileVersion(final String currentProfileVersion) {
|
||||
public void setCurrentProfileVersion(final byte[] currentProfileVersion) {
|
||||
requireNotStale();
|
||||
|
||||
this.currentProfileVersion = currentProfileVersion;
|
||||
@@ -564,4 +575,26 @@ public class Account {
|
||||
logger.error("Accessor called on stale account", new RuntimeException());
|
||||
}
|
||||
}
|
||||
|
||||
private static class ProfileKeyAdapter {
|
||||
private static class Deserializing extends JsonDeserializer<byte[]> {
|
||||
@Override
|
||||
public byte[] deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
|
||||
final String val = jsonParser.getValueAsString();
|
||||
if (val.length() == 64) {
|
||||
return HexFormat.of().parseHex(val);
|
||||
} else {
|
||||
return Base64.getDecoder().decode(val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// after deploying this once we can get rid of it in favor of the default base64
|
||||
private static class Serializing extends JsonSerializer<byte[]> {
|
||||
@Override
|
||||
public void serialize(byte[] bytes, JsonGenerator jsonGenerator, SerializerProvider serializaterProvider) throws IOException {
|
||||
jsonGenerator.writeString(HexFormat.of().formatHex(bytes));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -967,7 +967,7 @@ public class AccountsManager extends RedisPubSubAdapter<String, String> implemen
|
||||
///
|
||||
/// @throws WriteConflictException if the expected current version does not match the current version
|
||||
public Account updateCurrentProfileVersion(final UUID accountIdentifier, final byte[] newVersion,
|
||||
final String expectedCurrentVersion, final Consumer<Account> updater) throws WriteConflictException {
|
||||
final byte[] expectedCurrentVersion, final Consumer<Account> updater) throws WriteConflictException {
|
||||
|
||||
Objects.requireNonNull(expectedCurrentVersion, "expectedCurrentVersion");
|
||||
|
||||
@@ -975,22 +975,20 @@ public class AccountsManager extends RedisPubSubAdapter<String, String> implemen
|
||||
final Account account = accounts.getByAccountIdentifier(accountIdentifier)
|
||||
.orElseThrow(() -> new IllegalArgumentException("Account not found: " + accountIdentifier));
|
||||
|
||||
final String newVersionHex = HexFormat.of().formatHex(newVersion);
|
||||
|
||||
return accountLockManager.withLock(Set.of(account.getPhoneNumberIdentifier()), () -> {
|
||||
final Account maybeUpdatedAccount = update(accountIdentifier, a -> {
|
||||
if (!a.getCurrentProfileVersion().orElse("").equals(expectedCurrentVersion)) {
|
||||
if (!a.getCurrentProfileVersion().orElse(new byte[0]).equals(expectedCurrentVersion)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
a.setCurrentProfileVersion(newVersionHex);
|
||||
a.setCurrentProfileVersion(newVersion);
|
||||
|
||||
updater.accept(a);
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
if (!maybeUpdatedAccount.getCurrentProfileVersion().map(v -> v.equals(newVersionHex)).orElse(false)) {
|
||||
if (!maybeUpdatedAccount.getCurrentProfileVersion().map(v -> v.equals(newVersion)).orElse(false)) {
|
||||
throw new WriteConflictException();
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user