Require donation permit headers

This commit is contained in:
Ravi Khadiwala authored and ravi-signal committed 2026-07-15 11:35:46 -05:00
1 parent 3d45706a35
commit 749d9c4f92
5 files changed
+74 -3

No files matched your search

@@ -194,7 +194,7 @@ public class OneTimeDonationController {
return false;
}
})
.orElse(true);
.orElse(false);
if (!spendSuccessful) {
throw new WebApplicationException(Response.Status.UNAUTHORIZED);
}
@@ -234,7 +234,7 @@ public class SubscriptionController {
return false;
}
})
.orElse(true);
.orElse(false);
subscriptionManager.updateSubscriber(subscriberCredentials, creationPermitted);
return Response.ok().build();
@@ -248,6 +248,16 @@ public class SubscriptionController {
@Path("/{subscriberId}/create_payment_method")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Operation(summary = "Create a payment method", description = """
Creates a payment method setup token with the payment processor and returns a client secret that can be used to
complete the payment method setup.
""")
@ApiResponse(responseCode = "200", description = "The payment method was created", content = @Content(schema = @Schema(implementation = CreatePaymentMethodResponse.class)))
@ApiResponse(responseCode = "400", description = "The requested payment method type is not supported by this endpoint")
@ApiResponse(responseCode = "401", description = "Donation permit was invalid or already spent")
@ApiResponse(responseCode = "403", description = "subscriberId authentication failure OR account authentication is present")
@ApiResponse(responseCode = "404", description = "subscriberId is malformed OR the subscriber does not exist")
@ApiResponse(responseCode = "409", description = "The subscriber is already associated with a different payment processor")
@ManagedAsync
@RateLimitedByIp(RateLimiters.For.ADD_SUBSCRIPTION_PAYMENT_METHOD)
public CreatePaymentMethodResponse createPaymentMethod(
@@ -283,7 +293,7 @@ public class SubscriptionController {
return false;
}
})
.orElse(true);
.orElse(false);
if (!spendSuccessful) {
throw new WebApplicationException(Response.Status.UNAUTHORIZED);
@@ -12,6 +12,7 @@ import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;
import java.util.Map;
import org.whispersystems.textsecuregcm.subscriptions.SubscriberIdCreationNotPermittedException;
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionChargeFailurePaymentRequiredException;
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionException;
import org.whispersystems.textsecuregcm.subscriptions.ChargeFailure;
@@ -63,6 +64,7 @@ public class SubscriptionExceptionMapper implements ExceptionMapper<Subscription
case SubscriptionProcessorConflictException _ -> Response.Status.CONFLICT;
case SubscriptionPaymentRequiredException _ -> Response.Status.PAYMENT_REQUIRED;
case SubscriptionReceiptAlreadyRedeemedException _ -> Response.Status.CONFLICT;
case SubscriberIdCreationNotPermittedException _ -> Response.Status.UNAUTHORIZED;
default -> Response.Status.INTERNAL_SERVER_ERROR;
});
@@ -203,6 +203,21 @@ class OneTimeDonationControllerTest extends AbstractV1SubscriptionControllerTest
}
}
@Test
void createBoostPaymentIntentMissingDonationPermit() {
when(STRIPE_MANAGER.getSupportedCurrenciesForPaymentMethod(PaymentMethod.CARD))
.thenReturn(Set.of("usd", "jpy", "bif", "eur"));
try (Response response = RESOURCE_EXTENSION.target("/v1/subscription/boost/create")
.request()
.post(Entity.json("""
{"currency": "USD", "amount": 300}
"""
))) {
assertThat(response.getStatus()).isEqualTo(401);
}
}
@Test
void testCreateBoostPayPal() throws IOException {
final BraintreeManager.PayPalOneTimePaymentApprovalDetails payPalOneTimePaymentApprovalDetails = mock(
@@ -340,6 +340,7 @@ class SubscriptionControllerTest extends AbstractV1SubscriptionControllerTest {
try (Response managerCreateNullResponse = RESOURCE_EXTENSION.target(
String.format("/v1/subscription/%s", subscriberId))
.request()
.header(HeaderUtils.DONATION_PERMIT, getDonationPermitHeader())
.put(Entity.json(""))) {
assertThat(managerCreateNullResponse.getStatus()).isEqualTo(403);
}
@@ -434,6 +435,49 @@ class SubscriptionControllerTest extends AbstractV1SubscriptionControllerTest {
}
}
@Test
void updateSubscriberMissingDonationPermit() {
final byte[] subscriberUserAndKey = new byte[32];
Arrays.fill(subscriberUserAndKey, (byte) 1);
final String subscriberId = Base64.getEncoder().encodeToString(subscriberUserAndKey);
// Creating a subscriber requires a permit
when(SUBSCRIPTIONS.get(any(), any())).thenReturn(Subscriptions.GetResult.NOT_STORED);
try (Response response = RESOURCE_EXTENSION.target(String.format("/v1/subscription/%s", subscriberId))
.request()
.put(Entity.json(""))) {
assertThat(response.getStatus()).isEqualTo(401);
}
// Updating a subscriber does not require a permit
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())
))));
try (Response response = RESOURCE_EXTENSION.target(String.format("/v1/subscription/%s", subscriberId))
.request()
.put(Entity.json(""))) {
assertThat(response.getStatus()).isEqualTo(200);
}
}
@Test
void createPaymentMethodMissingDonationPermit() {
final byte[] subscriberUserAndKey = new byte[32];
Arrays.fill(subscriberUserAndKey, (byte) 1);
final String subscriberId = Base64.getEncoder().encodeToString(subscriberUserAndKey);
try (Response response = RESOURCE_EXTENSION
.target(String.format("/v1/subscription/%s/create_payment_method", subscriberId))
.request()
.post(Entity.json(""))) {
assertThat(response.getStatus()).isEqualTo(401);
}
}
@Test
void setSubscriptionLevelMissingProcessorCustomer() {
// set up record