Don't assume presence of phone numbers when generating account identity responses

This commit is contained in:
Jon Chambers authored and Jon Chambers committed 2026-07-29 14:41:26 -04:00
1 parent 79935ea22a
commit ac4d61978b
8 files changed
+41 -18

No files matched your search

@@ -112,7 +112,7 @@ public final class Operations {
.executeExpectSuccess(AccountIdentityResponse.class);
user.setAciUuid(registrationResponse.uuid());
user.setPniUuid(registrationResponse.pni());
registrationResponse.pni().ifPresent(user::setPniUuid);
return user;
}
@@ -18,18 +18,18 @@ public class AccountIdentityResponseBuilder {
private boolean storageCapable;
private Clock clock;
public AccountIdentityResponseBuilder(Account account) {
public AccountIdentityResponseBuilder(final Account account) {
this.account = account;
this.storageCapable = account.hasCapability(DeviceCapability.STORAGE);
this.clock = Clock.systemUTC();
}
public AccountIdentityResponseBuilder storageCapable(boolean storageCapable) {
public AccountIdentityResponseBuilder storageCapable(final boolean storageCapable) {
this.storageCapable = storageCapable;
return this;
}
public AccountIdentityResponseBuilder clock(Clock clock) {
public AccountIdentityResponseBuilder clock(final Clock clock) {
this.clock = clock;
return this;
}
@@ -48,8 +48,8 @@ public class AccountIdentityResponseBuilder {
.orElse(null);
return new AccountIdentityResponse(account.getAccountIdentifier(),
account.getNumber(),
account.getPhoneNumberIdentifier(),
account.getNumberOptional(),
account.getPhoneNumberIdentifierOptional(),
account.getUsernameHash().filter(h -> h.length > 0).orElse(null),
account.getUsernameLinkHandle(),
storageCapable,
@@ -8,6 +8,7 @@ package org.whispersystems.textsecuregcm.entities;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import io.swagger.v3.oas.annotations.media.Schema;
import java.util.Optional;
import java.util.UUID;
import javax.annotation.Nullable;
import org.whispersystems.textsecuregcm.util.ByteArrayBase64UrlAdapter;
@@ -16,11 +17,11 @@ public record AccountIdentityResponse(
@Schema(description = "the account identifier for this account")
UUID uuid,
@Schema(description = "the phone number associated with this account")
String number,
@Schema(description = "the phone number associated with this account; may be null if this account does not have a phone number")
Optional<String> number,
@Schema(description = "the account identifier for this account's phone-number identity")
UUID pni,
@Schema(description = "the account identifier for this account's phone-number identity; may be null if this account does not have a phone number")
Optional<UUID> pni,
@Schema(description = "a hash of this account's username, if set")
@JsonSerialize(using = ByteArrayBase64UrlAdapter.Serializing.class)
@@ -516,9 +516,13 @@ public class AccountsGrpcService extends SimpleAccountsGrpc.AccountsImplBase {
private static AccountIdentifiers buildAccountIdentifiers(final Account account) {
final AccountIdentifiers.Builder accountIdentifiersBuilder = AccountIdentifiers.newBuilder()
.addServiceIdentifiers(GrpcServiceIdentifierUtil.toGrpcServiceIdentifier(new AciServiceIdentifier(account.getAccountIdentifier())))
.addServiceIdentifiers(GrpcServiceIdentifierUtil.toGrpcServiceIdentifier(new PniServiceIdentifier(account.getPhoneNumberIdentifier())))
.setE164(account.getNumber());
.addServiceIdentifiers(GrpcServiceIdentifierUtil.toGrpcServiceIdentifier(new AciServiceIdentifier(account.getAccountIdentifier())));
account.getPhoneNumberIdentifierOptional()
.map(phoneNumberIdentifier -> GrpcServiceIdentifierUtil.toGrpcServiceIdentifier(new PniServiceIdentifier(phoneNumberIdentifier)))
.ifPresent(accountIdentifiersBuilder::addServiceIdentifiers);
account.getNumberOptional().ifPresent(accountIdentifiersBuilder::setE164);
account.getUsernameHash().ifPresent(usernameHash ->
accountIdentifiersBuilder.setUsernameHash(ByteString.copyFrom(usernameHash)));
@@ -28,10 +28,11 @@ message ServiceIdentifier {
// All identifiers associated with an account.
message AccountIdentifiers {
// A list of service identifiers for the identified account. Always includes
// exactly one ACI service identifier and one PNI service identifier.
// exactly one ACI service identifier and at most one PNI service identifier.
repeated ServiceIdentifier service_identifiers = 1;
// The phone number associated with the identified account.
// The phone number associated with the identified account. May be empty if
// the given account does not have a phone number.
string e164 = 2 [(org.signal.chat.require.e164) = true];
// The username hash (if any) associated with the identified account. May be
@@ -128,11 +128,16 @@ class AccountControllerV2Test {
final Account updatedAccount = mock(Account.class);
when(updatedAccount.getAccountIdentifier()).thenReturn(uuid);
when(updatedAccount.getNumber()).thenReturn(number);
when(updatedAccount.getNumberOptional()).thenReturn(Optional.of(number));
when(updatedAccount.getIdentityKey(IdentityType.PNI)).thenReturn(pniIdentityKey);
if (number.equals(account.getNumber())) {
when(updatedAccount.getPhoneNumberIdentifier()).thenReturn(AuthHelper.VALID_PNI);
when(updatedAccount.getPhoneNumberIdentifierOptional()).thenReturn(Optional.of(AuthHelper.VALID_PNI));
} else {
when(updatedAccount.getPhoneNumberIdentifier()).thenReturn(UUID.randomUUID());
final UUID pni = UUID.randomUUID();
when(updatedAccount.getPhoneNumberIdentifier()).thenReturn(pni);
when(updatedAccount.getPhoneNumberIdentifierOptional()).thenReturn(Optional.of(pni));
}
when(updatedAccount.getDevices()).thenReturn(devices);
@@ -169,8 +174,8 @@ class AccountControllerV2Test {
any(), any(), any(), any(), any(), any(), any());
assertEquals(AuthHelper.VALID_UUID, accountIdentityResponse.uuid());
assertEquals(NEW_NUMBER, accountIdentityResponse.number());
assertNotEquals(AuthHelper.VALID_PNI, accountIdentityResponse.pni());
assertEquals(Optional.of(NEW_NUMBER), accountIdentityResponse.number());
assertNotEquals(Optional.of(AuthHelper.VALID_PNI), accountIdentityResponse.pni());
}
@Test
@@ -172,7 +172,9 @@ 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);
@@ -829,7 +831,9 @@ 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());
when(changeNumberManager.changeNumber(eq(AUTHENTICATED_ACI), any(), any(), any(), eq(newNumber),
@@ -158,23 +158,31 @@ public class AuthHelper {
when(VALID_ACCOUNT_3.getDevices()).thenReturn(List.of(VALID_DEVICE_3_PRIMARY, VALID_DEVICE_3_LINKED));
when(VALID_ACCOUNT.getNumber()).thenReturn(VALID_NUMBER);
when(VALID_ACCOUNT.getNumberOptional()).thenReturn(Optional.of(VALID_NUMBER));
when(VALID_ACCOUNT.getAccountIdentifier()).thenReturn(VALID_UUID);
when(VALID_ACCOUNT.getPhoneNumberIdentifier()).thenReturn(VALID_PNI);
when(VALID_ACCOUNT.getPhoneNumberIdentifierOptional()).thenReturn(Optional.of(VALID_PNI));
when(VALID_ACCOUNT.getIdentifier(IdentityType.ACI)).thenReturn(VALID_UUID);
when(VALID_ACCOUNT.getIdentifier(IdentityType.PNI)).thenReturn(VALID_PNI);
when(VALID_ACCOUNT_TWO.getNumber()).thenReturn(VALID_NUMBER_TWO);
when(VALID_ACCOUNT_TWO.getNumberOptional()).thenReturn(Optional.of(VALID_NUMBER_TWO));
when(VALID_ACCOUNT_TWO.getAccountIdentifier()).thenReturn(VALID_UUID_TWO);
when(VALID_ACCOUNT_TWO.getPhoneNumberIdentifier()).thenReturn(VALID_PNI_TWO);
when(VALID_ACCOUNT_TWO.getPhoneNumberIdentifierOptional()).thenReturn(Optional.of(VALID_PNI_TWO));
when(VALID_ACCOUNT_TWO.getIdentifier(IdentityType.ACI)).thenReturn(VALID_UUID_TWO);
when(VALID_ACCOUNT_TWO.getIdentifier(IdentityType.PNI)).thenReturn(VALID_PNI_TWO);
when(UNDISCOVERABLE_ACCOUNT.getNumber()).thenReturn(UNDISCOVERABLE_NUMBER);
when(UNDISCOVERABLE_ACCOUNT.getNumberOptional()).thenReturn(Optional.of(UNDISCOVERABLE_NUMBER));
when(UNDISCOVERABLE_ACCOUNT.getAccountIdentifier()).thenReturn(UNDISCOVERABLE_UUID);
when(UNDISCOVERABLE_ACCOUNT.getPhoneNumberIdentifier()).thenReturn(UNDISCOVERABLE_PNI);
when(UNDISCOVERABLE_ACCOUNT.getPhoneNumberIdentifierOptional()).thenReturn(Optional.of(UNDISCOVERABLE_PNI));
when(UNDISCOVERABLE_ACCOUNT.getIdentifier(IdentityType.ACI)).thenReturn(UNDISCOVERABLE_UUID);
when(UNDISCOVERABLE_ACCOUNT.getIdentifier(IdentityType.PNI)).thenReturn(UNDISCOVERABLE_PNI);
when(VALID_ACCOUNT_3.getNumber()).thenReturn(VALID_NUMBER_3);
when(VALID_ACCOUNT_3.getNumberOptional()).thenReturn(Optional.of(VALID_NUMBER_3));
when(VALID_ACCOUNT_3.getAccountIdentifier()).thenReturn(VALID_UUID_3);
when(VALID_ACCOUNT_3.getPhoneNumberIdentifier()).thenReturn(VALID_PNI_3);
when(VALID_ACCOUNT_3.getPhoneNumberIdentifierOptional()).thenReturn(Optional.of(VALID_PNI_3));
when(VALID_ACCOUNT_3.getIdentifier(IdentityType.ACI)).thenReturn(VALID_UUID_3);
when(VALID_ACCOUNT_3.getIdentifier(IdentityType.PNI)).thenReturn(VALID_PNI_3);