Unify TTL handling of redeemed receipts

This commit is contained in:
Ameya Lokare committed 2026-08-12 14:49:38 -07:00
1 parent ae04fae1b9
commit a8a4a70a68
21 files changed
+154 -82

No files matched your search

@@ -340,7 +340,7 @@ public class BackupAuthManagerTest {
.build();
clock.pin(Instant.EPOCH.plus(Duration.ofDays(1)));
when(accountsManager.update(any(Account.class), any())).thenReturn(account);
when(redeemedReceiptsManager.put(any(), eq(expirationTime.getEpochSecond()), eq(201L), eq(aci)))
when(redeemedReceiptsManager.put(any(), eq(expirationTime), eq(201L), eq(aci)))
.thenReturn(true);
authManager.redeemReceipt(account, receiptPresentation(201, expirationTime));
verify(accountsManager, times(1)).update(any(Account.class), any());
@@ -353,7 +353,7 @@ public class BackupAuthManagerTest {
final Account account = new MockAccountBuilder().mediaCredential(Optional.empty()).build();
clock.pin(Instant.EPOCH.plus(Duration.ofDays(1)));
when(redeemedReceiptsManager.put(any(), eq(expirationTime.getEpochSecond()), eq(201L), eq(aci)))
when(redeemedReceiptsManager.put(any(), eq(expirationTime), eq(201L), eq(aci)))
.thenReturn(true);
assertThatExceptionOfType(BackupMissingIdCommitmentException.class)
.isThrownBy(() -> authManager.redeemReceipt(account, receiptPresentation(201, expirationTime)));
@@ -374,7 +374,7 @@ public class BackupAuthManagerTest {
clock.pin(Instant.EPOCH.plus(Duration.ofDays(1)));
when(accountsManager.update(any(Account.class), any())).thenReturn(account);
when(redeemedReceiptsManager.put(any(), eq(newExpirationTime.getEpochSecond()), eq(201L), eq(aci)))
when(redeemedReceiptsManager.put(any(), eq(newExpirationTime), eq(201L), eq(aci)))
.thenReturn(true);
authManager.redeemReceipt(account, receiptPresentation(201, newExpirationTime));
@@ -429,7 +429,7 @@ public class BackupAuthManagerTest {
clock.pin(Instant.EPOCH.plus(Duration.ofDays(1)));
when(accountsManager.update(any(Account.class), any())).thenReturn(account);
when(redeemedReceiptsManager.put(any(), eq(expirationTime.getEpochSecond()), eq(201L), eq(aci)))
when(redeemedReceiptsManager.put(any(), eq(expirationTime), eq(201L), eq(aci)))
.thenReturn(false);
assertThatExceptionOfType(BackupBadReceiptException.class)
@@ -138,7 +138,7 @@ class DonationControllerTest {
when(receiptCredentialPresentation.getReceiptLevel()).thenReturn(receiptLevel);
final long receiptExpiration = nowEpochSeconds + 86400 * 30;
when(receiptCredentialPresentation.getReceiptExpirationTime()).thenReturn(receiptExpiration);
when(redeemedReceiptsManager.put(same(receiptSerial), eq(receiptExpiration), eq(receiptLevel), eq(AuthHelper.VALID_UUID))).thenReturn(true);
when(redeemedReceiptsManager.put(same(receiptSerial), eq(Instant.ofEpochSecond(receiptExpiration)), eq(receiptLevel), eq(AuthHelper.VALID_UUID))).thenReturn(true);
when(accountsManager.getByAccountIdentifier(eq(AuthHelper.VALID_UUID)))
.thenReturn(Optional.of(AuthHelper.VALID_ACCOUNT));
@@ -163,7 +163,7 @@ class DonationControllerTest {
when(receiptCredentialPresentation.getReceiptLevel()).thenReturn(receiptLevel);
final long receiptExpiration = nowEpochSeconds + 86400 * 30;
when(receiptCredentialPresentation.getReceiptExpirationTime()).thenReturn(receiptExpiration);
when(redeemedReceiptsManager.put(same(receiptSerial), eq(receiptExpiration), eq(receiptLevel), eq(AuthHelper.VALID_UUID))).thenReturn(false);
when(redeemedReceiptsManager.put(same(receiptSerial), eq(Instant.ofEpochSecond(receiptExpiration)), eq(receiptLevel), eq(AuthHelper.VALID_UUID))).thenReturn(false);
when(accountsManager.getByAccountIdentifier(eq(AuthHelper.VALID_UUID)))
.thenReturn(Optional.of(AuthHelper.VALID_ACCOUNT));
@@ -84,7 +84,7 @@ class DonationsGrpcServiceTest extends SimpleBaseGrpcTest<DonationsGrpcService,
private final TestClock clock = TestClock.pinned(Instant.ofEpochSecond(100));
private static final long EXPIRATION_TIME_EPOCH_SECONDS = 200;
private static final Instant EXPIRATION_TIME = Instant.ofEpochSecond(200);
@Override
protected DonationsGrpcService createServiceBeforeEachTest() {
@@ -98,7 +98,7 @@ class DonationsGrpcServiceTest extends SimpleBaseGrpcTest<DonationsGrpcService,
}
when(receiptCredentialPresentation.getReceiptLevel()).thenReturn(ReceiptLevel.ONE_TIME_DONATION.getValue());
when(badgesConfiguration.getReceiptLevels()).thenReturn(Map.of(ReceiptLevel.ONE_TIME_DONATION, "testBadge"));
when(receiptCredentialPresentation.getReceiptExpirationTime()).thenReturn(EXPIRATION_TIME_EPOCH_SECONDS);
when(receiptCredentialPresentation.getReceiptExpirationTime()).thenReturn(EXPIRATION_TIME.getEpochSecond());
when(receiptCredentialPresentation.getReceiptSerial()).thenReturn(receiptSerial);
donationPermitsManager = new DonationPermitsManager(mock(DonationPermits.class), donationsPermitSecretParams,
@@ -123,7 +123,7 @@ class DonationsGrpcServiceTest extends SimpleBaseGrpcTest<DonationsGrpcService,
void redeemReceipt(
@CartesianTest.Values(booleans = {true, false}) final boolean isVisible,
@CartesianTest.Values(booleans = {true, false}) final boolean isPrimary) {
when(redeemedReceiptsManager.put(receiptSerial, EXPIRATION_TIME_EPOCH_SECONDS, 1, AUTHENTICATED_ACI)).thenReturn(
when(redeemedReceiptsManager.put(receiptSerial, EXPIRATION_TIME, 1, AUTHENTICATED_ACI)).thenReturn(
true);
final RedeemReceiptResponse response = authenticatedServiceStub().redeemReceipt(RedeemReceiptRequest.newBuilder()
@@ -135,7 +135,7 @@ class DonationsGrpcServiceTest extends SimpleBaseGrpcTest<DonationsGrpcService,
assertEquals(RedeemReceiptResponse.ResponseCase.SUCCESS, response.getResponseCase());
verify(account).addBadge(clock,
new AccountBadge("testBadge", Instant.ofEpochSecond(EXPIRATION_TIME_EPOCH_SECONDS), isVisible));
new AccountBadge("testBadge", EXPIRATION_TIME, isVisible));
if (isPrimary) {
verify(account).makeBadgePrimaryIfExists(clock, "testBadge");
} else {
@@ -145,7 +145,7 @@ class DonationsGrpcServiceTest extends SimpleBaseGrpcTest<DonationsGrpcService,
@Test
void alreadyRedeemed() {
when(redeemedReceiptsManager.put(receiptSerial, EXPIRATION_TIME_EPOCH_SECONDS, 1, AUTHENTICATED_ACI)).thenReturn(
when(redeemedReceiptsManager.put(receiptSerial, EXPIRATION_TIME, 1, AUTHENTICATED_ACI)).thenReturn(
false);
final RedeemReceiptResponse response = authenticatedServiceStub().redeemReceipt(RedeemReceiptRequest.newBuilder()
@@ -115,7 +115,7 @@ public class AccountCreationDeletionIntegrationTest {
DYNAMO_DB_EXTENSION.getDynamoDbClient(),
DYNAMO_DB_EXTENSION.getDynamoDbAsyncClient(),
new RedeemedReceiptsManager(CLOCK, DynamoDbExtensionSchema.Tables.REDEEMED_RECEIPTS.tableName(),
DYNAMO_DB_EXTENSION.getDynamoDbClient(), Duration.ofDays(30)),
DYNAMO_DB_EXTENSION.getDynamoDbClient()),
DynamoDbExtensionSchema.Tables.ACCOUNTS.tableName(),
DynamoDbExtensionSchema.Tables.NUMBERS.tableName(),
DynamoDbExtensionSchema.Tables.PNI_ASSIGNMENTS.tableName(),
@@ -98,7 +98,7 @@ class AccountsManagerChangeNumberIntegrationTest {
DYNAMO_DB_EXTENSION.getDynamoDbClient(),
DYNAMO_DB_EXTENSION.getDynamoDbAsyncClient(),
new RedeemedReceiptsManager(Clock.systemUTC(), Tables.REDEEMED_RECEIPTS.tableName(),
DYNAMO_DB_EXTENSION.getDynamoDbClient(), Duration.ofDays(30)),
DYNAMO_DB_EXTENSION.getDynamoDbClient()),
Tables.ACCOUNTS.tableName(),
Tables.NUMBERS.tableName(),
Tables.PNI_ASSIGNMENTS.tableName(),
@@ -88,7 +88,7 @@ class AccountsManagerConcurrentModificationIntegrationTest {
DYNAMO_DB_EXTENSION.getDynamoDbClient(),
DYNAMO_DB_EXTENSION.getDynamoDbAsyncClient(),
new RedeemedReceiptsManager(Clock.systemUTC(), Tables.REDEEMED_RECEIPTS.tableName(),
DYNAMO_DB_EXTENSION.getDynamoDbClient(), Duration.ofDays(30)),
DYNAMO_DB_EXTENSION.getDynamoDbClient()),
Tables.ACCOUNTS.tableName(),
Tables.NUMBERS.tableName(),
Tables.PNI_ASSIGNMENTS.tableName(),
@@ -85,7 +85,7 @@ public class AccountsManagerMigrateRecoveryPasswordIntegrationTest {
DYNAMO_DB_EXTENSION.getDynamoDbClient(),
DYNAMO_DB_EXTENSION.getDynamoDbAsyncClient(),
new RedeemedReceiptsManager(Clock.systemUTC(), DynamoDbExtensionSchema.Tables.REDEEMED_RECEIPTS.tableName(),
DYNAMO_DB_EXTENSION.getDynamoDbClient(), Duration.ofDays(30)),
DYNAMO_DB_EXTENSION.getDynamoDbClient()),
DynamoDbExtensionSchema.Tables.ACCOUNTS.tableName(),
DynamoDbExtensionSchema.Tables.NUMBERS.tableName(),
DynamoDbExtensionSchema.Tables.PNI_ASSIGNMENTS.tableName(),
@@ -106,7 +106,7 @@ class AccountsManagerUsernameIntegrationTest {
DYNAMO_DB_EXTENSION.getDynamoDbClient(),
DYNAMO_DB_EXTENSION.getDynamoDbAsyncClient(),
new RedeemedReceiptsManager(Clock.systemUTC(), Tables.REDEEMED_RECEIPTS.tableName(),
DYNAMO_DB_EXTENSION.getDynamoDbClient(), Duration.ofDays(30)),
DYNAMO_DB_EXTENSION.getDynamoDbClient()),
Tables.ACCOUNTS.tableName(),
Tables.NUMBERS.tableName(),
Tables.PNI_ASSIGNMENTS.tableName(),
@@ -147,7 +147,7 @@ class AccountsTest {
DYNAMO_DB_EXTENSION.getDynamoDbClient(),
DYNAMO_DB_EXTENSION.getDynamoDbAsyncClient(),
new RedeemedReceiptsManager(clock, Tables.REDEEMED_RECEIPTS.tableName(),
DYNAMO_DB_EXTENSION.getDynamoDbClient(), Duration.ofDays(30)),
DYNAMO_DB_EXTENSION.getDynamoDbClient()),
Tables.ACCOUNTS.tableName(),
Tables.NUMBERS.tableName(),
Tables.PNI_ASSIGNMENTS.tableName(),
@@ -880,7 +880,7 @@ class AccountsTest {
dynamoDbClient,
mock(DynamoDbAsyncClient.class),
new RedeemedReceiptsManager(clock, Tables.REDEEMED_RECEIPTS.tableName(),
dynamoDbClient, Duration.ofDays(30)),
dynamoDbClient),
Tables.ACCOUNTS.tableName(),
Tables.NUMBERS.tableName(),
Tables.PNI_ASSIGNMENTS.tableName(),
@@ -986,7 +986,7 @@ class AccountsTest {
dynamoDbClient,
mock(DynamoDbAsyncClient.class),
new RedeemedReceiptsManager(clock, Tables.REDEEMED_RECEIPTS.tableName(),
dynamoDbClient, Duration.ofDays(30)),
dynamoDbClient),
Tables.ACCOUNTS.tableName(),
Tables.NUMBERS.tableName(),
Tables.PNI_ASSIGNMENTS.tableName(),
@@ -1444,7 +1444,7 @@ class AccountsTest {
dynamoDbClient,
mock(DynamoDbAsyncClient.class),
new RedeemedReceiptsManager(clock, Tables.REDEEMED_RECEIPTS.tableName(),
dynamoDbClient, Duration.ofDays(30)),
dynamoDbClient),
Tables.ACCOUNTS.tableName(),
Tables.NUMBERS.tableName(),
Tables.PNI_ASSIGNMENTS.tableName(),
@@ -1492,7 +1492,7 @@ class AccountsTest {
dynamoDbClient,
mock(DynamoDbAsyncClient.class),
new RedeemedReceiptsManager(clock, Tables.REDEEMED_RECEIPTS.tableName(),
dynamoDbClient, Duration.ofDays(30)),
dynamoDbClient),
Tables.ACCOUNTS.tableName(),
Tables.NUMBERS.tableName(),
Tables.PNI_ASSIGNMENTS.tableName(),
@@ -1598,7 +1598,7 @@ class AccountsTest {
dynamoDbClient,
mock(DynamoDbAsyncClient.class),
new RedeemedReceiptsManager(clock, Tables.REDEEMED_RECEIPTS.tableName(),
dynamoDbClient, Duration.ofDays(30)),
dynamoDbClient),
Tables.ACCOUNTS.tableName(),
Tables.NUMBERS.tableName(),
Tables.PNI_ASSIGNMENTS.tableName(),
@@ -2378,9 +2378,9 @@ class AccountsTest {
.build()
);
assertThat(receiptConstraintResponse.hasItem()).isTrue();
assertThat(AttributeValues.getUUID(receiptConstraintResponse.item(), RedeemedReceiptsManager.KEY_ACCOUNT_UUID, null)).isEqualTo(accountId);
assertThat(AttributeValues.getLong(receiptConstraintResponse.item(), RedeemedReceiptsManager.KEY_RECEIPT_LEVEL, -1)).isEqualTo(receiptCredentialPresentation.getReceiptLevel());
assertThat(AttributeValues.getLong(receiptConstraintResponse.item(), RedeemedReceiptsManager.KEY_RECEIPT_EXPIRATION, -1)).isEqualTo(receiptCredentialPresentation.getReceiptExpirationTime());
assertThat(AttributeValues.getUUID(receiptConstraintResponse.item(), RedeemedReceiptsManager.ATTR_ACCOUNT_UUID, null)).isEqualTo(accountId);
assertThat(AttributeValues.getLong(receiptConstraintResponse.item(), RedeemedReceiptsManager.ATTR_RECEIPT_LEVEL, -1)).isEqualTo(receiptCredentialPresentation.getReceiptLevel());
assertThat(AttributeValues.getLong(receiptConstraintResponse.item(), RedeemedReceiptsManager.ATTR_RECEIPT_EXPIRATION, -1)).isEqualTo(receiptCredentialPresentation.getReceiptExpirationTime());
}
private Map<String, AttributeValue> readAccount(final UUID uuid) {
@@ -102,7 +102,7 @@ public class AddRemoveDeviceIntegrationTest {
DYNAMO_DB_EXTENSION.getDynamoDbClient(),
DYNAMO_DB_EXTENSION.getDynamoDbAsyncClient(),
new RedeemedReceiptsManager(clock, DynamoDbExtensionSchema.Tables.REDEEMED_RECEIPTS.tableName(),
DYNAMO_DB_EXTENSION.getDynamoDbClient(), Duration.ofDays(30)),
DYNAMO_DB_EXTENSION.getDynamoDbClient()),
DynamoDbExtensionSchema.Tables.ACCOUNTS.tableName(),
DynamoDbExtensionSchema.Tables.NUMBERS.tableName(),
DynamoDbExtensionSchema.Tables.PNI_ASSIGNMENTS.tableName(),
@@ -6,20 +6,29 @@
package org.whispersystems.textsecuregcm.storage;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.ExecutionException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.signal.libsignal.zkgroup.InvalidInputException;
import org.signal.libsignal.zkgroup.receipts.ReceiptSerial;
import org.whispersystems.textsecuregcm.storage.DynamoDbExtensionSchema.Tables;
import org.whispersystems.textsecuregcm.tests.util.AuthHelper;
import org.whispersystems.textsecuregcm.util.AttributeValues;
import org.whispersystems.textsecuregcm.util.TestClock;
import org.whispersystems.textsecuregcm.util.TestRandomUtil;
import software.amazon.awssdk.services.dynamodb.model.CancellationReason;
import software.amazon.awssdk.services.dynamodb.model.GetItemRequest;
import software.amazon.awssdk.services.dynamodb.model.GetItemResponse;
import software.amazon.awssdk.services.dynamodb.model.TransactWriteItem;
import software.amazon.awssdk.services.dynamodb.model.TransactWriteItemsRequest;
import software.amazon.awssdk.services.dynamodb.model.TransactionCanceledException;
class RedeemedReceiptsManagerTest {
@@ -38,30 +47,95 @@ class RedeemedReceiptsManagerTest {
redeemedReceiptsManager = new RedeemedReceiptsManager(
clock,
Tables.REDEEMED_RECEIPTS.tableName(),
DYNAMO_DB_EXTENSION.getDynamoDbClient(),
Duration.ofDays(90));
DYNAMO_DB_EXTENSION.getDynamoDbClient());
}
@Test
void testPut() throws ExecutionException, InterruptedException {
final long receiptExpiration = 42;
void testPut() {
final Instant receiptExpiration = Instant.ofEpochSecond(42);
final long receiptLevel = 3;
final UUID uuid1 = UUID.randomUUID();
boolean put;
// initial insert should return true
put = redeemedReceiptsManager.put(receiptSerial, receiptExpiration, receiptLevel, AuthHelper.VALID_UUID);
put = redeemedReceiptsManager.put(receiptSerial, receiptExpiration, receiptLevel, uuid1);
assertThat(put).isTrue();
// subsequent attempted inserts with modified parameters should return false
put = redeemedReceiptsManager.put(receiptSerial, receiptExpiration + 1, receiptLevel, AuthHelper.VALID_UUID);
put = redeemedReceiptsManager.put(receiptSerial, receiptExpiration.plusSeconds(1), receiptLevel, uuid1);
assertThat(put).isFalse();
put = redeemedReceiptsManager.put(receiptSerial, receiptExpiration, receiptLevel + 1, AuthHelper.VALID_UUID);
put = redeemedReceiptsManager.put(receiptSerial, receiptExpiration, receiptLevel + 1, uuid1);
assertThat(put).isFalse();
put = redeemedReceiptsManager.put(receiptSerial, receiptExpiration, receiptLevel, AuthHelper.VALID_UUID_TWO);
final UUID uuid2 = UUID.randomUUID();
put = redeemedReceiptsManager.put(receiptSerial, receiptExpiration, receiptLevel, uuid2);
assertThat(put).isFalse();
// repeated insert attempt of the original parameters should return true
put = redeemedReceiptsManager.put(receiptSerial, receiptExpiration, receiptLevel, AuthHelper.VALID_UUID);
put = redeemedReceiptsManager.put(receiptSerial, receiptExpiration, receiptLevel, uuid1);
assertThat(put).isTrue();
// verify that the TTL is receipt expiration + padding
final GetItemResponse response = DYNAMO_DB_EXTENSION.getDynamoDbClient().getItem(GetItemRequest.builder()
.tableName(Tables.REDEEMED_RECEIPTS.tableName())
.key(Map.of(RedeemedReceiptsManager.KEY_SERIAL, AttributeValues.b(receiptSerial.serialize())))
.build());
assertThat(Long.parseLong(response.item().get(RedeemedReceiptsManager.ATTR_TTL).n())).isEqualTo(
receiptExpiration.plus(RedeemedReceiptsManager.TTL_PADDING).getEpochSecond());
}
@Test
void testBuildTransactWriteItemForReceipt() {
final Instant receiptExpiration = Instant.ofEpochSecond(42);
final long receiptLevel1 = 3;
final UUID uuid1 = UUID.randomUUID();
final TransactWriteItem writeItem = redeemedReceiptsManager.buildTransactWriteItemForReceipt(receiptSerial,
receiptExpiration, receiptLevel1, uuid1);
assertThatCode(() -> DYNAMO_DB_EXTENSION.getDynamoDbClient().transactWriteItems(TransactWriteItemsRequest.builder()
.transactItems(writeItem)
.build())).doesNotThrowAnyException();
// A subsequent write with the same parameters should be idempotent
assertThatCode(() -> DYNAMO_DB_EXTENSION.getDynamoDbClient().transactWriteItems(TransactWriteItemsRequest.builder()
.transactItems(writeItem)
.build())).doesNotThrowAnyException();
// An attempt to insert the same receipt with a different UUID should fail
final UUID uuid2 = UUID.randomUUID();
final TransactWriteItem writeItem2 = redeemedReceiptsManager.buildTransactWriteItemForReceipt(receiptSerial,
receiptExpiration, receiptLevel1, uuid2);
assertThatThrownBy(
() -> DYNAMO_DB_EXTENSION.getDynamoDbClient().transactWriteItems(TransactWriteItemsRequest.builder()
.transactItems(writeItem2)
.build()))
.isInstanceOfSatisfying(TransactionCanceledException.class, e -> {
final List<CancellationReason> cancellationReasons = e.cancellationReasons();
assertThat(cancellationReasons).hasSize(1);
assertThat(cancellationReasons.getFirst().code()).isEqualTo("ConditionalCheckFailed");
});
// An attempt to insert the same receipt with the original UUID but a different receipt level should fail
final long receiptLevel2 = 4;
final TransactWriteItem writeItem3 = redeemedReceiptsManager.buildTransactWriteItemForReceipt(receiptSerial,
receiptExpiration, receiptLevel2, uuid1);
assertThatThrownBy(
() -> DYNAMO_DB_EXTENSION.getDynamoDbClient().transactWriteItems(TransactWriteItemsRequest.builder()
.transactItems(writeItem3)
.build()))
.isInstanceOfSatisfying(TransactionCanceledException.class, e -> {
final List<CancellationReason> cancellationReasons = e.cancellationReasons();
assertThat(cancellationReasons).hasSize(1);
assertThat(cancellationReasons.getFirst().code()).isEqualTo("ConditionalCheckFailed");
});
// verify that the TTL is receipt expiration + padding
final GetItemResponse response = DYNAMO_DB_EXTENSION.getDynamoDbClient().getItem(GetItemRequest.builder()
.tableName(Tables.REDEEMED_RECEIPTS.tableName())
.key(Map.of(RedeemedReceiptsManager.KEY_SERIAL, AttributeValues.b(receiptSerial.serialize())))
.build());
assertThat(Long.parseLong(response.item().get(RedeemedReceiptsManager.ATTR_TTL).n())).isEqualTo(
receiptExpiration.plus(RedeemedReceiptsManager.TTL_PADDING).getEpochSecond());
}
}
@@ -144,7 +144,6 @@ dynamoDbTables:
tableName: Example_PushNotificationExperimentSamples
redeemedReceipts:
tableName: redeemed_receipts_test
expiration: P30D # Duration of time until rows expire
registrationRecovery:
tableName: registration_recovery_passwords_test
expiration: P300D # Duration of time until rows expire