mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-12-02 12:06:53 +00:00
Add LoginPurchaseController
This commit is contained in:
1 parent
7a34493d53
commit
d6b80dfd16
15 files changed
+469
-6
No files matched your search
+209
@@ -0,0 +1,209 @@
|
||||
/*
|
||||
* Copyright 2026 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package org.whispersystems.textsecuregcm.controllers;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.reset;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import io.dropwizard.auth.AuthValueFactoryProvider;
|
||||
import io.dropwizard.testing.junit5.DropwizardExtensionsSupport;
|
||||
import io.dropwizard.testing.junit5.ResourceExtension;
|
||||
import jakarta.ws.rs.client.Entity;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import java.util.stream.Stream;
|
||||
import org.glassfish.jersey.server.ServerProperties;
|
||||
import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.signal.libsignal.zkgroup.InvalidInputException;
|
||||
import org.signal.libsignal.zkgroup.ServerSecretParams;
|
||||
import org.signal.libsignal.zkgroup.VerificationFailedException;
|
||||
import org.signal.libsignal.zkgroup.receipts.ClientZkReceiptOperations;
|
||||
import org.signal.libsignal.zkgroup.receipts.ReceiptCredentialRequest;
|
||||
import org.signal.libsignal.zkgroup.receipts.ReceiptCredentialRequestContext;
|
||||
import org.signal.libsignal.zkgroup.receipts.ReceiptCredentialResponse;
|
||||
import org.signal.libsignal.zkgroup.receipts.ReceiptSerial;
|
||||
import org.signal.libsignal.zkgroup.receipts.ServerZkReceiptOperations;
|
||||
import org.whispersystems.textsecuregcm.auth.AuthenticatedDevice;
|
||||
import org.whispersystems.textsecuregcm.configuration.dynamic.DynamicConfiguration;
|
||||
import org.whispersystems.textsecuregcm.configuration.dynamic.DynamicLoginPurchaseConfiguration;
|
||||
import org.whispersystems.textsecuregcm.mappers.CompletionExceptionMapper;
|
||||
import org.whispersystems.textsecuregcm.mappers.RateLimitExceededExceptionMapper;
|
||||
import org.whispersystems.textsecuregcm.mappers.SubscriptionExceptionMapper;
|
||||
import org.whispersystems.textsecuregcm.storage.DynamicConfigurationManager;
|
||||
import org.whispersystems.textsecuregcm.subscriptions.ChargeFailure;
|
||||
import org.whispersystems.textsecuregcm.subscriptions.LoginPurchaseManager;
|
||||
import org.whispersystems.textsecuregcm.subscriptions.PaymentProvider;
|
||||
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionChargeFailurePaymentRequiredException;
|
||||
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionInvalidArgumentsException;
|
||||
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionNotFoundException;
|
||||
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionPaymentRequiredException;
|
||||
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionReceiptAlreadyRedeemedException;
|
||||
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionReceiptRequestedForOpenPaymentException;
|
||||
import org.whispersystems.textsecuregcm.tests.util.AuthHelper;
|
||||
import org.whispersystems.textsecuregcm.util.SystemMapper;
|
||||
import org.whispersystems.textsecuregcm.util.TestRandomUtil;
|
||||
|
||||
@ExtendWith(DropwizardExtensionsSupport.class)
|
||||
class LoginPurchaseControllerTest {
|
||||
|
||||
private static final String PURCHASE_ID = "purchaseId";
|
||||
|
||||
private static final ServerSecretParams SERVER_SECRET_PARAMS = ServerSecretParams.generate();
|
||||
private static final ClientZkReceiptOperations CLIENT_ZK_OPS =
|
||||
new ClientZkReceiptOperations(SERVER_SECRET_PARAMS.getPublicParams());
|
||||
private static final ServerZkReceiptOperations SERVER_ZK_OPS =
|
||||
new ServerZkReceiptOperations(SERVER_SECRET_PARAMS);
|
||||
|
||||
private static final LoginPurchaseManager LOGIN_PURCHASE_MANAGER = mock(LoginPurchaseManager.class);
|
||||
|
||||
private static final DynamicLoginPurchaseConfiguration ENABLED = new DynamicLoginPurchaseConfiguration(true);
|
||||
private static final DynamicLoginPurchaseConfiguration DISABLED = new DynamicLoginPurchaseConfiguration(false);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static final DynamicConfigurationManager<DynamicConfiguration> DYNAMIC_CONFIGURATION_MANAGER =
|
||||
mock(DynamicConfigurationManager.class);
|
||||
private static final DynamicConfiguration DYNAMIC_CONFIGURATION = mock(DynamicConfiguration.class);
|
||||
|
||||
private static final ResourceExtension RESOURCE_EXTENSION = ResourceExtension.builder()
|
||||
.addProperty(ServerProperties.UNWRAP_COMPLETION_STAGE_IN_WRITER_ENABLE, Boolean.TRUE)
|
||||
.addProvider(AuthHelper.getAuthFilter())
|
||||
.addProvider(CompletionExceptionMapper.class)
|
||||
.addProvider(new AuthValueFactoryProvider.Binder<>(AuthenticatedDevice.class))
|
||||
.addProvider(SubscriptionExceptionMapper.class)
|
||||
.addProvider(RateLimitExceededExceptionMapper.class)
|
||||
.setMapper(SystemMapper.jsonMapper())
|
||||
.setTestContainerFactory(new GrizzlyWebTestContainerFactory())
|
||||
.addResource(new LoginPurchaseController(LOGIN_PURCHASE_MANAGER, DYNAMIC_CONFIGURATION_MANAGER))
|
||||
.build();
|
||||
|
||||
private ReceiptCredentialRequestContext receiptCredentialRequestContext;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws InvalidInputException, VerificationFailedException {
|
||||
reset(LOGIN_PURCHASE_MANAGER, DYNAMIC_CONFIGURATION_MANAGER, DYNAMIC_CONFIGURATION);
|
||||
|
||||
when(DYNAMIC_CONFIGURATION_MANAGER.getConfiguration()).thenReturn(DYNAMIC_CONFIGURATION);
|
||||
when(DYNAMIC_CONFIGURATION.getLoginPurchaseConfiguration()).thenReturn(ENABLED);
|
||||
|
||||
receiptCredentialRequestContext = CLIENT_ZK_OPS.createReceiptCredentialRequestContext(
|
||||
new ReceiptSerial(TestRandomUtil.nextBytes(ReceiptSerial.SIZE)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createReceiptCredential() throws Exception {
|
||||
final ReceiptCredentialResponse receiptCredentialResponse =
|
||||
SERVER_ZK_OPS.issueReceiptCredential(receiptCredentialRequestContext.getRequest(), 0L, 200L);
|
||||
|
||||
when(LOGIN_PURCHASE_MANAGER.generateReceipt(any(), any(), any())).thenReturn(receiptCredentialResponse);
|
||||
|
||||
try (final Response response = RESOURCE_EXTENSION
|
||||
.target("/v1/login-purchase/receipt_credentials")
|
||||
.request()
|
||||
.post(Entity.json(new LoginPurchaseController.CreateLoginReceiptCredentialRequest(
|
||||
PURCHASE_ID,
|
||||
receiptCredentialRequestContext.getRequest().serialize(),
|
||||
PaymentProvider.APPLE_APP_STORE)))) {
|
||||
assertThat(response.getStatus()).isEqualTo(200);
|
||||
assertThat(response.readEntity(LoginPurchaseController.CreateLoginReceiptCredentialResponse.class)
|
||||
.receiptCredentialResponse()).isEqualTo(receiptCredentialResponse.serialize());
|
||||
}
|
||||
|
||||
verify(LOGIN_PURCHASE_MANAGER).generateReceipt(
|
||||
eq(PaymentProvider.APPLE_APP_STORE),
|
||||
eq(PURCHASE_ID),
|
||||
any(ReceiptCredentialRequest.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createReceiptCredentialAuthenticated() {
|
||||
try (final Response response = RESOURCE_EXTENSION.target("/v1/login-purchase/receipt_credentials")
|
||||
.request()
|
||||
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_UUID, AuthHelper.VALID_PASSWORD))
|
||||
.post(Entity.json(new LoginPurchaseController.CreateLoginReceiptCredentialRequest(
|
||||
PURCHASE_ID,
|
||||
receiptCredentialRequestContext.getRequest().serialize(),
|
||||
PaymentProvider.APPLE_APP_STORE)))) {
|
||||
|
||||
assertThat(response.getStatus()).isEqualTo(403);
|
||||
}
|
||||
|
||||
verifyNoInteractions(LOGIN_PURCHASE_MANAGER);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createReceiptCredentialNotEnabled() {
|
||||
when(DYNAMIC_CONFIGURATION.getLoginPurchaseConfiguration()).thenReturn(DISABLED);
|
||||
|
||||
try (final Response response = RESOURCE_EXTENSION.target("/v1/login-purchase/receipt_credentials")
|
||||
.request()
|
||||
.post(Entity.json(new LoginPurchaseController.CreateLoginReceiptCredentialRequest(
|
||||
PURCHASE_ID,
|
||||
receiptCredentialRequestContext.getRequest().serialize(),
|
||||
PaymentProvider.APPLE_APP_STORE)))) {
|
||||
|
||||
assertThat(response.getStatus()).isEqualTo(400);
|
||||
}
|
||||
|
||||
verifyNoInteractions(LOGIN_PURCHASE_MANAGER);
|
||||
}
|
||||
|
||||
static Stream<Arguments> createReceiptCredentialErrors() {
|
||||
return Stream.of(
|
||||
Arguments.of(new SubscriptionReceiptRequestedForOpenPaymentException(), 204),
|
||||
Arguments.of(new SubscriptionPaymentRequiredException(), 402),
|
||||
Arguments.of(new SubscriptionNotFoundException(), 404),
|
||||
Arguments.of(new SubscriptionInvalidArgumentsException("test"), 400),
|
||||
Arguments.of(new SubscriptionReceiptAlreadyRedeemedException(), 409),
|
||||
Arguments.of(new VerificationFailedException(), 400),
|
||||
Arguments.of(new RateLimitExceededException(null), 429));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
void createReceiptCredentialErrors(final Exception exception, final int expectedStatus) throws Exception {
|
||||
when(LOGIN_PURCHASE_MANAGER.generateReceipt(any(), any(), any())).thenThrow(exception);
|
||||
|
||||
try (final Response response = RESOURCE_EXTENSION.target("/v1/login-purchase/receipt_credentials")
|
||||
.request()
|
||||
.post(Entity.json(new LoginPurchaseController.CreateLoginReceiptCredentialRequest(
|
||||
PURCHASE_ID,
|
||||
receiptCredentialRequestContext.getRequest().serialize(),
|
||||
PaymentProvider.APPLE_APP_STORE)))) {
|
||||
|
||||
assertThat(response.getStatus()).isEqualTo(expectedStatus);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void createReceiptCredentialPaymentRequiredWithChargeFailure() throws Exception {
|
||||
final ChargeFailure chargeFailure =
|
||||
new ChargeFailure("generic_decline", "some failure message", null, null, null);
|
||||
when(LOGIN_PURCHASE_MANAGER.generateReceipt(any(), any(), any()))
|
||||
.thenThrow(new SubscriptionChargeFailurePaymentRequiredException(PaymentProvider.APPLE_APP_STORE, chargeFailure));
|
||||
|
||||
try (final Response response = RESOURCE_EXTENSION.target("/v1/login-purchase/receipt_credentials")
|
||||
.request()
|
||||
.post(Entity.json(new LoginPurchaseController.CreateLoginReceiptCredentialRequest(
|
||||
PURCHASE_ID,
|
||||
receiptCredentialRequestContext.getRequest().serialize(),
|
||||
PaymentProvider.APPLE_APP_STORE)))) {
|
||||
final SubscriptionExceptionMapper.ChargeFailureResponse failureResponse =
|
||||
response.readEntity(SubscriptionExceptionMapper.ChargeFailureResponse.class);
|
||||
assertThat(failureResponse.chargeFailure()).isEqualTo(chargeFailure);
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
-1
@@ -60,6 +60,7 @@ import org.signal.libsignal.zkgroup.receipts.ReceiptSerial;
|
||||
import org.signal.libsignal.zkgroup.receipts.ServerZkReceiptOperations;
|
||||
import org.whispersystems.textsecuregcm.auth.AuthenticatedDevice;
|
||||
import org.whispersystems.textsecuregcm.badges.BadgeTranslator;
|
||||
import org.whispersystems.textsecuregcm.configuration.LoginPurchaseConfiguration;
|
||||
import org.whispersystems.textsecuregcm.configuration.SubscriptionConfiguration;
|
||||
import org.whispersystems.textsecuregcm.controllers.SubscriptionController.GetBankMandateResponse;
|
||||
import org.whispersystems.textsecuregcm.controllers.SubscriptionController.GetSubscriptionConfigurationResponse;
|
||||
@@ -109,8 +110,10 @@ class SubscriptionControllerTest extends AbstractV1SubscriptionControllerTest {
|
||||
private static final ServerZkReceiptOperations ZK_OPS = mock(ServerZkReceiptOperations.class);
|
||||
private static final BadgeTranslator BADGE_TRANSLATOR = mock(BadgeTranslator.class);
|
||||
private static final BankMandateTranslator BANK_MANDATE_TRANSLATOR = mock(BankMandateTranslator.class);
|
||||
private static final LoginPurchaseConfiguration LOGIN_PURCHASE_CONFIG =
|
||||
new LoginPurchaseConfiguration(300L, "testLoginPlayProductId", "testLoginAppStoreProductId");
|
||||
private final static SubscriptionController SUBSCRIPTION_CONTROLLER = new SubscriptionController(CLOCK,
|
||||
SUBSCRIPTION_CONFIG, ONETIME_CONFIG,
|
||||
SUBSCRIPTION_CONFIG, ONETIME_CONFIG, LOGIN_PURCHASE_CONFIG,
|
||||
new SubscriptionManager(SUBSCRIPTIONS, List.of(STRIPE_MANAGER, BRAINTREE_MANAGER, PLAY_MANAGER, APPSTORE_MANAGER),
|
||||
ZK_OPS, ISSUED_RECEIPTS_MANAGER), STRIPE_MANAGER, BRAINTREE_MANAGER, PLAY_MANAGER, APPSTORE_MANAGER,
|
||||
BADGE_TRANSLATOR, BANK_MANDATE_TRANSLATOR, DONATION_PERMITS_MANAGER, MAX_TOTAL_BACKUP_MEDIA_BYTES);
|
||||
@@ -1074,6 +1077,10 @@ class SubscriptionControllerTest extends AbstractV1SubscriptionControllerTest {
|
||||
});
|
||||
assertThat(response.backup().freeTierMediaDays()).isEqualTo(30);
|
||||
|
||||
assertThat(response.login().level()).isEqualTo(LOGIN_PURCHASE_CONFIG.level());
|
||||
assertThat(response.login().playProductId()).isEqualTo(LOGIN_PURCHASE_CONFIG.playProductId());
|
||||
assertThat(response.login().appStoreProductId()).isEqualTo(LOGIN_PURCHASE_CONFIG.appStoreProductId());
|
||||
|
||||
// check the badge vs purchasable badge fields
|
||||
// subscription levels are Badge, while one-time levels are PurchasableBadge, which adds `duration`
|
||||
Map<String, Object> genericResponse = RESOURCE_EXTENSION.target("/v1/subscription/configuration")
|
||||
|
||||
+9
-1
@@ -22,6 +22,7 @@ import org.signal.chat.purchase.GetConfigurationRequest;
|
||||
import org.signal.chat.purchase.GetConfigurationResponse;
|
||||
import org.signal.chat.purchase.PaymentMethod;
|
||||
import org.signal.chat.purchase.ProductConfigurationGrpc;
|
||||
import org.whispersystems.textsecuregcm.configuration.LoginPurchaseConfiguration;
|
||||
import org.whispersystems.textsecuregcm.configuration.OneTimeDonationConfiguration;
|
||||
import org.whispersystems.textsecuregcm.configuration.SubscriptionConfiguration;
|
||||
import org.whispersystems.textsecuregcm.subscriptions.BraintreeManager;
|
||||
@@ -37,6 +38,9 @@ public class ProductConfigurationGrpcServiceTest extends
|
||||
private final OneTimeDonationConfiguration oneTimeDonationConfiguration =
|
||||
SubscriptionConfigTestHelper.getOneTimeConfig();
|
||||
|
||||
private static final LoginPurchaseConfiguration LOGIN_PURCHASE_CONFIGURATION =
|
||||
new LoginPurchaseConfiguration(300L, "testLoginPlayProductId", "testLoginAppStoreProductId");
|
||||
|
||||
@Mock
|
||||
private StripeManager stripeManager;
|
||||
|
||||
@@ -65,7 +69,7 @@ public class ProductConfigurationGrpcServiceTest extends
|
||||
|
||||
|
||||
return new ProductConfigurationGrpcService(subscriptionConfiguration, oneTimeDonationConfiguration,
|
||||
List.of(stripeManager, braintreeManager), 1234L);
|
||||
LOGIN_PURCHASE_CONFIGURATION, List.of(stripeManager, braintreeManager), 1234L);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -127,6 +131,10 @@ public class ProductConfigurationGrpcServiceTest extends
|
||||
assertTrue(configuration.getBadgeLevelsOrThrow(1L).getBadgeDurationSeconds() > 0);
|
||||
assertEquals("GIFT", configuration.getBadgeLevelsOrThrow(100L).getBadgeId());
|
||||
assertTrue(configuration.getBadgeLevelsOrThrow(100L).getBadgeDurationSeconds() > 0);
|
||||
|
||||
assertEquals(LOGIN_PURCHASE_CONFIGURATION.level(), configuration.getLogin().getLevel());
|
||||
assertEquals(LOGIN_PURCHASE_CONFIGURATION.playProductId(), configuration.getLogin().getPlayProductId());
|
||||
assertEquals(LOGIN_PURCHASE_CONFIGURATION.appStoreProductId(), configuration.getLogin().getAppStoreProductId());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -397,6 +397,11 @@ subscription: # configuration for Stripe subscriptions
|
||||
STRIPE: price_example # stripe Price ID
|
||||
BRAINTREE: plan_example # braintree Plan ID
|
||||
|
||||
loginPurchase:
|
||||
level: 300
|
||||
playProductId: login_example
|
||||
appStoreProductId: login_example
|
||||
|
||||
oneTimeDonations:
|
||||
sepaMaximumEuros: '10000'
|
||||
boost:
|
||||
|
||||
Reference in new issue
Block a user