Only enable setting MFA from numberless accounts

This commit is contained in:
Ravi Khadiwala authored and Ameya Lokare committed 2026-09-10 10:48:19 -07:00
1 parent 5815bb8e3f
commit 78c15d2a9a
3 files changed
+59

No files matched your search

@@ -562,6 +562,11 @@ public class AccountsGrpcService extends SimpleAccountsGrpc.AccountsImplBase {
@Override
public GenerateTotpKeyResponse generateTotpKey(final GenerateTotpKeyRequest request) {
try {
final Account account = getAuthenticatedAccount();
if (account.getNumber().isPresent()) {
throw GrpcExceptions.invalidArguments("TOTP keys may not be generated for an account with a number");
}
final TotpKey pendingTotpKey =
accountsManager.generatePendingTotpKey(AuthenticationUtil.requireAuthenticatedDevice().accountIdentifier());
@@ -585,6 +590,11 @@ public class AccountsGrpcService extends SimpleAccountsGrpc.AccountsImplBase {
@Override
public ConfirmTotpKeyResponse confirmTotpKey(final ConfirmTotpKeyRequest request) {
try {
final Account account = getAuthenticatedAccount();
if (account.getNumber().isPresent()) {
throw GrpcExceptions.invalidArguments("TOTP keys may not be added to an account with a number");
}
final Optional<Byte> maybeConfirmedTotpKeyId =
accountsManager.confirmPendingTotpKey(
AuthenticationUtil.requireAuthenticatedDevice().accountIdentifier(),
@@ -80,11 +80,15 @@ service Accounts {
// Generates and stores a pending TOTP key for the authenticated account.
// To "activate" the key, callers must call the `ConfirmTotpKey` endpoint.
//
// TOTP keys may only be generated for accounts without phone numbers.
rpc GenerateTotpKey(GenerateTotpKeyRequest) returns (GenerateTotpKeyResponse) {}
// Confirms that the caller has stored and can derive one-time passwords from
// a pending TOTP key generated via `GenerateTotpKey` and stores/activates the
// key for the caller's account
//
// TOTP keys may only be generated for accounts without phone numbers.
rpc ConfirmTotpKey(ConfirmTotpKeyRequest) returns (ConfirmTotpKeyResponse) {}
// Returns a list of confirmed MFA keys for the authenticated account
@@ -1130,6 +1130,11 @@ class AccountsGrpcServiceTest extends SimpleBaseGrpcTest<AccountsGrpcService, Ac
void generateTotpKey() throws TooManyTotpKeysException, TooManyMfaKeysException {
final byte[] encodedKey = TestRandomUtil.nextBytes(16);
final Account account = mock(Account.class);
when(accountsManager.getByAccountIdentifier(AUTHENTICATED_ACI))
.thenReturn(Optional.of(account));
when(account.getNumber()).thenReturn(Optional.empty());
when(accountsManager.generatePendingTotpKey(AUTHENTICATED_ACI))
.thenReturn(new TotpKey(TOTP_PARAMETERS, encodedKey));
@@ -1146,6 +1151,10 @@ class AccountsGrpcServiceTest extends SimpleBaseGrpcTest<AccountsGrpcService, Ac
@Test
void generateTotpKeyTooManyKeys() throws TooManyTotpKeysException, TooManyMfaKeysException {
final Account account = mock(Account.class);
when(accountsManager.getByAccountIdentifier(AUTHENTICATED_ACI)).thenReturn(Optional.of(account));
when(account.getNumber()).thenReturn(Optional.empty());
when(accountsManager.generatePendingTotpKey(AUTHENTICATED_ACI))
.thenThrow(TooManyTotpKeysException.class);
@@ -1157,6 +1166,10 @@ class AccountsGrpcServiceTest extends SimpleBaseGrpcTest<AccountsGrpcService, Ac
@Test
void generateTotpKeyTooManyNonTotpKeys() throws TooManyTotpKeysException, TooManyMfaKeysException {
final Account account = mock(Account.class);
when(accountsManager.getByAccountIdentifier(AUTHENTICATED_ACI)).thenReturn(Optional.of(account));
when(account.getNumber()).thenReturn(Optional.empty());
when(accountsManager.generatePendingTotpKey(AUTHENTICATED_ACI))
.thenThrow(TooManyMfaKeysException.class);
@@ -1173,6 +1186,11 @@ class AccountsGrpcServiceTest extends SimpleBaseGrpcTest<AccountsGrpcService, Ac
final int oneTimePassword = 123456;
final byte[] metadataCiphertext = TestRandomUtil.nextBytes(MFA_KEY_METADATA_SIZE);
final Account account = mock(Account.class);
when(accountsManager.getByAccountIdentifier(AUTHENTICATED_ACI))
.thenReturn(Optional.of(account));
when(account.getNumber()).thenReturn(Optional.empty());
when(accountsManager.confirmPendingTotpKey(AUTHENTICATED_ACI, oneTimePassword, testClock.instant(), metadataCiphertext))
.thenReturn(Optional.of(keyId));
@@ -1196,6 +1214,10 @@ class AccountsGrpcServiceTest extends SimpleBaseGrpcTest<AccountsGrpcService, Ac
@Test
void confirmTotpKeyPasswordNotVerified() throws TooManyMfaKeysException {
final Account account = mock(Account.class);
when(accountsManager.getByAccountIdentifier(AUTHENTICATED_ACI)).thenReturn(Optional.of(account));
when(account.getNumber()).thenReturn(Optional.empty());
when(accountsManager.confirmPendingTotpKey(any(), anyInt(), any(), any()))
.thenReturn(Optional.empty());
@@ -1209,6 +1231,11 @@ class AccountsGrpcServiceTest extends SimpleBaseGrpcTest<AccountsGrpcService, Ac
@Test
void confirmTotpKeyTooManyNonTotpKeys() throws TooManyTotpKeysException, TooManyMfaKeysException {
final Account account = mock(Account.class);
when(accountsManager.getByAccountIdentifier(AUTHENTICATED_ACI))
.thenReturn(Optional.of(account));
when(account.getNumber()).thenReturn(Optional.empty());
when(accountsManager.confirmPendingTotpKey(any(), anyInt(), any(), any()))
.thenThrow(TooManyMfaKeysException.class);
@@ -1221,6 +1248,24 @@ class AccountsGrpcServiceTest extends SimpleBaseGrpcTest<AccountsGrpcService, Ac
assertEquals(ConfirmTotpKeyResponse.ResponseCase.TOO_MANY_MFA_KEYS, response.getResponseCase());
}
@Test
void forbidSetMfaOnAccountsWithNumbers() {
final Account account = mock(Account.class);
when(account.getNumber()).thenReturn(Optional.of(
PhoneNumberUtil.getInstance().format(PhoneNumberUtil.getInstance()
.getExampleNumber("US"), PhoneNumberUtil.PhoneNumberFormat.E164)));
when(accountsManager.getByAccountIdentifier(AUTHENTICATED_ACI)).thenReturn(Optional.of(account));
GrpcTestUtils.assertStatusException(Status.INVALID_ARGUMENT, "CONSTRAINT_VIOLATED", () ->
authenticatedServiceStub().generateTotpKey(GenerateTotpKeyRequest.getDefaultInstance()));
GrpcTestUtils.assertStatusException(Status.INVALID_ARGUMENT, "CONSTRAINT_VIOLATED", () ->
authenticatedServiceStub().confirmTotpKey(ConfirmTotpKeyRequest.newBuilder()
.setOneTimePassword(123456)
.setMetadataCiphertext(ByteString.copyFrom(TestRandomUtil.nextBytes(MFA_KEY_METADATA_SIZE)))
.build()));
}
@Test
void listTotpKeys() {