Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/Context/ContextResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@
*/
class ContextResolver
{
public function __construct(private readonly InAppPurchaseProvider $inAppPurchaseProvider)
{
public function __construct(
private readonly ?InAppPurchaseProvider $inAppPurchaseProvider = null
) {
}

/**
Expand Down Expand Up @@ -94,7 +95,7 @@
if (!empty($params['in-app-purchases'])) {
/** @var non-empty-string $inAppPurchaseString */
$inAppPurchaseString = $params['in-app-purchases'];
$inAppPurchases = $this->inAppPurchaseProvider->decodePurchases($inAppPurchaseString, $shop);
$inAppPurchases = $this->inAppPurchaseProvider?->decodePurchases($inAppPurchaseString, $shop);

Check warning on line 98 in src/Context/ContextResolver.php

View workflow job for this annotation

GitHub Actions / unit

Escaped Mutant for Mutator "NullSafeMethodCall": @@ @@ if (!empty($params['in-app-purchases'])) { /** @var non-empty-string $inAppPurchaseString */ $inAppPurchaseString = $params['in-app-purchases']; - $inAppPurchases = $this->inAppPurchaseProvider?->decodePurchases($inAppPurchaseString, $shop); + $inAppPurchases = $this->inAppPurchaseProvider->decodePurchases($inAppPurchaseString, $shop); } return new ModuleAction($shop, $params['sw-version'], $params['sw-context-language'], $params['sw-user-language'], $inAppPurchases ?? new Collection()); }
}

return new ModuleAction(
Expand Down Expand Up @@ -259,7 +260,7 @@
throw new MalformedWebhookBodyException();
}

$inAppPurchases = $this->inAppPurchaseProvider->decodePurchases($claims['inAppPurchases'], $shop);
$inAppPurchases = $this->inAppPurchaseProvider?->decodePurchases($claims['inAppPurchases'], $shop);

Check warning on line 263 in src/Context/ContextResolver.php

View workflow job for this annotation

GitHub Actions / unit

Escaped Mutant for Mutator "NullSafeMethodCall": @@ @@ if (!\is_string($claims['inAppPurchases']) || empty($claims['inAppPurchases'])) { throw new MalformedWebhookBodyException(); } - $inAppPurchases = $this->inAppPurchaseProvider?->decodePurchases($claims['inAppPurchases'], $shop); + $inAppPurchases = $this->inAppPurchaseProvider->decodePurchases($claims['inAppPurchases'], $shop); } return new StorefrontAction($shop, new StorefrontClaims($claims), $inAppPurchases ?? new Collection()); }
}

return new StorefrontAction(
Expand Down Expand Up @@ -322,7 +323,7 @@
throw new MalformedWebhookBodyException();
}

$inAppPurchases = $this->inAppPurchaseProvider->decodePurchases($source['inAppPurchases'], $shop);
$inAppPurchases = $this->inAppPurchaseProvider?->decodePurchases($source['inAppPurchases'], $shop);

Check warning on line 326 in src/Context/ContextResolver.php

View workflow job for this annotation

GitHub Actions / unit

Escaped Mutant for Mutator "NullSafeMethodCall": @@ @@ if (!\is_string($source['inAppPurchases']) || empty($source['inAppPurchases'])) { throw new MalformedWebhookBodyException(); } - $inAppPurchases = $this->inAppPurchaseProvider?->decodePurchases($source['inAppPurchases'], $shop); + $inAppPurchases = $this->inAppPurchaseProvider->decodePurchases($source['inAppPurchases'], $shop); } return new ActionSource($source['url'], $source['appVersion'], $inAppPurchases ?? new Collection()); } }
}


Expand Down
2 changes: 2 additions & 0 deletions src/Context/InAppPurchase/HasMatchingDomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

