mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-11-30 01:06:50 +00:00
Calculate the IssuedReceipt TTL from the receipt expiration
This commit is contained in:
1 parent
6e3106297b
commit
3e5ccb82de
14 files changed
+107
-97
No files matched your search
@@ -105,7 +105,6 @@ dynamoDbTables:
|
||||
expiration: P7D # Duration of time until rows expire
|
||||
issuedReceipts:
|
||||
tableName: Example_IssuedReceipts
|
||||
expiration: P30D # Duration of time until rows expire
|
||||
generator: abcdefg12345678= # random base64-encoded binary sequence
|
||||
maxReceiptsPerSubscriptionPayment:
|
||||
STRIPE: 1
|
||||
|
||||
@@ -810,7 +810,6 @@ public class WhisperServerService extends Application<WhisperServerConfiguration
|
||||
ProvisioningManager provisioningManager = new ProvisioningManager(pubsubClient);
|
||||
IssuedReceiptsManager issuedReceiptsManager = new IssuedReceiptsManager(
|
||||
config.getDynamoDbTables().getIssuedReceipts().getTableName(),
|
||||
config.getDynamoDbTables().getIssuedReceipts().getExpiration(),
|
||||
dynamoDbClient,
|
||||
config.getDynamoDbTables().getIssuedReceipts().getGenerator(),
|
||||
config.getDynamoDbTables().getIssuedReceipts().getMaxReceiptsPerSubscriptionPayment());
|
||||
@@ -996,8 +995,7 @@ public class WhisperServerService extends Application<WhisperServerConfiguration
|
||||
PaymentProvider.GOOGLE_PLAY_BILLING, googlePlayBillingManager),
|
||||
issuedReceiptsManager,
|
||||
zkReceiptOperations,
|
||||
config.getLoginPurchase().level(),
|
||||
clock);
|
||||
config.getLoginPurchase().level());
|
||||
|
||||
final List<SpamFilter> spamFilters = ServiceLoader.load(SpamFilter.class)
|
||||
.stream()
|
||||
|
||||
+4
-6
@@ -7,13 +7,12 @@ package org.whispersystems.textsecuregcm.configuration;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import org.whispersystems.textsecuregcm.subscriptions.PaymentProvider;
|
||||
import org.whispersystems.textsecuregcm.util.EnumMapUtil;
|
||||
import java.time.Duration;
|
||||
import java.util.EnumMap;
|
||||
import java.util.Map;
|
||||
import org.whispersystems.textsecuregcm.subscriptions.PaymentProvider;
|
||||
import org.whispersystems.textsecuregcm.util.EnumMapUtil;
|
||||
|
||||
public class IssuedReceiptsTableConfiguration extends DynamoDbTables.TableWithExpiration {
|
||||
public class IssuedReceiptsTableConfiguration extends DynamoDbTables.Table {
|
||||
|
||||
private final byte[] generator;
|
||||
|
||||
@@ -22,10 +21,9 @@ public class IssuedReceiptsTableConfiguration extends DynamoDbTables.TableWithEx
|
||||
|
||||
public IssuedReceiptsTableConfiguration(
|
||||
@JsonProperty("tableName") final String tableName,
|
||||
@JsonProperty("expiration") final Duration expiration,
|
||||
@JsonProperty("generator") final byte[] generator,
|
||||
@JsonProperty("maxReceiptsPerSubscriptionPayment") final Map<PaymentProvider, Integer> maxReceiptsPerSubscriptionPayment) {
|
||||
super(tableName, expiration);
|
||||
super(tableName);
|
||||
this.generator = generator;
|
||||
this.maxReceiptsPerSubscriptionPayment = EnumMapUtil.toCompleteEnumMap(PaymentProvider.class, maxReceiptsPerSubscriptionPayment);
|
||||
}
|
||||
|
||||
+6
-6
@@ -383,17 +383,17 @@ public class OneTimeDonationController {
|
||||
} catch (final InvalidInputException e) {
|
||||
throw new BadRequestException("invalid receipt credential request", e);
|
||||
}
|
||||
try {
|
||||
issuedReceiptsManager.recordOneTimeIssuance(paymentDetails.id(), request.processor,
|
||||
receiptCredentialRequest, clock.instant());
|
||||
} catch (WriteConflictException _) {
|
||||
throw new WebApplicationException(Response.Status.CONFLICT);
|
||||
}
|
||||
final Instant paidAt = oneTimeDonationsManager.getPaidAt(request.processor, paymentDetails.id(), paymentDetails.created());
|
||||
final Instant expiration = paidAt
|
||||
.plus(levelDetails.levelExpiration())
|
||||
.truncatedTo(ChronoUnit.DAYS)
|
||||
.plus(1, ChronoUnit.DAYS);
|
||||
try {
|
||||
issuedReceiptsManager.recordOneTimeIssuance(paymentDetails.id(), request.processor,
|
||||
receiptCredentialRequest, expiration);
|
||||
} catch (WriteConflictException _) {
|
||||
throw new WebApplicationException(Response.Status.CONFLICT);
|
||||
}
|
||||
final ReceiptCredentialResponse receiptCredentialResponse;
|
||||
try {
|
||||
receiptCredentialResponse = zkReceiptOperations.issueReceiptCredential(
|
||||
|
||||
+8
-8
@@ -303,20 +303,20 @@ public class OneTimeDonationsGrpcService extends SimpleOneTimeDonationsGrpc.OneT
|
||||
throw GrpcExceptions.fieldViolation("receipt_credential_request", "invalid receipt credential request");
|
||||
}
|
||||
|
||||
try {
|
||||
issuedReceiptsManager.recordOneTimeIssuance(
|
||||
paymentDetails.id(), processor, receiptCredentialRequest, clock.instant());
|
||||
} catch (final WriteConflictException e) {
|
||||
return CreateBoostReceiptCredentialsResponse.newBuilder()
|
||||
.setReceiptAlreadyIssued(FailedPrecondition.getDefaultInstance()).build();
|
||||
}
|
||||
|
||||
final Instant paidAt = oneTimeDonationsManager.getPaidAt(processor, paymentDetails.id(), paymentDetails.created());
|
||||
final Instant expiration = paidAt
|
||||
.plus(levelDetails.levelExpiration())
|
||||
.truncatedTo(ChronoUnit.DAYS)
|
||||
.plus(1, ChronoUnit.DAYS);
|
||||
|
||||
try {
|
||||
issuedReceiptsManager.recordOneTimeIssuance(
|
||||
paymentDetails.id(), processor, receiptCredentialRequest, expiration);
|
||||
} catch (final WriteConflictException e) {
|
||||
return CreateBoostReceiptCredentialsResponse.newBuilder()
|
||||
.setReceiptAlreadyIssued(FailedPrecondition.getDefaultInstance()).build();
|
||||
}
|
||||
|
||||
final ReceiptCredentialResponse receiptCredentialResponse;
|
||||
try {
|
||||
receiptCredentialResponse = zkReceiptOperations.issueReceiptCredential(
|
||||
|
||||
+22
-15
@@ -40,34 +40,41 @@ public class IssuedReceiptsManager {
|
||||
public static final String KEY_EXPIRATION = "E"; // N
|
||||
public static final String KEY_ISSUED_RECEIPT_TAG_SET = "T"; // BS
|
||||
|
||||
// An issued receipt will stay in the table until the receipt's expiration + TTL_PADDING
|
||||
@VisibleForTesting
|
||||
static final Duration TTL_PADDING = Duration.ofDays(30);
|
||||
|
||||
private final String table;
|
||||
private final Duration expiration;
|
||||
private final DynamoDbClient dynamoDbClient;
|
||||
private final byte[] receiptTagGenerator;
|
||||
private final EnumMap<PaymentProvider, Integer> maxReceiptsPerSubscriptionPayment;
|
||||
|
||||
public IssuedReceiptsManager(
|
||||
@Nonnull final String table,
|
||||
@Nonnull final Duration expiration,
|
||||
@Nonnull final DynamoDbClient dynamoDbClient,
|
||||
@Nonnull final byte[] receiptTagGenerator,
|
||||
@Nonnull final EnumMap<PaymentProvider, Integer> maxReceiptsPerSubscriptionPayment) {
|
||||
this.table = Objects.requireNonNull(table);
|
||||
this.expiration = Objects.requireNonNull(expiration);
|
||||
this.dynamoDbClient = Objects.requireNonNull(dynamoDbClient);
|
||||
this.receiptTagGenerator = Objects.requireNonNull(receiptTagGenerator);
|
||||
this.maxReceiptsPerSubscriptionPayment = Objects.requireNonNull(maxReceiptsPerSubscriptionPayment);
|
||||
}
|
||||
|
||||
/// Records the issuance of a receipt credential for a one-time purchases.
|
||||
/// Records the issuance of a receipt credential for a one-time purchase.
|
||||
///
|
||||
/// Same as [#recordIssuance] except one-time purchases never allow multiple receipts on a single payment
|
||||
///
|
||||
/// @param processorItemId The identifier of the item within the processor
|
||||
/// @param processor The processor used
|
||||
/// @param request The [ReceiptCredentialRequest] to generate a receipt for. Subsequent retries for the same
|
||||
/// `processorItemId` should use the same request
|
||||
/// @param receiptExpiration When the corresponding issued receipt will expire
|
||||
public void recordOneTimeIssuance(
|
||||
final String processorItemId,
|
||||
final PaymentProvider processor,
|
||||
final ReceiptCredentialRequest request,
|
||||
final Instant now) throws WriteConflictException {
|
||||
recordIssuance(processorItemId, processor, request, 1, now);
|
||||
final Instant receiptExpiration) throws WriteConflictException {
|
||||
recordIssuance(processorItemId, processor, request, 1, receiptExpiration);
|
||||
}
|
||||
|
||||
/// Returns normally if either this processor item was never issued a receipt credential
|
||||
@@ -80,17 +87,17 @@ public class IssuedReceiptsManager {
|
||||
/// For [PaymentProvider#STRIPE], item is expected to refer to an invoice line item (subscriptions) or a payment
|
||||
/// intent (one-time).
|
||||
///
|
||||
/// @param processorItemId The identifier of the item within the processor
|
||||
/// @param processor The processor used
|
||||
/// @param request The [ReceiptCredentialRequest] to generate a receipt for. Subsequent retries for the same
|
||||
/// `processorItemId` should use the same request
|
||||
/// @param now The current time
|
||||
/// @param processorItemId The identifier of the item within the processor
|
||||
/// @param processor The processor used
|
||||
/// @param request The [ReceiptCredentialRequest] to generate a receipt for. Subsequent retries for the same
|
||||
/// `processorItemId` should use the same request
|
||||
/// @param receiptExpiration When the corresponding issued receipt will expire
|
||||
public void recordIssuance(
|
||||
final String processorItemId,
|
||||
final PaymentProvider processor,
|
||||
final ReceiptCredentialRequest request,
|
||||
final Instant now) throws WriteConflictException {
|
||||
recordIssuance(processorItemId, processor, request, maxReceiptsPerSubscriptionPayment.get(processor), now);
|
||||
final Instant receiptExpiration) throws WriteConflictException {
|
||||
recordIssuance(processorItemId, processor, request, maxReceiptsPerSubscriptionPayment.get(processor), receiptExpiration);
|
||||
}
|
||||
|
||||
private void recordIssuance(
|
||||
@@ -98,7 +105,7 @@ public class IssuedReceiptsManager {
|
||||
final PaymentProvider processor,
|
||||
final ReceiptCredentialRequest request,
|
||||
final int maxReceipts,
|
||||
final Instant now) throws WriteConflictException {
|
||||
final Instant receiptExpiration) throws WriteConflictException {
|
||||
|
||||
final AttributeValue key = dynamoDbKey(processor, processorItemId);
|
||||
final byte[] tag = generateIssuedReceiptTag(request);
|
||||
@@ -115,7 +122,7 @@ public class IssuedReceiptsManager {
|
||||
.expressionAttributeValues(Map.of(
|
||||
":tag", b(tag),
|
||||
":singletonTag", AttributeValue.fromBs(List.of(SdkBytes.fromByteArray(tag))),
|
||||
":exp", n(now.plus(expiration).getEpochSecond()),
|
||||
":exp", n(receiptExpiration.plus(TTL_PADDING).getEpochSecond()),
|
||||
":maxTags", n(maxReceipts)))
|
||||
.build();
|
||||
try {
|
||||
|
||||
+3
-3
@@ -12,7 +12,6 @@ import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.annotation.Nonnull;
|
||||
@@ -211,13 +210,14 @@ public class SubscriptionManager {
|
||||
final PaymentProvider processor = record.getProcessorCustomer().orElseThrow().processor();
|
||||
final SubscriptionPaymentProcessor manager = getProcessor(processor);
|
||||
final SubscriptionPaymentProcessor.ReceiptItem receipt = manager.getReceiptItem(record.subscriptionId);
|
||||
final Instant expirationInstant = expiration.apply(receipt);
|
||||
final ReceiptCredentialResponse receiptCredentialResponse;
|
||||
try {
|
||||
issuedReceiptsManager
|
||||
.recordIssuance(receipt.itemId(), manager.getProvider(), receiptCredentialRequest, subscriberCredentials.now());
|
||||
.recordIssuance(receipt.itemId(), manager.getProvider(), receiptCredentialRequest, expirationInstant);
|
||||
receiptCredentialResponse = zkReceiptOperations.issueReceiptCredential(
|
||||
receiptCredentialRequest,
|
||||
expiration.apply(receipt).getEpochSecond(),
|
||||
expirationInstant.getEpochSecond(),
|
||||
receipt.level());
|
||||
} catch (final VerificationFailedException e) {
|
||||
throw new SubscriptionInvalidArgumentsException("receipt credential request failed verification", e);
|
||||
|
||||
+10
-15
@@ -5,7 +5,6 @@
|
||||
package org.whispersystems.textsecuregcm.subscriptions;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
@@ -26,7 +25,6 @@ public class LoginPurchaseManager {
|
||||
private final IssuedReceiptsManager issuedReceiptsManager;
|
||||
private final ServerZkReceiptOperations zkReceiptOperations;
|
||||
private final long loginLevel;
|
||||
private final Clock clock;
|
||||
|
||||
// Signal Login receipt credentials expire after 5 years
|
||||
@VisibleForTesting
|
||||
@@ -39,18 +37,15 @@ public class LoginPurchaseManager {
|
||||
/// @param zkReceiptOperations Used to issue receipt credentials
|
||||
/// @param loginLevel The receipt level that identifies a Signal Login purchase. Purchases for any other
|
||||
/// level are rejected.
|
||||
/// @param clock A clock
|
||||
public LoginPurchaseManager(
|
||||
final Map<PaymentProvider, OneTimePaymentProcessor> oneTimePaymentProcessors,
|
||||
final IssuedReceiptsManager issuedReceiptsManager,
|
||||
final ServerZkReceiptOperations zkReceiptOperations,
|
||||
final long loginLevel,
|
||||
final Clock clock) {
|
||||
final long loginLevel) {
|
||||
this.oneTimePaymentProcessors = oneTimePaymentProcessors;
|
||||
this.issuedReceiptsManager = issuedReceiptsManager;
|
||||
this.zkReceiptOperations = zkReceiptOperations;
|
||||
this.loginLevel = loginLevel;
|
||||
this.clock = clock;
|
||||
}
|
||||
|
||||
/// Verify a completed one-time purchase and issue a receipt credential for it.
|
||||
@@ -89,19 +84,19 @@ public class LoginPurchaseManager {
|
||||
throw new SubscriptionInvalidArgumentsException("purchase was for an unexpected product");
|
||||
}
|
||||
|
||||
try {
|
||||
issuedReceiptsManager.recordOneTimeIssuance(paymentDetails.id(), paymentProvider, receiptCredentialRequest,
|
||||
clock.instant());
|
||||
} catch (WriteConflictException _) {
|
||||
throw new SubscriptionReceiptAlreadyRedeemedException();
|
||||
}
|
||||
|
||||
// Calculating the expiration from the creation date works for IAP purchases. However, for other processors, the
|
||||
// creation date of the payment intent might be days before the payment actually completed. If we support non-IAP
|
||||
// processors we should attempt to get the latest date. see OneTimeDonationController/OneTimeDonationManager
|
||||
final Instant expiration = paymentDetails.created().plus(LOGIN_EXPIRATION).truncatedTo(ChronoUnit.DAYS);
|
||||
|
||||
return zkReceiptOperations.issueReceiptCredential(
|
||||
receiptCredentialRequest, expiration.getEpochSecond(), loginLevel);
|
||||
try {
|
||||
issuedReceiptsManager.recordOneTimeIssuance(paymentDetails.id(), paymentProvider, receiptCredentialRequest,
|
||||
expiration);
|
||||
} catch (WriteConflictException _) {
|
||||
throw new SubscriptionReceiptAlreadyRedeemedException();
|
||||
}
|
||||
|
||||
return zkReceiptOperations.issueReceiptCredential(receiptCredentialRequest, expiration.getEpochSecond(),
|
||||
loginLevel);
|
||||
}
|
||||
}
|
||||
-1
@@ -378,7 +378,6 @@ public record CommandDependencies(
|
||||
|
||||
final IssuedReceiptsManager issuedReceiptsManager = new IssuedReceiptsManager(
|
||||
configuration.getDynamoDbTables().getIssuedReceipts().getTableName(),
|
||||
configuration.getDynamoDbTables().getIssuedReceipts().getExpiration(),
|
||||
dynamoDbClient,
|
||||
configuration.getDynamoDbTables().getIssuedReceipts().getGenerator(),
|
||||
configuration.getDynamoDbTables().getIssuedReceipts().getMaxReceiptsPerSubscriptionPayment());
|
||||
|
||||
+1
@@ -339,6 +339,7 @@ class OneTimeDonationControllerTest extends AbstractV1SubscriptionControllerTest
|
||||
ServerSecretParams.generate().getPublicParams()).createReceiptCredentialRequestContext(
|
||||
new ReceiptSerial(new byte[ReceiptSerial.SIZE])).getRequest();
|
||||
|
||||
when(ONE_TIME_DONATIONS_MANAGER.getPaidAt(any(), anyString(), any())).thenReturn(Instant.now());
|
||||
when(STRIPE_MANAGER.claimOneTimePurchase(any())).thenReturn(Optional.of(new PaymentDetails(
|
||||
"id",
|
||||
ONETIME_CONFIG.boost().level(),
|
||||
|
||||
+1
@@ -304,6 +304,7 @@ public class OneTimeDonationsGrpcServiceTest extends
|
||||
ServerSecretParams.generate().getPublicParams()).createReceiptCredentialRequestContext(
|
||||
new ReceiptSerial(new byte[ReceiptSerial.SIZE])).getRequest();
|
||||
|
||||
when(oneTimeDonationsManager.getPaidAt(any(), anyString(), any())).thenReturn(clock.instant());
|
||||
when(stripeManager.claimOneTimePurchase(any())).thenReturn(
|
||||
Optional.of(new PaymentDetails("id", oneTimeDonationConfiguration.boost().level(),
|
||||
PaymentStatus.SUCCEEDED, clock.instant(), null)));
|
||||
|
||||
+35
-15
@@ -26,6 +26,7 @@ import org.junit.jupiter.params.provider.EnumSource;
|
||||
import org.signal.libsignal.zkgroup.receipts.ReceiptCredentialRequest;
|
||||
import org.whispersystems.textsecuregcm.storage.DynamoDbExtensionSchema.Tables;
|
||||
import org.whispersystems.textsecuregcm.subscriptions.PaymentProvider;
|
||||
import org.whispersystems.textsecuregcm.util.AttributeValues;
|
||||
import org.whispersystems.textsecuregcm.util.TestRandomUtil;
|
||||
import software.amazon.awssdk.core.SdkBytes;
|
||||
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
|
||||
@@ -35,7 +36,7 @@ import software.amazon.awssdk.services.dynamodb.model.GetItemResponse;
|
||||
|
||||
class IssuedReceiptsManagerTest {
|
||||
|
||||
private static final long NOW_EPOCH_SECONDS = 1_500_000_000L;
|
||||
private static final long EXPIRATION_EPOCH_SECONDS = 1_500_000_000L;
|
||||
|
||||
@RegisterExtension
|
||||
static final DynamoDbExtension DYNAMO_DB_EXTENSION = new DynamoDbExtension(Tables.ISSUED_RECEIPTS);
|
||||
@@ -52,7 +53,6 @@ class IssuedReceiptsManagerTest {
|
||||
void beforeEach() {
|
||||
issuedReceiptsManager = new IssuedReceiptsManager(
|
||||
Tables.ISSUED_RECEIPTS.tableName(),
|
||||
Duration.ofDays(90),
|
||||
DYNAMO_DB_EXTENSION.getDynamoDbClient(),
|
||||
TestRandomUtil.nextBytes(16),
|
||||
MAX_TAGS_MAP);
|
||||
@@ -60,10 +60,10 @@ class IssuedReceiptsManagerTest {
|
||||
|
||||
@Test
|
||||
void testRecordIssuance() throws WriteConflictException {
|
||||
final Instant now = Instant.ofEpochSecond(NOW_EPOCH_SECONDS);
|
||||
final Instant expiration = Instant.ofEpochSecond(EXPIRATION_EPOCH_SECONDS);
|
||||
final ReceiptCredentialRequest receiptCredentialRequest = randomReceiptCredentialRequest();
|
||||
issuedReceiptsManager.recordIssuance("item-1", PaymentProvider.STRIPE,
|
||||
receiptCredentialRequest, now);
|
||||
receiptCredentialRequest, expiration);
|
||||
|
||||
final Map<String, AttributeValue> item = getItem(PaymentProvider.STRIPE, "item-1").item();
|
||||
final Set<byte[]> tagSet = item.get(IssuedReceiptsManager.KEY_ISSUED_RECEIPT_TAG_SET).bs()
|
||||
@@ -74,25 +74,25 @@ class IssuedReceiptsManagerTest {
|
||||
|
||||
// same request should succeed
|
||||
issuedReceiptsManager.recordIssuance("item-1", PaymentProvider.STRIPE, receiptCredentialRequest,
|
||||
now);
|
||||
expiration);
|
||||
|
||||
// same item with new request should fail
|
||||
final byte[] request2 = TestRandomUtil.nextBytes(20);
|
||||
when(receiptCredentialRequest.serialize()).thenReturn(request2);
|
||||
assertThatThrownBy(
|
||||
() -> issuedReceiptsManager.recordIssuance("item-1", PaymentProvider.STRIPE, receiptCredentialRequest,
|
||||
now))
|
||||
expiration))
|
||||
.isExactlyInstanceOf(WriteConflictException.class);
|
||||
|
||||
// different item with new request should be okay though
|
||||
issuedReceiptsManager.recordIssuance("item-2", PaymentProvider.STRIPE, receiptCredentialRequest,
|
||||
now);
|
||||
expiration);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@EnumSource(PaymentProvider.class)
|
||||
void testIssueMax(final PaymentProvider processor) throws WriteConflictException {
|
||||
final Instant now = Instant.ofEpochSecond(NOW_EPOCH_SECONDS);
|
||||
final Instant expiration = Instant.ofEpochSecond(EXPIRATION_EPOCH_SECONDS);
|
||||
|
||||
final int maxTags = MAX_TAGS_MAP.get(processor);
|
||||
final List<ReceiptCredentialRequest> requests = IntStream.range(0, maxTags)
|
||||
@@ -100,10 +100,10 @@ class IssuedReceiptsManagerTest {
|
||||
.toList();
|
||||
for (int i = 0; i < maxTags; i++) {
|
||||
// Should be allowed to insert up to maxTags
|
||||
issuedReceiptsManager.recordIssuance("item-1", processor, requests.get(i), now);
|
||||
issuedReceiptsManager.recordIssuance("item-1", processor, requests.get(i), expiration);
|
||||
for (int j = 0; j < i; j++) {
|
||||
// Also should be allowed to repeat any previous tag
|
||||
issuedReceiptsManager.recordIssuance("item-1", processor, requests.get(j), now);
|
||||
issuedReceiptsManager.recordIssuance("item-1", processor, requests.get(j), expiration);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,21 +116,41 @@ class IssuedReceiptsManagerTest {
|
||||
.toArray(byte[][]::new));
|
||||
|
||||
// Should not be allowed to insert past maxTags
|
||||
assertThatThrownBy(() -> issuedReceiptsManager.recordIssuance("item-1", processor, randomReceiptCredentialRequest(), now))
|
||||
assertThatThrownBy(() -> issuedReceiptsManager.recordIssuance("item-1", processor, randomReceiptCredentialRequest(), expiration))
|
||||
.isExactlyInstanceOf(WriteConflictException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testExpiration() throws WriteConflictException {
|
||||
final Instant expiration = Instant.ofEpochSecond(EXPIRATION_EPOCH_SECONDS);
|
||||
final ReceiptCredentialRequest receiptCredentialRequest = randomReceiptCredentialRequest();
|
||||
issuedReceiptsManager.recordIssuance("item-1", PaymentProvider.STRIPE, receiptCredentialRequest, expiration);
|
||||
|
||||
final Map<String, AttributeValue> item = getItem(PaymentProvider.STRIPE, "item-1").item();
|
||||
assertThat(Instant.ofEpochSecond(AttributeValues.getLong(item, IssuedReceiptsManager.KEY_EXPIRATION, 0L)))
|
||||
.isEqualTo(expiration.plus(IssuedReceiptsManager.TTL_PADDING));
|
||||
|
||||
// Only the initial TTL sticks (should never change anyways, because receipt expirations should be deterministic)
|
||||
issuedReceiptsManager.recordIssuance("item-1", PaymentProvider.STRIPE, receiptCredentialRequest, expiration.plus(Duration.ofDays(1)));
|
||||
assertThat(Instant.ofEpochSecond(AttributeValues.getLong(item, IssuedReceiptsManager.KEY_EXPIRATION, 0L)))
|
||||
.isEqualTo(expiration.plus(IssuedReceiptsManager.TTL_PADDING));
|
||||
|
||||
issuedReceiptsManager.recordIssuance("item-1", PaymentProvider.STRIPE, receiptCredentialRequest, expiration.minus(Duration.ofDays(1)));
|
||||
assertThat(Instant.ofEpochSecond(AttributeValues.getLong(item, IssuedReceiptsManager.KEY_EXPIRATION, 0L)))
|
||||
.isEqualTo(expiration.plus(IssuedReceiptsManager.TTL_PADDING));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@EnumSource(PaymentProvider.class)
|
||||
void testSingleIssuance(final PaymentProvider processor) throws WriteConflictException {
|
||||
final Instant now = Instant.ofEpochSecond(NOW_EPOCH_SECONDS);
|
||||
final Instant expiration = Instant.ofEpochSecond(EXPIRATION_EPOCH_SECONDS);
|
||||
final ReceiptCredentialRequest request = randomReceiptCredentialRequest();
|
||||
|
||||
issuedReceiptsManager.recordOneTimeIssuance("item-1", processor, request, now);
|
||||
issuedReceiptsManager.recordOneTimeIssuance("item-1", processor, request, expiration);
|
||||
// Retrying with the same request should still succeed
|
||||
issuedReceiptsManager.recordOneTimeIssuance("item-1", processor, request, now);
|
||||
issuedReceiptsManager.recordOneTimeIssuance("item-1", processor, request, expiration);
|
||||
assertThatThrownBy(
|
||||
() -> issuedReceiptsManager.recordOneTimeIssuance("item-1", processor, randomReceiptCredentialRequest(), now))
|
||||
() -> issuedReceiptsManager.recordOneTimeIssuance("item-1", processor, randomReceiptCredentialRequest(), expiration))
|
||||
.isExactlyInstanceOf(WriteConflictException.class);
|
||||
}
|
||||
|
||||
|
||||
+16
-23
@@ -13,10 +13,8 @@ import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@@ -47,8 +45,7 @@ class LoginPurchaseManagerTest {
|
||||
private static final PaymentProvider PROVIDER = PaymentProvider.APPLE_APP_STORE;
|
||||
private static final String PURCHASE_ID = "purchaseId";
|
||||
private static final long LEVEL = 123L;
|
||||
private static final Instant NOW = Instant.now();
|
||||
private static final Instant PURCHASED_AT = NOW.minus(Duration.ofHours(3));
|
||||
private static final Instant PURCHASED_AT = Instant.now().minus(Duration.ofHours(3));
|
||||
private static final ChargeFailure CHARGE_FAILURE =
|
||||
new ChargeFailure("generic_decline", "some failure message", null, null, null);
|
||||
|
||||
@@ -74,8 +71,7 @@ class LoginPurchaseManagerTest {
|
||||
Map.of(PROVIDER, paymentProcessor),
|
||||
issuedReceiptsManager,
|
||||
new ServerZkReceiptOperations(SERVER_SECRET_PARAMS),
|
||||
LEVEL,
|
||||
Clock.fixed(NOW, ZoneOffset.UTC));
|
||||
LEVEL);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -88,14 +84,15 @@ class LoginPurchaseManagerTest {
|
||||
final ReceiptCredential receiptCredential =
|
||||
clientZkReceiptOperations.receiveReceiptCredential(receiptCredentialRequestContext, receipt);
|
||||
|
||||
assertThat(receiptCredential.getReceiptLevel()).isEqualTo(LEVEL);
|
||||
assertThat(receiptCredential.getReceiptExpirationTime()).isEqualTo(PURCHASED_AT
|
||||
final Instant expectedExpiration = PURCHASED_AT
|
||||
.plus(LoginPurchaseManager.LOGIN_EXPIRATION)
|
||||
.truncatedTo(ChronoUnit.DAYS)
|
||||
.getEpochSecond());
|
||||
.truncatedTo(ChronoUnit.DAYS);
|
||||
|
||||
assertThat(receiptCredential.getReceiptLevel()).isEqualTo(LEVEL);
|
||||
assertThat(receiptCredential.getReceiptExpirationTime()).isEqualTo(expectedExpiration.getEpochSecond());
|
||||
|
||||
verify(issuedReceiptsManager).recordOneTimeIssuance(
|
||||
PURCHASE_ID, PROVIDER, receiptCredentialRequestContext.getRequest(), NOW);
|
||||
PURCHASE_ID, PROVIDER, receiptCredentialRequestContext.getRequest(), expectedExpiration);
|
||||
}
|
||||
|
||||
static Stream<Arguments> generateReceiptErrors() {
|
||||
@@ -119,18 +116,16 @@ class LoginPurchaseManagerTest {
|
||||
void generateReceiptErrors(final Optional<PaymentDetails> paymentDetails, final Class<? extends Exception> expectedException)
|
||||
throws SubscriptionInvalidArgumentsException, RateLimitExceededException, IOException {
|
||||
when(paymentProcessor.claimOneTimePurchase(PURCHASE_ID)).thenReturn(paymentDetails);
|
||||
assertThatExceptionOfType(expectedException).isThrownBy(() -> {
|
||||
loginPurchaseManager.generateReceipt(PROVIDER, PURCHASE_ID, receiptCredentialRequestContext.getRequest());
|
||||
});
|
||||
assertThatExceptionOfType(expectedException).isThrownBy(() ->
|
||||
loginPurchaseManager.generateReceipt(PROVIDER, PURCHASE_ID, receiptCredentialRequestContext.getRequest()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateReceiptUnknownLevel() throws Exception {
|
||||
when(paymentProcessor.claimOneTimePurchase(PURCHASE_ID))
|
||||
.thenReturn(Optional.of(new PaymentDetails(PURCHASE_ID, null, PaymentStatus.SUCCEEDED, PURCHASED_AT, null)));
|
||||
assertThatExceptionOfType(SubscriptionInvalidArgumentsException.class).isThrownBy(() -> {
|
||||
loginPurchaseManager.generateReceipt(PROVIDER, PURCHASE_ID, receiptCredentialRequestContext.getRequest());
|
||||
});
|
||||
assertThatExceptionOfType(SubscriptionInvalidArgumentsException.class).isThrownBy(() ->
|
||||
loginPurchaseManager.generateReceipt(PROVIDER, PURCHASE_ID, receiptCredentialRequestContext.getRequest()));
|
||||
verifyNoInteractions(issuedReceiptsManager);
|
||||
}
|
||||
|
||||
@@ -138,9 +133,8 @@ class LoginPurchaseManagerTest {
|
||||
void generateReceiptNonLoginLevel() throws Exception {
|
||||
when(paymentProcessor.claimOneTimePurchase(PURCHASE_ID))
|
||||
.thenReturn(Optional.of(new PaymentDetails(PURCHASE_ID, LEVEL + 1, PaymentStatus.SUCCEEDED, PURCHASED_AT, null)));
|
||||
assertThatExceptionOfType(SubscriptionInvalidArgumentsException.class).isThrownBy(() -> {
|
||||
loginPurchaseManager.generateReceipt(PROVIDER, PURCHASE_ID, receiptCredentialRequestContext.getRequest());
|
||||
});
|
||||
assertThatExceptionOfType(SubscriptionInvalidArgumentsException.class).isThrownBy(() ->
|
||||
loginPurchaseManager.generateReceipt(PROVIDER, PURCHASE_ID, receiptCredentialRequestContext.getRequest()));
|
||||
verifyNoInteractions(issuedReceiptsManager);
|
||||
}
|
||||
|
||||
@@ -150,8 +144,7 @@ class LoginPurchaseManagerTest {
|
||||
.thenReturn(Optional.of(new PaymentDetails(PURCHASE_ID, LEVEL, PaymentStatus.SUCCEEDED, PURCHASED_AT, null)));
|
||||
doThrow(new WriteConflictException())
|
||||
.when(issuedReceiptsManager).recordOneTimeIssuance(any(), any(), any(), any());
|
||||
assertThatExceptionOfType(SubscriptionReceiptAlreadyRedeemedException.class).isThrownBy(() -> {
|
||||
loginPurchaseManager.generateReceipt(PROVIDER, PURCHASE_ID, receiptCredentialRequestContext.getRequest());
|
||||
});
|
||||
assertThatExceptionOfType(SubscriptionReceiptAlreadyRedeemedException.class).isThrownBy(() ->
|
||||
loginPurchaseManager.generateReceipt(PROVIDER, PURCHASE_ID, receiptCredentialRequestContext.getRequest()));
|
||||
}
|
||||
}
|
||||
@@ -111,7 +111,6 @@ dynamoDbTables:
|
||||
expiration: P7D # Duration of time until rows expire
|
||||
issuedReceipts:
|
||||
tableName: issued_receipts_test
|
||||
expiration: P30D # Duration of time until rows expire
|
||||
generator: abcdefg12345678= # random base64-encoded binary sequence
|
||||
maxReceiptsPerSubscriptionPayment:
|
||||
STRIPE: 1
|
||||
|
||||
Reference in new issue
Block a user