mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-12-02 11:46:53 +00:00
Fix handling write conflicts when redeeming subscription receipt
credentials
This commit is contained in:
1 parent
d6c7c5e7bf
commit
50ef5272f7
13 files changed
+223
-163
No files matched your search
+31
@@ -9,6 +9,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.reset;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -20,12 +21,17 @@ import io.dropwizard.testing.junit5.ResourceExtension;
|
||||
import jakarta.ws.rs.client.Entity;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import java.time.Instant;
|
||||
import java.util.Base64;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.stream.Stream;
|
||||
import org.signal.libsignal.zkgroup.ServerSecretParams;
|
||||
import org.signal.libsignal.zkgroup.receipts.ClientZkReceiptOperations;
|
||||
import org.signal.libsignal.zkgroup.receipts.ReceiptCredentialRequest;
|
||||
import org.signal.libsignal.zkgroup.receipts.ReceiptSerial;
|
||||
import org.assertj.core.api.InstanceOfAssertFactories;
|
||||
import org.glassfish.jersey.server.ServerProperties;
|
||||
import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory;
|
||||
@@ -48,6 +54,7 @@ import org.whispersystems.textsecuregcm.subscriptions.PaymentProvider;
|
||||
import org.whispersystems.textsecuregcm.subscriptions.PaymentStatus;
|
||||
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionProcessorException;
|
||||
import org.whispersystems.textsecuregcm.tests.util.AuthHelper;
|
||||
import org.whispersystems.textsecuregcm.storage.WriteConflictException;
|
||||
import org.whispersystems.textsecuregcm.util.SystemMapper;
|
||||
|
||||
@ExtendWith(DropwizardExtensionsSupport.class)
|
||||
@@ -284,4 +291,28 @@ class OneTimeDonationControllerTest extends AbstractV1SubscriptionControllerTest
|
||||
assertThat(response.getStatus()).isEqualTo(422);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createBoostReceiptAlreadyRedeemed() throws Exception {
|
||||
final ReceiptCredentialRequest receiptCredentialRequest = new ClientZkReceiptOperations(
|
||||
ServerSecretParams.generate().getPublicParams()).createReceiptCredentialRequestContext(
|
||||
new ReceiptSerial(new byte[ReceiptSerial.SIZE])).getRequest();
|
||||
|
||||
when(STRIPE_MANAGER.getPaymentDetails(any())).thenReturn(CompletableFuture.completedFuture(new PaymentDetails(
|
||||
"id",
|
||||
Collections.emptyMap(),
|
||||
PaymentStatus.SUCCEEDED,
|
||||
Instant.now(),
|
||||
null)));
|
||||
doThrow(WriteConflictException.class).when(ISSUED_RECEIPTS_MANAGER).recordIssuance(any(), any(), any(), any());
|
||||
|
||||
final Response response = RESOURCE_EXTENSION.target("/v1/subscription/boost/receipt_credentials")
|
||||
.request()
|
||||
.post(Entity.json(Map.of(
|
||||
"paymentIntentId", "foo",
|
||||
"receiptCredentialRequest", Base64.getEncoder().encodeToString(receiptCredentialRequest.serialize()),
|
||||
"processor", "STRIPE"
|
||||
)));
|
||||
assertThat(response.getStatus()).isEqualTo(409);
|
||||
}
|
||||
|
||||
}
|
||||
+37
-2
@@ -10,6 +10,7 @@ import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.reset;
|
||||
import static org.mockito.Mockito.times;
|
||||
@@ -90,6 +91,7 @@ import org.whispersystems.textsecuregcm.subscriptions.SubscriptionPaymentRequire
|
||||
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionProcessorConflictException;
|
||||
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionProcessorException;
|
||||
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionReceiptRequestedForOpenPaymentException;
|
||||
import org.whispersystems.textsecuregcm.storage.WriteConflictException;
|
||||
import org.whispersystems.textsecuregcm.tests.util.AuthHelper;
|
||||
import org.whispersystems.textsecuregcm.util.MockUtils;
|
||||
import org.whispersystems.textsecuregcm.util.SystemMapper;
|
||||
@@ -782,6 +784,41 @@ class SubscriptionControllerTest extends AbstractV1SubscriptionControllerTest {
|
||||
.isEqualTo("card_declined");
|
||||
}
|
||||
|
||||
@Test
|
||||
void createReceiptCredentialAlreadyRedeemed()
|
||||
throws InvalidInputException, VerificationFailedException, SubscriptionException, WriteConflictException {
|
||||
final byte[] subscriberUserAndKey = new byte[32];
|
||||
Arrays.fill(subscriberUserAndKey, (byte) 1);
|
||||
final String subscriberId = Base64.getEncoder().encodeToString(subscriberUserAndKey);
|
||||
|
||||
when(CLOCK.instant()).thenReturn(Instant.now());
|
||||
when(SUBSCRIPTIONS.get(any(), any()))
|
||||
.thenReturn(Subscriptions.GetResult.found(Subscriptions.Record.from(
|
||||
Arrays.copyOfRange(subscriberUserAndKey, 0, 16),
|
||||
Map.of(Subscriptions.KEY_PASSWORD, b(new byte[16]),
|
||||
Subscriptions.KEY_CREATED_AT, n(Instant.now().getEpochSecond()),
|
||||
Subscriptions.KEY_ACCESSED_AT, n(Instant.now().getEpochSecond()),
|
||||
Subscriptions.KEY_PROCESSOR_ID_CUSTOMER_ID,
|
||||
b(new ProcessorCustomer("customer", PaymentProvider.STRIPE).toDynamoBytes()),
|
||||
Subscriptions.KEY_SUBSCRIPTION_ID, s("subscriptionId")))));
|
||||
when(STRIPE_MANAGER.getReceiptItem(any()))
|
||||
.thenReturn(new CustomerAwareSubscriptionPaymentProcessor.ReceiptItem(
|
||||
"itemId",
|
||||
PaymentTime.periodStart(Instant.ofEpochSecond(10).plus(Duration.ofDays(1))),
|
||||
5L));
|
||||
doThrow(WriteConflictException.class).when(ISSUED_RECEIPTS_MANAGER).recordIssuance(any(), any(), any(), any());
|
||||
|
||||
final ReceiptCredentialRequest receiptRequest = new ClientZkReceiptOperations(
|
||||
ServerSecretParams.generate().getPublicParams()).createReceiptCredentialRequestContext(
|
||||
new ReceiptSerial(new byte[ReceiptSerial.SIZE])).getRequest();
|
||||
final Response response = RESOURCE_EXTENSION
|
||||
.target(String.format("/v1/subscription/%s/receipt_credentials", subscriberId))
|
||||
.request()
|
||||
.post(Entity.json(new SubscriptionController.GetReceiptCredentialsRequest(receiptRequest.serialize())));
|
||||
|
||||
assertThat(response.getStatus()).isEqualTo(409);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"5, P45D", "201, P13D"})
|
||||
public void createReceiptCredential(long level, Duration expectedExpirationWindow)
|
||||
@@ -813,8 +850,6 @@ class SubscriptionControllerTest extends AbstractV1SubscriptionControllerTest {
|
||||
"itemId",
|
||||
PaymentTime.periodStart(Instant.ofEpochSecond(10).plus(Duration.ofDays(1))),
|
||||
level));
|
||||
when(ISSUED_RECEIPTS_MANAGER.recordIssuance(eq("itemId"), eq(PaymentProvider.BRAINTREE), eq(receiptRequest), any()))
|
||||
.thenReturn(CompletableFuture.completedFuture(null));
|
||||
when(ZK_OPS.issueReceiptCredential(any(), anyLong(), eq(level))).thenReturn(receiptCredentialResponse);
|
||||
when(receiptCredentialResponse.serialize()).thenReturn(new byte[0]);
|
||||
final Response response = RESOURCE_EXTENSION
|
||||
|
||||
+18
-36
@@ -6,20 +6,18 @@
|
||||
package org.whispersystems.textsecuregcm.storage;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import jakarta.ws.rs.ClientErrorException;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.EnumMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
import org.assertj.core.api.Condition;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
@@ -42,7 +40,7 @@ class IssuedReceiptsManagerTest {
|
||||
@RegisterExtension
|
||||
static final DynamoDbExtension DYNAMO_DB_EXTENSION = new DynamoDbExtension(Tables.ISSUED_RECEIPTS);
|
||||
|
||||
private static EnumMap<PaymentProvider, Integer> MAX_TAGS_MAP = new EnumMap<>(Map.of(
|
||||
private static final EnumMap<PaymentProvider, Integer> MAX_TAGS_MAP = new EnumMap<>(Map.of(
|
||||
PaymentProvider.STRIPE, 1,
|
||||
PaymentProvider.BRAINTREE, 2,
|
||||
PaymentProvider.GOOGLE_PLAY_BILLING, 3,
|
||||
@@ -55,18 +53,17 @@ class IssuedReceiptsManagerTest {
|
||||
issuedReceiptsManager = new IssuedReceiptsManager(
|
||||
Tables.ISSUED_RECEIPTS.tableName(),
|
||||
Duration.ofDays(90),
|
||||
DYNAMO_DB_EXTENSION.getDynamoDbAsyncClient(),
|
||||
DYNAMO_DB_EXTENSION.getDynamoDbClient(),
|
||||
TestRandomUtil.nextBytes(16),
|
||||
MAX_TAGS_MAP);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRecordIssuance() {
|
||||
Instant now = Instant.ofEpochSecond(NOW_EPOCH_SECONDS);
|
||||
void testRecordIssuance() throws WriteConflictException {
|
||||
final Instant now = Instant.ofEpochSecond(NOW_EPOCH_SECONDS);
|
||||
final ReceiptCredentialRequest receiptCredentialRequest = randomReceiptCredentialRequest();
|
||||
CompletableFuture<Void> future = issuedReceiptsManager.recordIssuance("item-1", PaymentProvider.STRIPE,
|
||||
issuedReceiptsManager.recordIssuance("item-1", PaymentProvider.STRIPE,
|
||||
receiptCredentialRequest, now);
|
||||
assertThat(future).succeedsWithin(Duration.ofSeconds(3));
|
||||
|
||||
final Map<String, AttributeValue> item = getItem(PaymentProvider.STRIPE, "item-1").item();
|
||||
final Set<byte[]> tagSet = item.get(IssuedReceiptsManager.KEY_ISSUED_RECEIPT_TAG_SET).bs()
|
||||
@@ -76,32 +73,25 @@ class IssuedReceiptsManagerTest {
|
||||
assertThat(tagSet).containsExactly(issuedReceiptsManager.generateIssuedReceiptTag(receiptCredentialRequest));
|
||||
|
||||
// same request should succeed
|
||||
future = issuedReceiptsManager.recordIssuance("item-1", PaymentProvider.STRIPE, receiptCredentialRequest,
|
||||
issuedReceiptsManager.recordIssuance("item-1", PaymentProvider.STRIPE, receiptCredentialRequest,
|
||||
now);
|
||||
assertThat(future).succeedsWithin(Duration.ofSeconds(3));
|
||||
|
||||
// same item with new request should fail
|
||||
byte[] request2 = TestRandomUtil.nextBytes(20);
|
||||
final byte[] request2 = TestRandomUtil.nextBytes(20);
|
||||
when(receiptCredentialRequest.serialize()).thenReturn(request2);
|
||||
future = issuedReceiptsManager.recordIssuance("item-1", PaymentProvider.STRIPE, receiptCredentialRequest,
|
||||
now);
|
||||
assertThat(future).failsWithin(Duration.ofSeconds(3)).
|
||||
withThrowableOfType(Throwable.class).
|
||||
havingCause().
|
||||
isExactlyInstanceOf(ClientErrorException.class).
|
||||
has(new Condition<>(
|
||||
e -> e instanceof ClientErrorException && ((ClientErrorException) e).getResponse().getStatus() == 409,
|
||||
"status 409"));
|
||||
assertThatThrownBy(
|
||||
() -> issuedReceiptsManager.recordIssuance("item-1", PaymentProvider.STRIPE, receiptCredentialRequest,
|
||||
now))
|
||||
.isExactlyInstanceOf(WriteConflictException.class);
|
||||
|
||||
// different item with new request should be okay though
|
||||
future = issuedReceiptsManager.recordIssuance("item-2", PaymentProvider.STRIPE, receiptCredentialRequest,
|
||||
issuedReceiptsManager.recordIssuance("item-2", PaymentProvider.STRIPE, receiptCredentialRequest,
|
||||
now);
|
||||
assertThat(future).succeedsWithin(Duration.ofSeconds(3));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@EnumSource(PaymentProvider.class)
|
||||
void testIssueMax(PaymentProvider processor) {
|
||||
void testIssueMax(final PaymentProvider processor) throws WriteConflictException {
|
||||
final Instant now = Instant.ofEpochSecond(NOW_EPOCH_SECONDS);
|
||||
|
||||
final int maxTags = MAX_TAGS_MAP.get(processor);
|
||||
@@ -110,12 +100,10 @@ class IssuedReceiptsManagerTest {
|
||||
.toList();
|
||||
for (int i = 0; i < maxTags; i++) {
|
||||
// Should be allowed to insert up to maxTags
|
||||
assertThat(issuedReceiptsManager.recordIssuance("item-1", processor, requests.get(i), now))
|
||||
.succeedsWithin(Duration.ofSeconds(3));
|
||||
issuedReceiptsManager.recordIssuance("item-1", processor, requests.get(i), now);
|
||||
for (int j = 0; j < i; j++) {
|
||||
// Also should be allowed to repeat any previous tag
|
||||
assertThat(issuedReceiptsManager.recordIssuance("item-1", processor, requests.get(j), now))
|
||||
.succeedsWithin(Duration.ofSeconds(3));
|
||||
issuedReceiptsManager.recordIssuance("item-1", processor, requests.get(j), now);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,14 +116,8 @@ class IssuedReceiptsManagerTest {
|
||||
.toArray(byte[][]::new));
|
||||
|
||||
// Should not be allowed to insert past maxTags
|
||||
assertThat(issuedReceiptsManager.recordIssuance("item-1", processor, randomReceiptCredentialRequest(), now))
|
||||
.failsWithin(Duration.ofSeconds(3))
|
||||
.withThrowableOfType(Throwable.class)
|
||||
.havingCause()
|
||||
.isExactlyInstanceOf(ClientErrorException.class)
|
||||
.has(new Condition<>(
|
||||
e -> e instanceof ClientErrorException && ((ClientErrorException) e).getResponse().getStatus() == 409,
|
||||
"status 409"));
|
||||
assertThatThrownBy(() -> issuedReceiptsManager.recordIssuance("item-1", processor, randomReceiptCredentialRequest(), now))
|
||||
.isExactlyInstanceOf(WriteConflictException.class);
|
||||
}
|
||||
|
||||
|
||||
|
||||
+4
-4
@@ -23,7 +23,7 @@ public class OnetimeDonationsManagerTest {
|
||||
oneTimeDonationsManager = new OneTimeDonationsManager(
|
||||
DynamoDbExtensionSchema.Tables.ONETIME_DONATIONS.tableName(),
|
||||
Duration.ofDays(90),
|
||||
DYNAMO_DB_EXTENSION.getDynamoDbAsyncClient());
|
||||
DYNAMO_DB_EXTENSION.getDynamoDbClient());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -31,9 +31,9 @@ public class OnetimeDonationsManagerTest {
|
||||
final String validPaymentIntentId = "abc";
|
||||
final Instant paidAt = Instant.ofEpochSecond(1_000_000);
|
||||
final Instant fallBackTimestamp = Instant.ofEpochSecond(2_000_000);
|
||||
oneTimeDonationsManager.putPaidAt(validPaymentIntentId, paidAt).join();
|
||||
oneTimeDonationsManager.putPaidAt(validPaymentIntentId, paidAt);
|
||||
|
||||
assertThat(oneTimeDonationsManager.getPaidAt(validPaymentIntentId, fallBackTimestamp).join()).isEqualTo(paidAt);
|
||||
assertThat(oneTimeDonationsManager.getPaidAt("invalidPaymentId", fallBackTimestamp).join()).isEqualTo(fallBackTimestamp);
|
||||
assertThat(oneTimeDonationsManager.getPaidAt(validPaymentIntentId, fallBackTimestamp)).isEqualTo(paidAt);
|
||||
assertThat(oneTimeDonationsManager.getPaidAt("invalidPaymentId", fallBackTimestamp)).isEqualTo(fallBackTimestamp);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user