diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/purchases/AppleAppStoreClient.java b/service/src/main/java/org/whispersystems/textsecuregcm/purchases/AppleAppStoreClient.java index 6241105cb..a1b285078 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/purchases/AppleAppStoreClient.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/purchases/AppleAppStoreClient.java @@ -15,6 +15,7 @@ import com.apple.itunes.storekit.model.StatusResponse; import com.apple.itunes.storekit.model.TransactionInfoResponse; import com.apple.itunes.storekit.verification.SignedDataVerifier; import com.apple.itunes.storekit.verification.VerificationException; +import com.apple.itunes.storekit.verification.VerificationStatus; import com.google.common.annotations.VisibleForTesting; import io.github.resilience4j.retry.Retry; import io.github.resilience4j.retry.RetryConfig; @@ -27,6 +28,7 @@ import java.util.Base64; import java.util.List; import java.util.Optional; import java.util.Set; +import java.util.concurrent.Callable; import java.util.stream.Collectors; import javax.annotation.Nullable; import io.micrometer.core.instrument.Tags; @@ -113,10 +115,10 @@ public class AppleAppStoreClient { default -> throw new IllegalStateException("Unexpected environment: " + environment); }; try { - return new AppleAppStoreDecodedTransaction( + return verifyWithRetry(() -> new AppleAppStoreDecodedTransaction( tx, signedDataVerifier.verifyAndDecodeTransaction(tx.getSignedTransactionInfo()), - signedDataVerifier.verifyAndDecodeRenewalInfo(tx.getSignedRenewalInfo())); + signedDataVerifier.verifyAndDecodeRenewalInfo(tx.getSignedRenewalInfo()))); } catch (VerificationException e) { throw new UncheckedIOException(new IOException("Failed to verify payload from App Store Server", e)); } @@ -146,7 +148,8 @@ public class AppleAppStoreClient { return Optional.of(lookupByTransactionId(transactionId, errorTags, (environment, txId) -> { try { final TransactionInfoResponse transactionInfo = client(environment).getTransactionInfo(txId); - return verifier(environment).verifyAndDecodeTransaction(transactionInfo.getSignedTransactionInfo()); + return verifyWithRetry(() -> + verifier(environment).verifyAndDecodeTransaction(transactionInfo.getSignedTransactionInfo())); } catch (VerificationException e) { throw new IOException("Failed to verify payload from App Store Server", e); } @@ -217,6 +220,16 @@ public class AppleAppStoreClient { } } + private T verifyWithRetry(final Callable verification) throws VerificationException { + try { + return retry.executeCallable(verification); + } catch (VerificationException e) { + throw e; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + private AppStoreServerAPIClient client(final Environment environment) { return switch (environment) { case SANDBOX -> sandboxApiClient; @@ -247,6 +260,9 @@ public class AppleAppStoreClient { } private static boolean shouldRetry(Throwable e) { + if (e instanceof VerificationException verificationException) { + return verificationException.getStatus() == VerificationStatus.RETRYABLE_VERIFICATION_FAILURE; + } return e instanceof APIException apiException && switch (apiException.getApiError()) { case ORIGINAL_TRANSACTION_ID_NOT_FOUND_RETRYABLE, GENERAL_INTERNAL_RETRYABLE, APP_NOT_FOUND_RETRYABLE, ACCOUNT_NOT_FOUND_RETRYABLE -> true; case null, default -> false; diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/purchases/AppleAppStoreClientTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/purchases/AppleAppStoreClientTest.java index 2d714acde..86e2f6a0a 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/purchases/AppleAppStoreClientTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/purchases/AppleAppStoreClientTest.java @@ -24,10 +24,12 @@ import com.apple.itunes.storekit.model.StatusResponse; import com.apple.itunes.storekit.model.TransactionInfoResponse; import com.apple.itunes.storekit.verification.SignedDataVerifier; import com.apple.itunes.storekit.verification.VerificationException; +import com.apple.itunes.storekit.verification.VerificationStatus; import io.github.resilience4j.retry.Retry; import io.github.resilience4j.retry.RetryConfig; import io.micrometer.core.instrument.Tags; import java.io.IOException; +import java.io.UncheckedIOException; import java.time.Duration; import java.util.ArrayDeque; import java.util.ArrayList; @@ -39,6 +41,7 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.EnumSource; import org.junit.jupiter.params.provider.MethodSource; +import org.mockito.Mockito; import org.whispersystems.textsecuregcm.controllers.RateLimitExceededException; class AppleAppStoreClientTest { @@ -230,6 +233,37 @@ class AppleAppStoreClientTest { verifyNoInteractions(unexpectedVerifier); } + @Test + public void verifySubscriptionRetryableFailure() throws VerificationException { + when(productionSignedDataVerifier.verifyAndDecodeTransaction(SIGNED_TX_INFO)) + .thenThrow(new VerificationException(VerificationStatus.RETRYABLE_VERIFICATION_FAILURE)) + .thenReturn(new JWSTransactionDecodedPayload().productId(PRODUCT_ID)); + when(productionSignedDataVerifier.verifyAndDecodeRenewalInfo(SIGNED_RENEWAL_INFO)) + .thenReturn(new JWSRenewalInfoDecodedPayload()); + + apiWrapper.verifySubscription(Environment.PRODUCTION, new LastTransactionsItem() + .originalTransactionId(ORIGINAL_TX_ID) + .signedRenewalInfo(SIGNED_RENEWAL_INFO) + .signedTransactionInfo(SIGNED_TX_INFO)); + + verify(productionSignedDataVerifier, Mockito.times(2)) + .verifyAndDecodeTransaction(SIGNED_TX_INFO); + } + + @Test + public void verifySubscriptionInvalidChain() throws VerificationException { + when(productionSignedDataVerifier.verifyAndDecodeTransaction(SIGNED_TX_INFO)) + .thenThrow(new VerificationException(VerificationStatus.INVALID_CHAIN)); + + assertThatExceptionOfType(UncheckedIOException.class).isThrownBy(() -> + apiWrapper.verifySubscription(Environment.PRODUCTION, new LastTransactionsItem() + .originalTransactionId(ORIGINAL_TX_ID) + .signedRenewalInfo(SIGNED_RENEWAL_INFO) + .signedTransactionInfo(SIGNED_TX_INFO))); + + verify(productionSignedDataVerifier).verifyAndDecodeTransaction(SIGNED_TX_INFO); + } + /// Indicates how many times to throw an exception on calls to [ConfigurableFinder#lookup] private static class ExceptionConfig { int numTimes;