/**
* @phpstan-import-type InAppPurchaseArray from InAppPurchaseProvider
*
* @deprecated Will be removed with version 5.0.0, as no licence domain can be provided for validation
*/
class HasMatchingDomain implements Constraint
{
Expand Down
14 changes: 5 additions & 9 deletions src/Context/InAppPurchase/HasValidRSAJWKSignature.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
use Lcobucci\JWT\Signer\Rsa\Sha384;
use Lcobucci\JWT\Signer\Rsa\Sha512;
use Lcobucci\JWT\Token;
use Lcobucci\JWT\UnencryptedToken;
use Lcobucci\JWT\Validation\Constraint;
use Lcobucci\JWT\Validation\ConstraintViolation;
use Lcobucci\JWT\Validation\Constraint\SignedWith;
use Strobotti\JWK\Key\KeyInterface;
use Strobotti\JWK\Key\Rsa as RsaKey;
use Strobotti\JWK\KeyConverter;
Expand All @@ -27,12 +26,11 @@ public function __construct(private readonly KeySet $keys)
{
}

/**
* {@inheritDoc}
*/
public function assert(Token $token): void
{
if (!$token instanceof UnencryptedToken) {
throw new \Exception('Token must be a plain JWT');
}

$this->validateAlgorithm($token);

$key = $this->getValidKey($token);
Expand All @@ -45,9 +43,7 @@ public function assert(Token $token): void

$signer = $this->getSigner($alg);

if (!$signer->verify($token->signature()->hash(), $token->payload(), InMemory::plainText($pem))) {
throw ConstraintViolation::error('Token signature mismatch', $this);
}
(new SignedWith($signer, InMemory::plainText($pem)))->assert($token);
}

private function validateAlgorithm(Token $token): void
Expand Down
8 changes: 3 additions & 5 deletions src/Context/InAppPurchase/InAppPurchaseProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Shopware\App\SDK\Shop\ShopInterface;

/**
* @phpstan-type InAppPurchaseArray=array{identifier: string, quantity: int, nextBookingDate?: string, sub: string}
* @phpstan-type InAppPurchaseArray array{identifier: string, quantity: int, nextBookingDate?: string, sub: string}
*/
class InAppPurchaseProvider
{
Expand All @@ -27,21 +27,19 @@ public function __construct(
/**
* @param non-empty-string $encodedPurchases
* @return Collection<InAppPurchase>
* @throws \Exception
*/
public function decodePurchases(string $encodedPurchases, ShopInterface $shop, bool $retried = false): Collection
{
try {
$keys = $this->keyFetcher->getKey($retried);
$signatureValidator = new HasValidRSAJWKSignature($keys);
$domainValidator = new HasMatchingDomain($shop);

$parser = new Parser(new JoseEncoder());
/** @var Token\Plain $token */
$token = $parser->parse($encodedPurchases);

$validator = new Validator();
$validator->assert($token, $signatureValidator, $domainValidator);
$validator->assert($token, $signatureValidator);

return $this->transformClaims($token);
} catch (\Exception $e) {
Expand All @@ -51,7 +49,7 @@ public function decodePurchases(string $encodedPurchases, ShopInterface $shop, b

$this->logger->error('Failed to decode in-app purchases: ' . $e->getMessage());

throw $e;
return new Collection();
}
}

Expand Down
9 changes: 6 additions & 3 deletions tests/Context/InAppPurchase/HasValidRSAJWKSignatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ public function testAssertWithWrongTokenType(): void
$token = new class () implements Token {
public function headers(): DataSet
{
return new DataSet([], '');
return new DataSet([
'alg' => 'RS256',
'kid' => JWKSHelper::getStaticKid(),
], '');
}

public function isPermittedFor(string $audience): bool
Expand Down Expand Up @@ -157,9 +160,9 @@ public function toString(): string
};

static::expectException(\Exception::class);
static::expectExceptionMessage('Token must be a plain JWT');
static::expectExceptionMessage('You should pass a plain token');

$constraint = new HasValidRSAJWKSignature(new KeySet());
$constraint = new HasValidRSAJWKSignature($this->jwks);
$constraint->assert($token);
}
}
7 changes: 3 additions & 4 deletions tests/Context/InAppPurchase/InAppPurchaseProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Shopware\App\SDK\Tests\Context\InAppPurchase;

use Lcobucci\JWT\Validation\RequiredConstraintsViolated;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -122,9 +121,9 @@ public function testDecodePurchaseRetryFails(): void
(new KeySetFactory())->createFromJSON('{"keys": [{"kty": "RSA","n": "yHenasOsOl-Vv2BmpayS1R8l5L-JN99FwaRRKXFssGTjJDwbYdbe3CqTSKqtOfdqZLzE6-bN2-Q1xqZZsgs0_zHNx7EROXNG_uQs1uuGkS6bgGhnq_2d7wzFvCsyI00CDXZxRlGjKAEhvcXormomF1jpUW08Y5tPeUvMSdEZbZxW1ydir-UrMm1RUSgJgSP-sUqLG7kTIJ6SG7cLtF8c8cHcVXFljMyiYLQHYOECj1oklwvfrfaoT3OKdKGumi39rDthXtFa0Aq1OS_P9qfZJ-yXiQlpf2RxRr3Q5EQJ8E9iqrlOndbkSq7eXne2DvvgsiNdyzRWFvxWSPSd9GZXkw","e": "AQAB","kid": "-1xljHNcPM59Qx9OcULA9LS219bsmKCZueVXhdF0N0k","use": "sig","alg": "RS256"}]}'),
);

static::expectException(RequiredConstraintsViolated::class);

$provider = new InAppPurchaseProvider($fetcher, $logger);
$provider->decodePurchases($token->toString(), $shop, true);
$decoded = $provider->decodePurchases($token->toString(), $shop, true);

static::assertEmpty($decoded);
}
}
Loading