diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/OneTimeDonationController.java b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/OneTimeDonationController.java index e17632180..3e59fba89 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/OneTimeDonationController.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/OneTimeDonationController.java @@ -194,7 +194,7 @@ public class OneTimeDonationController { return false; } }) - .orElse(true); + .orElse(false); if (!spendSuccessful) { throw new WebApplicationException(Response.Status.UNAUTHORIZED); } diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/SubscriptionController.java b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/SubscriptionController.java index cebc9fb18..7bf1d22a4 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/SubscriptionController.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/SubscriptionController.java @@ -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); diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/mappers/SubscriptionExceptionMapper.java b/service/src/main/java/org/whispersystems/textsecuregcm/mappers/SubscriptionExceptionMapper.java index b816bfc25..15d801366 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/mappers/SubscriptionExceptionMapper.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/mappers/SubscriptionExceptionMapper.java @@ -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 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; }); diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/controllers/OneTimeDonationControllerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/controllers/OneTimeDonationControllerTest.java index 06aa57729..29d85e94d 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/controllers/OneTimeDonationControllerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/controllers/OneTimeDonationControllerTest.java @@ -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( diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/controllers/SubscriptionControllerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/controllers/SubscriptionControllerTest.java index f787237d5..565f09734 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/controllers/SubscriptionControllerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/controllers/SubscriptionControllerTest.java @@ -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