-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from alexanderjordanbaker/AdditionalEnvironmen…
…tSupport Add support for Xcode and LocalTesting environments
- Loading branch information
Showing
14 changed files
with
228 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/java/com/apple/itunes/storekit/model/XcodeCompatibleTimestampDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (c) 2023 Apple Inc. Licensed under MIT License. | ||
|
||
package com.apple.itunes.storekit.model; | ||
|
||
import com.google.gson.JsonDeserializationContext; | ||
import com.google.gson.JsonDeserializer; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonParseException; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
/** | ||
* Xcode may sometimes return timestamps as floating point numbers not integers. This class allows parsing those receipts | ||
*/ | ||
class XcodeCompatibleTimestampDeserializer implements JsonDeserializer<Long> { | ||
@Override | ||
public Long deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { | ||
return json.getAsJsonPrimitive().getAsNumber().longValue(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/test/java/com/apple/itunes/storekit/util/TestingUtility.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright (c) 2023 Apple Inc. Licensed under MIT License. | ||
|
||
package com.apple.itunes.storekit.util; | ||
|
||
import com.apple.itunes.storekit.model.Environment; | ||
import com.apple.itunes.storekit.verification.SignedDataVerifier; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Set; | ||
|
||
public class TestingUtility { | ||
|
||
public static String readFile(String file) throws IOException { | ||
return new String(readBytes(file), StandardCharsets.UTF_8); | ||
} | ||
|
||
public static byte[] readBytes(String file) throws IOException { | ||
try (InputStream stream = TestingUtility.class.getClassLoader().getResourceAsStream(file)) { | ||
return stream.readAllBytes(); | ||
} | ||
} | ||
|
||
public static SignedDataVerifier getSignedPayloadVerifier(Environment environment, String bundleId) throws IOException { | ||
return new SignedDataVerifier(Set.of(new ByteArrayInputStream(readBytes("certs/testCA.der"))), bundleId, 1234L, environment, false); | ||
} | ||
|
||
public static SignedDataVerifier getSignedPayloadVerifier() throws IOException { | ||
return getSignedPayloadVerifier(Environment.LOCAL_TESTING, "com.example"); | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
src/test/java/com/apple/itunes/storekit/verification/XcodeSignedDataVerifierTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Copyright (c) 2023 Apple Inc. Licensed under MIT License. | ||
|
||
package com.apple.itunes.storekit.verification; | ||
|
||
import com.apple.itunes.storekit.model.AppTransaction; | ||
import com.apple.itunes.storekit.model.AutoRenewStatus; | ||
import com.apple.itunes.storekit.model.Environment; | ||
import com.apple.itunes.storekit.model.InAppOwnershipType; | ||
import com.apple.itunes.storekit.model.JWSRenewalInfoDecodedPayload; | ||
import com.apple.itunes.storekit.model.JWSTransactionDecodedPayload; | ||
import com.apple.itunes.storekit.model.OfferType; | ||
import com.apple.itunes.storekit.model.TransactionReason; | ||
import com.apple.itunes.storekit.model.Type; | ||
import com.apple.itunes.storekit.util.TestingUtility; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
import java.util.UUID; | ||
|
||
public class XcodeSignedDataVerifierTest { | ||
|
||
private final String XCODE_BUNDLE_ID = "com.example.naturelab.backyardbirds.example"; | ||
|
||
@Test | ||
public void testXcodeSignedAppTransaction() throws IOException, VerificationException { | ||
SignedDataVerifier verifier = TestingUtility.getSignedPayloadVerifier(Environment.XCODE, XCODE_BUNDLE_ID); | ||
String encodedAppTransaction = TestingUtility.readFile("xcode/xcode-signed-app-transaction"); | ||
|
||
AppTransaction appTransaction = verifier.verifyAndDecodeAppTransaction(encodedAppTransaction); | ||
|
||
Assertions.assertNotNull(appTransaction); | ||
Assertions.assertNull(appTransaction.getAppAppleId()); | ||
Assertions.assertEquals(XCODE_BUNDLE_ID, appTransaction.getBundleId()); | ||
Assertions.assertEquals("1", appTransaction.getApplicationVersion()); | ||
Assertions.assertNull(appTransaction.versionExternalIdentifier()); | ||
Assertions.assertEquals(-62135769600000L, appTransaction.originalPurchaseDate()); | ||
Assertions.assertEquals("1", appTransaction.getOriginalApplicationVersion()); | ||
Assertions.assertEquals("cYUsXc53EbYc0pOeXG5d6/31LGHeVGf84sqSN0OrJi5u/j2H89WWKgS8N0hMsMlf", appTransaction.getDeviceVerification()); | ||
Assertions.assertEquals(UUID.fromString("48c8b92d-ce0d-4229-bedf-e61b4f9cfc92"), appTransaction.getDeviceVerificationNonce()); | ||
Assertions.assertNull(appTransaction.getPreorderDate()); | ||
} | ||
|
||
@Test | ||
public void testXcodeSignedTransaction() throws IOException, VerificationException { | ||
SignedDataVerifier verifier = TestingUtility.getSignedPayloadVerifier(Environment.XCODE, XCODE_BUNDLE_ID); | ||
String encodedTransactino = TestingUtility.readFile("xcode/xcode-signed-transaction"); | ||
|
||
JWSTransactionDecodedPayload transaction = verifier.verifyAndDecodeTransaction(encodedTransactino); | ||
|
||
Assertions.assertEquals("0", transaction.getOriginalTransactionId()); | ||
Assertions.assertEquals("0", transaction.getTransactionId()); | ||
Assertions.assertEquals("0", transaction.getWebOrderLineItemId()); | ||
Assertions.assertEquals(XCODE_BUNDLE_ID, transaction.getBundleId()); | ||
Assertions.assertEquals("pass.premium", transaction.getProductId()); | ||
Assertions.assertEquals("6F3A93AB", transaction.getSubscriptionGroupIdentifier()); | ||
Assertions.assertEquals(1697679936049L, transaction.getPurchaseDate()); | ||
Assertions.assertEquals(1697679936049L, transaction.getOriginalPurchaseDate()); | ||
Assertions.assertEquals(1700358336049L, transaction.getExpiresDate()); | ||
Assertions.assertEquals(1, transaction.getQuantity()); | ||
Assertions.assertEquals(Type.AUTO_RENEWABLE_SUBSCRIPTION, transaction.getType()); | ||
Assertions.assertNull(transaction.getAppAccountToken()); | ||
Assertions.assertEquals(InAppOwnershipType.PURCHASED, transaction.getInAppOwnershipType()); | ||
Assertions.assertEquals(1697679936056L, transaction.getSignedDate()); | ||
Assertions.assertNull(transaction.getRevocationReason()); | ||
Assertions.assertNull(transaction.getRevocationDate()); | ||
Assertions.assertFalse(transaction.getIsUpgraded()); | ||
Assertions.assertEquals(OfferType.INTRODUCTORY_OFFER, transaction.getOfferType()); | ||
Assertions.assertNull(transaction.getOfferIdentifier()); | ||
Assertions.assertEquals(Environment.XCODE, transaction.getEnvironment()); | ||
Assertions.assertEquals("USA", transaction.getStorefront()); | ||
Assertions.assertEquals("143441", transaction.getStorefrontId()); | ||
Assertions.assertEquals(TransactionReason.PURCHASE, transaction.getTransactionReason()); | ||
} | ||
|
||
@Test | ||
public void testXcodeSignedRenewalInfo() throws IOException, VerificationException { | ||
SignedDataVerifier verifier = TestingUtility.getSignedPayloadVerifier(Environment.XCODE, XCODE_BUNDLE_ID); | ||
String encodedRenewalInfo = TestingUtility.readFile("xcode/xcode-signed-renewal-info"); | ||
|
||
JWSRenewalInfoDecodedPayload renewalInfo = verifier.verifyAndDecodeRenewalInfo(encodedRenewalInfo); | ||
|
||
System.out.println(renewalInfo); | ||
Assertions.assertNull(renewalInfo.getExpirationIntent()); | ||
Assertions.assertEquals("0", renewalInfo.getOriginalTransactionId()); | ||
Assertions.assertEquals("pass.premium", renewalInfo.getAutoRenewProductId()); | ||
Assertions.assertEquals("pass.premium", renewalInfo.getProductId()); | ||
Assertions.assertEquals(AutoRenewStatus.ON, renewalInfo.getAutoRenewStatus()); | ||
Assertions.assertNull(renewalInfo.getIsInBillingRetryPeriod()); | ||
Assertions.assertNull(renewalInfo.getPriceIncreaseStatus()); | ||
Assertions.assertNull(renewalInfo.getGracePeriodExpiresDate()); | ||
Assertions.assertNull(renewalInfo.getOfferType()); | ||
Assertions.assertNull(renewalInfo.getOfferIdentifier()); | ||
Assertions.assertEquals(1697679936711L, renewalInfo.getSignedDate()); | ||
Assertions.assertEquals(Environment.XCODE, renewalInfo.getEnvironment()); | ||
Assertions.assertEquals(1697679936049L, renewalInfo.getRecentSubscriptionStartDate()); | ||
Assertions.assertEquals(1700358336049L, renewalInfo.getRenewalDate()); | ||
} | ||
|
||
@Test | ||
public void testXcodeSignedAppTransactionWithProductionEnvironment() throws IOException { | ||
SignedDataVerifier verifier = TestingUtility.getSignedPayloadVerifier(Environment.PRODUCTION, XCODE_BUNDLE_ID); | ||
String encodedAppTransaction = TestingUtility.readFile("xcode/xcode-signed-app-transaction"); | ||
Assertions.assertThrows(VerificationException.class, () -> verifier.verifyAndDecodeAppTransaction(encodedAppTransaction)); | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
MIAGCSqGSIb3DQEHAqCAMIACAQExDzANBglghkgBZQMEAgEFADCABgkqhkiG9w0BBwGggCSABIHhMYHeMA8CAQACAQEEBwwFWGNvZGUwCwIBAQIBAQQDAgEAMDUCAQICAQEELQwrY29tLmV4YW1wbGUubmF0dXJlbGFiLmJhY2t5YXJkYmlyZHMuZXhhbXBsZTALAgEDAgEBBAMMATEwEAIBBAIBAQQI0bz+zwQAAAAwHAIBBQIBAQQU4nEwK24WxZhKi0PSGTYgWoXOIqMwCgIBCAIBAQQCFgAwHgIBDAIBAQQWFhQyMDIzLTEwLTE5VDAxOjE4OjU0WjAeAgEVAgEBBBYWFDQwMDEtMDEtMDFUMDA6MDA6MDBaAAAAAAAAoIIDeDCCA3QwggJcoAMCAQICAQEwDQYJKoZIhvcNAQELBQAwXzERMA8GA1UEAwwIU3RvcmVLaXQxETAPBgNVBAoMCFN0b3JlS2l0MREwDwYDVQQLDAhTdG9yZUtpdDELMAkGA1UEBhMCVVMxFzAVBgkqhkiG9w0BCQEWCFN0b3JlS2l0MB4XDTIwMDQwMTE3NTIzNVoXDTQwMDMyNzE3NTIzNVowXzERMA8GA1UEAwwIU3RvcmVLaXQxETAPBgNVBAoMCFN0b3JlS2l0MREwDwYDVQQLDAhTdG9yZUtpdDELMAkGA1UEBhMCVVMxFzAVBgkqhkiG9w0BCQEWCFN0b3JlS2l0MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA23+QPCxzD9uXJkuTuwr4oSE+yGHZJMheH3U+2pPbMRqRgLm/5QzLPLsORGIm+gQptknnb+Ab5g1ozSVuw3YI9UoLrnp0PMSpC7PPYg/7tLz324ReKOtHDfHti6z1n7AJOKNue8smUAoa4YnRcnYLOUzLT27As1+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