Set/remove account recovery passwords alongside phone number recovery passwords

This commit is contained in:
Jon Chambers authored and Jon Chambers committed 2026-08-05 14:50:00 -04:00
1 parent c64aeec36a
commit 453222ba17
13 files changed
+141 -54

No files matched your search

@@ -31,7 +31,6 @@ import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.whispersystems.textsecuregcm.controllers.RateLimitExceededException;
import org.whispersystems.textsecuregcm.entities.PhoneVerificationRequest;
import org.whispersystems.textsecuregcm.identity.IdentityType;
import org.whispersystems.textsecuregcm.limits.RateLimiter;
import org.whispersystems.textsecuregcm.limits.RateLimiters;
import org.whispersystems.textsecuregcm.push.NotPushRegisteredException;
@@ -77,8 +76,8 @@ class RegistrationLockVerificationManagerTest {
account = mock(Account.class);
final UUID accountIdentifier = UUID.randomUUID();
when(account.getAccountIdentifier()).thenReturn(accountIdentifier);
when(account.getIdentifier(IdentityType.ACI)).thenReturn(accountIdentifier);
when(account.getNumberOptional()).thenReturn(Optional.of("+18005551212"));
when(account.getPhoneNumberIdentifierOptional()).thenReturn(Optional.of(UUID.randomUUID()));
when(account.getDevices()).thenReturn(List.of(device));
AccountsHelper.setupMockGet(accountsManager, account);
@@ -104,7 +103,7 @@ class RegistrationLockVerificationManagerTest {
yield new Pair<>(RegistrationLockFailureException.class, e -> {
if (e instanceof RegistrationLockFailureException) {
if (!verificationType.equals(PhoneVerificationRequest.VerificationType.RECOVERY_PASSWORD) || clientRegistrationLock != null) {
verify(phoneNumberRecoveryPasswordsManager).remove(account.getIdentifier(IdentityType.PNI));
verify(phoneNumberRecoveryPasswordsManager).remove(account.getPhoneNumberIdentifierOptional().orElseThrow());
} else {
verify(phoneNumberRecoveryPasswordsManager, never()).remove(any());
}
@@ -12,6 +12,7 @@ import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyByte;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.clearInvocations;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
@@ -98,6 +99,7 @@ import org.whispersystems.textsecuregcm.util.SystemMapper;
import org.whispersystems.textsecuregcm.util.TestRandomUtil;
import org.whispersystems.textsecuregcm.util.TestRemoteAddressFilterProvider;
import org.whispersystems.textsecuregcm.util.UsernameHashZkProofVerifier;
import software.amazon.awssdk.services.dynamodb.model.TransactWriteItem;
@ExtendWith(DropwizardExtensionsSupport.class)
class AccountControllerTest {
@@ -227,6 +229,9 @@ class AccountControllerTest {
return null;
}).when(usernameZkProofVerifier).verifyProof(any(), any());
when(PHONE_NUMBER_RECOVERY_PASSWORDS_MANAGER.buildTransactWriteItemForStorePassword(any(), any()))
.thenReturn(TransactWriteItem.builder().build());
}
@AfterEach
@@ -856,7 +861,8 @@ class AccountControllerTest {
.setRecoveryPassword(recoveryPassword)))) {
assertThat(response.getStatus()).isEqualTo(204);
verify(PHONE_NUMBER_RECOVERY_PASSWORDS_MANAGER).store(AuthHelper.UNDISCOVERABLE_PNI, recoveryPassword);
verify(accountsManager).update(eq(AuthHelper.UNDISCOVERABLE_UUID), any(), argThat(additionWriteItems -> additionWriteItems.size() == 1));
verify(AuthHelper.UNDISCOVERABLE_ACCOUNT).setAccountRecoveryPassword(recoveryPassword);
}
}
@@ -10,6 +10,7 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
@@ -99,7 +100,6 @@ import org.whispersystems.textsecuregcm.entities.ECSignedPreKey;
import org.whispersystems.textsecuregcm.entities.EncryptedUsername;
import org.whispersystems.textsecuregcm.entities.KEMSignedPreKey;
import org.whispersystems.textsecuregcm.identity.AciServiceIdentifier;
import org.whispersystems.textsecuregcm.identity.IdentityType;
import org.whispersystems.textsecuregcm.identity.PniServiceIdentifier;
import org.whispersystems.textsecuregcm.limits.RateLimiter;
import org.whispersystems.textsecuregcm.limits.RateLimiters;
@@ -121,6 +121,7 @@ import org.whispersystems.textsecuregcm.util.TestClock;
import org.whispersystems.textsecuregcm.util.TestRandomUtil;
import org.whispersystems.textsecuregcm.util.UUIDUtil;
import org.whispersystems.textsecuregcm.util.UsernameHashZkProofVerifier;
import software.amazon.awssdk.services.dynamodb.model.TransactWriteItem;
class AccountsGrpcServiceTest extends SimpleBaseGrpcTest<AccountsGrpcService, AccountsGrpc.AccountsBlockingStub> {
@@ -171,9 +172,7 @@ class AccountsGrpcServiceTest extends SimpleBaseGrpcTest<AccountsGrpcService, Ac
final Account account = mock(Account.class);
when(account.getAccountIdentifier()).thenReturn(AUTHENTICATED_ACI);
when(account.getPhoneNumberIdentifier()).thenReturn(phoneNumberIdentifier);
when(account.getPhoneNumberIdentifierOptional()).thenReturn(Optional.of(phoneNumberIdentifier));
when(account.getNumber()).thenReturn(e164);
when(account.getNumberOptional()).thenReturn(Optional.of(e164));
when(account.getUsernameHash()).thenReturn(Optional.of(usernameHash));
when(account.getUsernameLinkHandle()).thenReturn(usernameLinkHandle);
@@ -286,7 +285,7 @@ class AccountsGrpcServiceTest extends SimpleBaseGrpcTest<AccountsGrpcService, Ac
@Test
void reserveUsernameHash() throws UsernameHashNotAvailableException {
final Account account = mock(Account.class);
when(account.getIdentifier(IdentityType.ACI)).thenReturn(AUTHENTICATED_ACI);
when(account.getAccountIdentifier()).thenReturn(AUTHENTICATED_ACI);
when(accountsManager.getByAccountIdentifier(AUTHENTICATED_ACI))
.thenReturn(Optional.of(account));
@@ -547,7 +546,7 @@ class AccountsGrpcServiceTest extends SimpleBaseGrpcTest<AccountsGrpcService, Ac
void setUsernameLink(final boolean keepLink) {
final Account account = mock(Account.class);
final UUID oldHandle = UUID.randomUUID();
when(account.getIdentifier(IdentityType.ACI)).thenReturn(AUTHENTICATED_ACI);
when(account.getAccountIdentifier()).thenReturn(AUTHENTICATED_ACI);
when(account.getUsernameHash()).thenReturn(Optional.of(new byte[AccountController.USERNAME_HASH_LENGTH]));
when(account.getUsernameLinkHandle()).thenReturn(oldHandle);
@@ -717,19 +716,24 @@ class AccountsGrpcServiceTest extends SimpleBaseGrpcTest<AccountsGrpcService, Ac
final UUID phoneNumberIdentifier = UUID.randomUUID();
final Account account = mock(Account.class);
when(account.getIdentifier(IdentityType.PNI)).thenReturn(phoneNumberIdentifier);
when(account.getAccountIdentifier()).thenReturn(AUTHENTICATED_ACI);
when(account.getPhoneNumberIdentifierOptional()).thenReturn(Optional.of(phoneNumberIdentifier));
when(accountsManager.getByAccountIdentifier(AUTHENTICATED_ACI))
.thenReturn(Optional.of(account));
final byte[] registrationRecoveryPassword = TestRandomUtil.nextBytes(32);
when(phoneNumberRecoveryPasswordsManager.buildTransactWriteItemForStorePassword(phoneNumberIdentifier, registrationRecoveryPassword))
.thenReturn(TransactWriteItem.builder().build());
assertDoesNotThrow(() ->
authenticatedServiceStub().setRegistrationRecoveryPassword(SetRegistrationRecoveryPasswordRequest.newBuilder()
.setRegistrationRecoveryPassword(ByteString.copyFrom(registrationRecoveryPassword))
.build()));
verify(phoneNumberRecoveryPasswordsManager).store(account.getIdentifier(IdentityType.PNI), registrationRecoveryPassword);
verify(account).setAccountRecoveryPassword(registrationRecoveryPassword);
verify(accountsManager).update(eq(AUTHENTICATED_ACI), any(), argThat(additionalWriteItems -> additionalWriteItems.size() == 1));
}
@Test
@@ -741,7 +745,7 @@ class AccountsGrpcServiceTest extends SimpleBaseGrpcTest<AccountsGrpcService, Ac
@ParameterizedTest
@ValueSource(booleans = {true, false})
void setZkCredentialKey(final boolean matchesCurrentZkCredentialKey) throws Exception {
void setZkCredentialKey(final boolean matchesCurrentZkCredentialKey) {
final ZkCredentialPublicKey publicKey = ZkCredentialKeyPair.generate().getPublicKey();
final long rotationId = ThreadLocalRandom.current().nextLong(1, Long.MAX_VALUE);
@@ -849,9 +853,7 @@ class AccountsGrpcServiceTest extends SimpleBaseGrpcTest<AccountsGrpcService, Ac
final Account updatedAccount = mock(Account.class);
when(updatedAccount.getAccountIdentifier()).thenReturn(AUTHENTICATED_ACI);
when(updatedAccount.getNumber()).thenReturn(newNumber);
when(updatedAccount.getNumberOptional()).thenReturn(Optional.of(newNumber));
when(updatedAccount.getPhoneNumberIdentifier()).thenReturn(updatedPni);
when(updatedAccount.getPhoneNumberIdentifierOptional()).thenReturn(Optional.of(updatedPni));
when(updatedAccount.getUsernameHash()).thenReturn(Optional.empty());
@@ -51,6 +51,7 @@ 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 software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient;
public class AccountCreationDeletionIntegrationTest {
@@ -67,7 +68,8 @@ public class AccountCreationDeletionIntegrationTest {
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.REGISTRATION_RECOVERY_PASSWORDS);
@RegisterExtension
static final RedisClusterExtension CACHE_CLUSTER_EXTENSION = RedisClusterExtension.builder().build();
@@ -81,6 +83,7 @@ public class AccountCreationDeletionIntegrationTest {
private AccountsManager accountsManager;
private KeysManager keysManager;
private PhoneNumberRecoveryPasswordsManager phoneNumberRecoveryPasswordsManager;
private DisconnectionRequestManager disconnectionRequestManager;
record DeliveryChannels(boolean fetchesMessages, String apnsToken, String fcmToken) {}
@@ -131,8 +134,13 @@ public class AccountCreationDeletionIntegrationTest {
final ProfilesManager profilesManager = mock(ProfilesManager.class);
when(profilesManager.deleteAll(any(), anyBoolean())).thenReturn(CompletableFuture.completedFuture(null));
final PhoneNumberRecoveryPasswordsManager phoneNumberRecoveryPasswordsManager =
mock(PhoneNumberRecoveryPasswordsManager.class);
final PhoneNumberRecoveryPasswords phoneNumberRecoveryPasswords =
new PhoneNumberRecoveryPasswords(DynamoDbExtensionSchema.Tables.REGISTRATION_RECOVERY_PASSWORDS.tableName(),
Duration.ofDays(1),
DYNAMO_DB_EXTENSION.getDynamoDbClient(),
CLOCK);
phoneNumberRecoveryPasswordsManager = new PhoneNumberRecoveryPasswordsManager(phoneNumberRecoveryPasswords);
disconnectionRequestManager = mock(DisconnectionRequestManager.class);
when(disconnectionRequestManager.requestDisconnection(any())).thenReturn(CompletableFuture.completedFuture(null));
@@ -423,6 +431,8 @@ public class AccountCreationDeletionIntegrationTest {
true,
deviceCapabilities);
accountAttributes.setRecoveryPassword(TestRandomUtil.nextBytes(16));
final List<AccountBadge> badges = new ArrayList<>(List.of(new AccountBadge(
RandomStringUtils.secure().nextAlphabetic(8),
CLOCK.instant().plus(Duration.ofDays(7)),
@@ -460,6 +470,8 @@ public class AccountCreationDeletionIntegrationTest {
final UUID aci = account.getIdentifier(IdentityType.ACI);
assertTrue(accountsManager.getByAccountIdentifier(aci).isPresent());
assertTrue(phoneNumberRecoveryPasswordsManager.verify(account.getPhoneNumberIdentifierOptional().orElseThrow(),
accountAttributes.recoveryPassword().orElseThrow()));
accountsManager.delete(account.getIdentifier(IdentityType.ACI), AccountsManager.DeletionReason.ADMIN_DELETED);
@@ -468,6 +480,8 @@ public class AccountCreationDeletionIntegrationTest {
assertFalse(keysManager.getEcSignedPreKey(account.getPhoneNumberIdentifier(), Device.PRIMARY_ID).join().isPresent());
assertFalse(keysManager.getLastResort(account.getAccountIdentifier(), Device.PRIMARY_ID).join().isPresent());
assertFalse(keysManager.getLastResort(account.getPhoneNumberIdentifier(), Device.PRIMARY_ID).join().isPresent());
assertFalse(phoneNumberRecoveryPasswordsManager.verify(account.getPhoneNumberIdentifierOptional().orElseThrow(),
accountAttributes.recoveryPassword().orElseThrow()));
verify(disconnectionRequestManager).requestDisconnection(argThat(disconnectedAccount ->
disconnectedAccount.getIdentifier(IdentityType.ACI).equals(account.getIdentifier(IdentityType.ACI))));
@@ -17,6 +17,7 @@ import static org.mockito.Mockito.when;
import java.nio.charset.StandardCharsets;
import java.time.Clock;
import java.time.Duration;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
@@ -60,7 +61,8 @@ class AccountsManagerChangeNumberIntegrationTest {
Tables.EC_KEYS,
Tables.PAGED_PQ_KEYS,
Tables.REPEATED_USE_EC_SIGNED_PRE_KEYS,
Tables.REPEATED_USE_KEM_SIGNED_PRE_KEYS);
Tables.REPEATED_USE_KEM_SIGNED_PRE_KEYS,
Tables.REGISTRATION_RECOVERY_PASSWORDS);
@RegisterExtension
static final RedisClusterExtension CACHE_CLUSTER_EXTENSION = RedisClusterExtension.builder().build();
@@ -123,8 +125,14 @@ class AccountsManagerChangeNumberIntegrationTest {
final ProfilesManager profilesManager = mock(ProfilesManager.class);
when(profilesManager.deleteAll(any(), anyBoolean())).thenReturn(CompletableFuture.completedFuture(null));
final PhoneNumberRecoveryPasswords phoneNumberRecoveryPasswords =
new PhoneNumberRecoveryPasswords(DynamoDbExtensionSchema.Tables.REGISTRATION_RECOVERY_PASSWORDS.tableName(),
Duration.ofDays(1),
DYNAMO_DB_EXTENSION.getDynamoDbClient(),
Clock.systemUTC());
final PhoneNumberRecoveryPasswordsManager phoneNumberRecoveryPasswordsManager =
mock(PhoneNumberRecoveryPasswordsManager.class);
new PhoneNumberRecoveryPasswordsManager(phoneNumberRecoveryPasswords);
accountsManager = new AccountsManager(
accounts,
@@ -130,6 +130,15 @@ public class AccountsHelper {
return account;
});
when(mockAccountsManager.update(any(UUID.class), any(), any())).thenAnswer(invocation -> {
final UUID accountIdentifier = invocation.getArgument(0, UUID.class);
final Account account = mockAccountsManager.getByAccountIdentifier(accountIdentifier).orElseThrow();
invocation.getArgument(1, Consumer.class).accept(account);
return account;
});
when(mockAccountsManager.update(any(Account.class), any())).thenAnswer(invocation -> {
final Account account = invocation.getArgument(0);
@@ -220,6 +229,7 @@ public class AccountsHelper {
for (Stubbing stubbing : mockingDetails.getStubbings()) {
switch (stubbing.getInvocation().getMethod().getName()) {
case "getAccountIdentifier" -> when(updatedAccount.getAccountIdentifier()).thenAnswer(stubbing);
case "getPhoneNumberIdentifierOptional" -> when(updatedAccount.getPhoneNumberIdentifierOptional()).thenAnswer(stubbing);
case "getPhoneNumberIdentifier" -> when(updatedAccount.getPhoneNumberIdentifier()).thenAnswer(stubbing);
case "getIdentifier" -> when(updatedAccount.getIdentifier(stubbing.getInvocation().getArgument(0))).thenAnswer(stubbing);
case "isIdentifiedBy" -> when(updatedAccount.isIdentifiedBy(stubbing.getInvocation().getArgument(0))).thenAnswer(stubbing);