Set phone number recovery password transactionally when creating accounts

This commit is contained in:
Jon Chambers authored and Jon Chambers committed 2026-08-12 09:11:43 -04:00
1 parent e7ec47aa25
commit 647b25b87c
11 files changed
+91 -63

No files matched your search

@@ -45,7 +45,6 @@ import org.junitpioneer.jupiter.cartesian.CartesianTest;
import org.signal.libsignal.protocol.IdentityKey;
import org.signal.libsignal.protocol.ecc.ECKeyPair;
import org.signal.libsignal.zkgroup.InvalidInputException;
import org.signal.libsignal.zkgroup.ServerSecretParams;
import org.signal.libsignal.zkgroup.VerificationFailedException;
import org.signal.libsignal.zkgroup.receipts.ReceiptSerial;
import org.whispersystems.textsecuregcm.auth.DisconnectionRequestManager;
@@ -54,14 +53,12 @@ import org.whispersystems.textsecuregcm.entities.ApnRegistrationId;
import org.whispersystems.textsecuregcm.entities.ECSignedPreKey;
import org.whispersystems.textsecuregcm.entities.GcmRegistrationId;
import org.whispersystems.textsecuregcm.entities.KEMSignedPreKey;
import org.whispersystems.textsecuregcm.identity.IdentityType;
import org.whispersystems.textsecuregcm.redis.FaultTolerantRedisClient;
import org.whispersystems.textsecuregcm.redis.RedisClusterExtension;
import org.whispersystems.textsecuregcm.securestorage.SecureStorageClient;
import org.whispersystems.textsecuregcm.securevaluerecovery.SecureValueRecoveryClient;
import org.whispersystems.textsecuregcm.tests.util.KeysHelper;
import org.whispersystems.textsecuregcm.util.TestRandomUtil;
import org.whispersystems.textsecuregcm.util.ValidBase64URLString;
import software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient;
public class AccountCreationDeletionIntegrationTest {
@@ -79,7 +76,7 @@ public class AccountCreationDeletionIntegrationTest {
DynamoDbExtensionSchema.Tables.PAGED_PQ_KEYS,
DynamoDbExtensionSchema.Tables.REPEATED_USE_EC_SIGNED_PRE_KEYS,
DynamoDbExtensionSchema.Tables.REPEATED_USE_KEM_SIGNED_PRE_KEYS,
DynamoDbExtensionSchema.Tables.REGISTRATION_RECOVERY_PASSWORDS,
DynamoDbExtensionSchema.Tables.PHONE_NUMBER_RECOVERY_PASSWORDS,
DynamoDbExtensionSchema.Tables.REDEEMED_RECEIPTS);
@RegisterExtension
@@ -90,8 +87,6 @@ public class AccountCreationDeletionIntegrationTest {
private static final Clock CLOCK = Clock.fixed(Instant.now(), ZoneId.systemDefault());
private static final ServerSecretParams RECEIPT_PARAMS = ServerSecretParams.generate();
private ScheduledExecutorService executor;
private AccountsManager accountsManager;
@@ -150,7 +145,7 @@ public class AccountCreationDeletionIntegrationTest {
when(profilesManager.deleteAll(any(), anyBoolean())).thenReturn(CompletableFuture.completedFuture(null));
final PhoneNumberRecoveryPasswords phoneNumberRecoveryPasswords =
new PhoneNumberRecoveryPasswords(DynamoDbExtensionSchema.Tables.REGISTRATION_RECOVERY_PASSWORDS.tableName(),
new PhoneNumberRecoveryPasswords(DynamoDbExtensionSchema.Tables.PHONE_NUMBER_RECOVERY_PASSWORDS.tableName(),
Duration.ofDays(1),
DYNAMO_DB_EXTENSION.getDynamoDbClient(),
CLOCK);
@@ -192,7 +187,7 @@ public class AccountCreationDeletionIntegrationTest {
@CartesianTest
@CartesianTest.MethodFactory("createAccount")
void createAccount(final DeliveryChannels deliveryChannels,
final boolean discoverableByPhoneNumber) throws InterruptedException {
final boolean discoverableByPhoneNumber) {
final String number = PhoneNumberUtil.getInstance().format(
PhoneNumberUtil.getInstance().getExampleNumber("US"),
@@ -204,6 +199,7 @@ public class AccountCreationDeletionIntegrationTest {
final int pniRegistrationId = ThreadLocalRandom.current().nextInt(Device.MAX_REGISTRATION_ID);
final byte[] deviceName = RandomStringUtils.secure().nextAlphabetic(16).getBytes(StandardCharsets.UTF_8);
final String registrationLockSecret = RandomStringUtils.secure().nextAlphanumeric(16);
final byte[] recoveryPassword = TestRandomUtil.nextBytes(16);
final Set<DeviceCapability> deviceCapabilities = Set.of();
@@ -213,7 +209,8 @@ public class AccountCreationDeletionIntegrationTest {
deviceName,
registrationLockSecret,
discoverableByPhoneNumber,
deviceCapabilities, null);
deviceCapabilities,
recoveryPassword);
final List<AccountBadge> badges = new ArrayList<>(List.of(new AccountBadge(
RandomStringUtils.secure().nextAlphabetic(8),
@@ -277,6 +274,7 @@ public class AccountCreationDeletionIntegrationTest {
assertEquals(Optional.of(pniSignedPreKey), keysManager.getEcSignedPreKey(account.getPhoneNumberIdentifierOptional().orElseThrow(), Device.PRIMARY_ID).join());
assertEquals(Optional.of(aciPqLastResortPreKey), keysManager.getLastResort(account.getAccountIdentifier(), Device.PRIMARY_ID).join());
assertEquals(Optional.of(pniPqLastResortPreKey), keysManager.getLastResort(account.getPhoneNumberIdentifierOptional().orElseThrow(), Device.PRIMARY_ID).join());
assertTrue(phoneNumberRecoveryPasswordsManager.verify(account.getPhoneNumberIdentifierOptional().orElseThrow(), recoveryPassword));
}
@ParameterizedTest
@@ -379,12 +377,15 @@ public class AccountCreationDeletionIntegrationTest {
@CartesianTest
@CartesianTest.MethodFactory("createAccount")
void reregisterAccount(final DeliveryChannels deliveryChannels,
final boolean discoverableByPhoneNumber) throws InterruptedException {
final boolean discoverableByPhoneNumber) {
final String number = PhoneNumberUtil.getInstance().format(
PhoneNumberUtil.getInstance().getExampleNumber("US"),
PhoneNumberUtil.PhoneNumberFormat.E164);
final byte[] originalRecoveryPassword = TestRandomUtil.nextBytes(16);
final byte[] updatedRecoveryPassword = TestRandomUtil.nextBytes(17);
final UUID existingAccountUuid;
{
final ECKeyPair aciKeyPair = ECKeyPair.generate();
@@ -397,7 +398,7 @@ public class AccountCreationDeletionIntegrationTest {
final Account existingAccount = accountsManager.create(number,
new AccountAttributes(true, 1, 1, "name".getBytes(StandardCharsets.UTF_8), "registration-lock", false, Set.of(),
null),
originalRecoveryPassword),
Collections.emptyList(),
new IdentityKey(aciKeyPair.getPublicKey()),
new IdentityKey(pniKeyPair.getPublicKey()),
@@ -430,7 +431,8 @@ public class AccountCreationDeletionIntegrationTest {
deviceName,
registrationLockSecret,
discoverableByPhoneNumber,
deviceCapabilities, null);
deviceCapabilities,
updatedRecoveryPassword);
final List<AccountBadge> badges = new ArrayList<>(List.of(new AccountBadge(
RandomStringUtils.secure().nextAlphabetic(8),
@@ -493,6 +495,9 @@ public class AccountCreationDeletionIntegrationTest {
verify(disconnectionRequestManager).requestDisconnection(argThat(account ->
account.getAccountIdentifier().equals(existingAccountUuid) && account != reregisteredAccount));
assertTrue(phoneNumberRecoveryPasswordsManager.verify(reregisteredAccount.getPhoneNumberIdentifierOptional().orElseThrow(), updatedRecoveryPassword));
assertFalse(phoneNumberRecoveryPasswordsManager.verify(reregisteredAccount.getPhoneNumberIdentifierOptional().orElseThrow(), originalRecoveryPassword));
}
@ParameterizedTest
@@ -573,10 +578,10 @@ public class AccountCreationDeletionIntegrationTest {
null);
}
final UUID aci = account.getIdentifier(IdentityType.ACI);
final UUID aci = account.getAccountIdentifier();
assertTrue(accountsManager.getByAccountIdentifier(aci).isPresent());
accountsManager.delete(account.getIdentifier(IdentityType.ACI), AccountsManager.DeletionReason.ADMIN_DELETED);
accountsManager.delete(account.getAccountIdentifier(), AccountsManager.DeletionReason.ADMIN_DELETED);
assertFalse(accountsManager.getByAccountIdentifier(aci).isPresent());
assertFalse(keysManager.getEcSignedPreKey(account.getAccountIdentifier(), Device.PRIMARY_ID).join().isPresent());
@@ -589,7 +594,7 @@ public class AccountCreationDeletionIntegrationTest {
}
verify(disconnectionRequestManager).requestDisconnection(argThat(disconnectedAccount ->
disconnectedAccount.getIdentifier(IdentityType.ACI).equals(account.getIdentifier(IdentityType.ACI))));
disconnectedAccount.getAccountIdentifier().equals(account.getAccountIdentifier())));
}
@Test
@@ -62,7 +62,7 @@ class AccountsManagerChangeNumberIntegrationTest {
Tables.PAGED_PQ_KEYS,
Tables.REPEATED_USE_EC_SIGNED_PRE_KEYS,
Tables.REPEATED_USE_KEM_SIGNED_PRE_KEYS,
Tables.REGISTRATION_RECOVERY_PASSWORDS,
Tables.PHONE_NUMBER_RECOVERY_PASSWORDS,
Tables.REDEEMED_RECEIPTS);
@RegisterExtension
@@ -129,7 +129,7 @@ class AccountsManagerChangeNumberIntegrationTest {
when(profilesManager.deleteAll(any(), anyBoolean())).thenReturn(CompletableFuture.completedFuture(null));
final PhoneNumberRecoveryPasswords phoneNumberRecoveryPasswords =
new PhoneNumberRecoveryPasswords(DynamoDbExtensionSchema.Tables.REGISTRATION_RECOVERY_PASSWORDS.tableName(),
new PhoneNumberRecoveryPasswords(DynamoDbExtensionSchema.Tables.PHONE_NUMBER_RECOVERY_PASSWORDS.tableName(),
Duration.ofDays(1),
DYNAMO_DB_EXTENSION.getDynamoDbClient(),
Clock.systemUTC());
@@ -51,7 +51,7 @@ public class AccountsManagerMigrateRecoveryPasswordIntegrationTest {
DynamoDbExtensionSchema.Tables.PAGED_PQ_KEYS,
DynamoDbExtensionSchema.Tables.REPEATED_USE_EC_SIGNED_PRE_KEYS,
DynamoDbExtensionSchema.Tables.REPEATED_USE_KEM_SIGNED_PRE_KEYS,
DynamoDbExtensionSchema.Tables.REGISTRATION_RECOVERY_PASSWORDS);
DynamoDbExtensionSchema.Tables.PHONE_NUMBER_RECOVERY_PASSWORDS);
@RegisterExtension
static final RedisClusterExtension CACHE_CLUSTER_EXTENSION = RedisClusterExtension.builder().build();
@@ -116,7 +116,7 @@ public class AccountsManagerMigrateRecoveryPasswordIntegrationTest {
when(profilesManager.deleteAll(any(), anyBoolean())).thenReturn(CompletableFuture.completedFuture(null));
final PhoneNumberRecoveryPasswords phoneNumberRecoveryPasswords =
new PhoneNumberRecoveryPasswords(DynamoDbExtensionSchema.Tables.REGISTRATION_RECOVERY_PASSWORDS.tableName(),
new PhoneNumberRecoveryPasswords(DynamoDbExtensionSchema.Tables.PHONE_NUMBER_RECOVERY_PASSWORDS.tableName(),
Duration.ofDays(1),
DYNAMO_DB_EXTENSION.getDynamoDbClient(),
Clock.systemUTC());
@@ -75,7 +75,8 @@ class AccountsManagerUsernameIntegrationTest {
Tables.PAGED_PQ_KEYS,
Tables.REPEATED_USE_EC_SIGNED_PRE_KEYS,
Tables.REPEATED_USE_KEM_SIGNED_PRE_KEYS,
Tables.REDEEMED_RECEIPTS);
Tables.REDEEMED_RECEIPTS,
Tables.PHONE_NUMBER_RECOVERY_PASSWORDS);
@RegisterExtension
static RedisClusterExtension CACHE_CLUSTER_EXTENSION = RedisClusterExtension.builder().build();
@@ -131,6 +132,13 @@ class AccountsManagerUsernameIntegrationTest {
final DisconnectionRequestManager disconnectionRequestManager = mock(DisconnectionRequestManager.class);
when(disconnectionRequestManager.requestDisconnection(any())).thenReturn(CompletableFuture.completedFuture(null));
final PhoneNumberRecoveryPasswordsManager phoneNumberRecoveryPasswordsManager =
new PhoneNumberRecoveryPasswordsManager(new PhoneNumberRecoveryPasswords(
Tables.PHONE_NUMBER_RECOVERY_PASSWORDS.tableName(),
Duration.ofDays(1),
DYNAMO_DB_EXTENSION.getDynamoDbClient(),
Clock.systemUTC()));
accountsManager = new AccountsManager(
accounts,
phoneNumberIdentifiers,
@@ -144,7 +152,7 @@ class AccountsManagerUsernameIntegrationTest {
mock(SecureStorageClient.class),
mock(SecureValueRecoveryClient.class),
disconnectionRequestManager,
mock(PhoneNumberRecoveryPasswordsManager.class),
phoneNumberRecoveryPasswordsManager,
Executors.newSingleThreadExecutor(),
Executors.newSingleThreadScheduledExecutor(),
Executors.newSingleThreadScheduledExecutor(),
@@ -14,6 +14,7 @@ import static org.mockito.Mockito.when;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import java.nio.charset.StandardCharsets;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.Optional;
@@ -60,7 +61,8 @@ public class AddRemoveDeviceIntegrationTest {
DynamoDbExtensionSchema.Tables.EC_KEYS,
DynamoDbExtensionSchema.Tables.PAGED_PQ_KEYS,
DynamoDbExtensionSchema.Tables.REPEATED_USE_EC_SIGNED_PRE_KEYS,
DynamoDbExtensionSchema.Tables.REPEATED_USE_KEM_SIGNED_PRE_KEYS);
DynamoDbExtensionSchema.Tables.REPEATED_USE_KEM_SIGNED_PRE_KEYS,
DynamoDbExtensionSchema.Tables.PHONE_NUMBER_RECOVERY_PASSWORDS);
@RegisterExtension
static final RedisClusterExtension CACHE_CLUSTER_EXTENSION = RedisClusterExtension.builder().build();
@@ -131,7 +133,11 @@ public class AddRemoveDeviceIntegrationTest {
when(profilesManager.deleteAll(any(), anyBoolean())).thenReturn(CompletableFuture.completedFuture(null));
final PhoneNumberRecoveryPasswordsManager phoneNumberRecoveryPasswordsManager =
mock(PhoneNumberRecoveryPasswordsManager.class);
new PhoneNumberRecoveryPasswordsManager(new PhoneNumberRecoveryPasswords(
DynamoDbExtensionSchema.Tables.PHONE_NUMBER_RECOVERY_PASSWORDS.tableName(),
Duration.ofDays(1),
DYNAMO_DB_EXTENSION.getDynamoDbClient(),
Clock.systemUTC()));
PUBSUB_SERVER_EXTENSION.getRedisClient().useConnection(connection -> {
connection.sync().flushall();
@@ -151,7 +157,7 @@ public class AddRemoveDeviceIntegrationTest {
secureStorageClient,
svr2Client,
mock(DisconnectionRequestManager.class),
mock(PhoneNumberRecoveryPasswordsManager.class),
phoneNumberRecoveryPasswordsManager,
accountLockExecutor,
scheduledExecutorService,
scheduledExecutorService,
@@ -316,7 +316,7 @@ public final class DynamoDbExtensionSchema {
.build()),
List.of(), List.of()),
REGISTRATION_RECOVERY_PASSWORDS("registration_recovery_passwords_test",
PHONE_NUMBER_RECOVERY_PASSWORDS("registration_recovery_passwords_test",
PhoneNumberRecoveryPasswords.KEY_PNI,
null,
List.of(AttributeDefinition.builder()
@@ -24,7 +24,7 @@ class PhoneNumberRecoveryPasswordsTest {
@RegisterExtension
static final DynamoDbExtension DYNAMO_DB_EXTENSION = new DynamoDbExtension(
DynamoDbExtensionSchema.Tables.REGISTRATION_RECOVERY_PASSWORDS
DynamoDbExtensionSchema.Tables.PHONE_NUMBER_RECOVERY_PASSWORDS
);
private PhoneNumberRecoveryPasswords phoneNumberRecoveryPasswords;
@@ -32,7 +32,7 @@ class PhoneNumberRecoveryPasswordsTest {
@BeforeEach
void setUp() {
phoneNumberRecoveryPasswords = new PhoneNumberRecoveryPasswords(
DynamoDbExtensionSchema.Tables.REGISTRATION_RECOVERY_PASSWORDS.tableName(),
DynamoDbExtensionSchema.Tables.PHONE_NUMBER_RECOVERY_PASSWORDS.tableName(),
Duration.ofDays(1),
DYNAMO_DB_EXTENSION.getDynamoDbClient(),
Clock.systemUTC());
@@ -39,7 +39,7 @@ public class RegistrationRecoveryTest {
@RegisterExtension
private static final DynamoDbExtension DYNAMO_DB_EXTENSION =
new DynamoDbExtension(Tables.REGISTRATION_RECOVERY_PASSWORDS);
new DynamoDbExtension(Tables.PHONE_NUMBER_RECOVERY_PASSWORDS);
private PhoneNumberRecoveryPasswords phoneNumberRecoveryPasswords;
@@ -49,7 +49,7 @@ public class RegistrationRecoveryTest {
public void before() throws Exception {
CLOCK.setTimeMillis(Clock.systemUTC().millis());
phoneNumberRecoveryPasswords = new PhoneNumberRecoveryPasswords(
Tables.REGISTRATION_RECOVERY_PASSWORDS.tableName(),
Tables.PHONE_NUMBER_RECOVERY_PASSWORDS.tableName(),
EXPIRATION,
DYNAMO_DB_EXTENSION.getDynamoDbClient(),
CLOCK
@@ -139,7 +139,7 @@ public class RegistrationRecoveryTest {
private static long fetchTimestamp(final UUID phoneNumberIdentifier) {
final GetItemResponse getItemResponse = DYNAMO_DB_EXTENSION.getDynamoDbClient().getItem(GetItemRequest.builder()
.tableName(Tables.REGISTRATION_RECOVERY_PASSWORDS.tableName())
.tableName(Tables.PHONE_NUMBER_RECOVERY_PASSWORDS.tableName())
.key(Map.of(PhoneNumberRecoveryPasswords.KEY_PNI, AttributeValues.fromString(phoneNumberIdentifier.toString())))
.build());