Skip to content

Commit

Permalink
Merge pull request #23 from shopware/next-34133/remove-validate-shop-…
Browse files Browse the repository at this point in the history
…url-lincense

NEXT-34133 - Remove validate shop url license
  • Loading branch information
nam4am authored Nov 6, 2024
2 parents e3059b5 + 8c26b3e commit b7a4dee
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/Controller/LicenseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function sync(ShopInterface $shop, Request $request): Response
}

try {
$this->commercialLicense->validate($shop->getShopUrl(), $licenseKey);
$this->commercialLicense->validate($licenseKey);
} catch (LicenseException $e) {
$data = [
'errors' => [
Expand Down
10 changes: 0 additions & 10 deletions src/Exception/LicenseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,4 @@ public static function licenseNotProvided(): self
{
return new self('License key not provided', self::ERROR_LICENSE_INVALID);
}

public static function domainInvalid(string $domain): self
{
return new self(sprintf('License domain not valid: %s', $domain), self::ERROR_LICENSE_INVALID);
}

public static function domainNotProvided(): self
{
return new self('License domain not provided', self::ERROR_LICENSE_INVALID);
}
}
19 changes: 1 addition & 18 deletions src/Service/CommercialLicense.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,19 @@
use Lcobucci\JWT\Signer\Key\InMemory;
use Lcobucci\JWT\Signer\Rsa\Sha512;
use Lcobucci\JWT\UnencryptedToken;
use Lcobucci\JWT\Validation\Constraint\PermittedFor;
use Lcobucci\JWT\Validation\Constraint\SignedWith;
use Lcobucci\JWT\Validation\Constraint\StrictValidAt;
use Lcobucci\JWT\Validation\RequiredConstraintsViolated;
use Shopware\ServiceBundle\Exception\LicenseException;

class CommercialLicense
{
public function validate(string $shopUrl, string $licenseKey): LicenseInfo
public function validate(string $licenseKey): LicenseInfo
{
if (!$licenseKey) {
throw LicenseException::licenseNotProvided();
}

$shopUrl = parse_url($shopUrl);
$domain = $shopUrl['host'] ?? null;

if (!$domain) {
throw LicenseException::domainNotProvided();
}

try {
$jwt = Configuration::forAsymmetricSigner(
new Sha512(),
Expand All @@ -50,15 +42,6 @@ public function validate(string $shopUrl, string $licenseKey): LicenseInfo
throw LicenseException::licenseExpired($licenseKey);
}

try {
$jwt->validator()->assert(
$token,
new PermittedFor($domain),
);
} catch (RequiredConstraintsViolated) {
throw LicenseException::domainInvalid($domain);
}

$jwt->validator()->assert(
$token,
new SignedWith($jwt->signer(), $jwt->verificationKey()),
Expand Down
4 changes: 2 additions & 2 deletions tests/Controller/LicenseControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function testSyncWithValidLicenseKey(): void
$request = $this->createMock(Request::class);
$request->method('getPayload')->willReturn(new InputBag(['licenseKey' => 'valid_key']));

$this->commercialLicense->expects($this->once())->method('validate')->with($shop->getShopUrl(), 'valid_key');
$this->commercialLicense->expects($this->once())->method('validate')->with('valid_key');

$licenseController = new LicenseController($shopRepository, $this->commercialLicense);

Expand Down Expand Up @@ -119,7 +119,7 @@ public function testSyncWithShopUpdateFailure(): void
$request = $this->createMock(Request::class);
$request->method('getPayload')->willReturn(new InputBag(['licenseKey' => 'valid_key']));

$this->commercialLicense->expects($this->once())->method('validate')->with($shop->getShopUrl(), 'valid_key');
$this->commercialLicense->expects($this->once())->method('validate')->with('valid_key');

$licenseController = new LicenseController($shopRepository, $this->commercialLicense);

Expand Down
26 changes: 3 additions & 23 deletions tests/Service/CommercialLicenseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CommercialLicenseTest extends TestCase
public function testValidateWithValidLicenseKey(): void
{
$commercialLicense = new CommercialLicense();
$info = $commercialLicense->validate('http://localhost', self::LICENSE_KEY);
$info = $commercialLicense->validate(self::LICENSE_KEY);

$this->assertInstanceOf(LicenseInfo::class, $info);

Expand All @@ -40,27 +40,7 @@ public function testValidateWithInvalidLicenseKey(): void

$commercialLicense = new CommercialLicense();

$commercialLicense->validate('https://shop.com', 'invalid_license_key');
}

public function testValidateWithInvalidDomain(): void
{
$commercialLicense = new CommercialLicense();

$this->expectException(LicenseException::class);
$this->expectExceptionMessage('License domain not valid: invalid.com');

$commercialLicense->validate('https://invalid.com', self::LICENSE_KEY);
}

public function testValidateWithEmptyDomain(): void
{
$commercialLicense = new CommercialLicense();

$this->expectException(LicenseException::class);
$this->expectExceptionMessage('License domain not provided');

$commercialLicense->validate('', self::LICENSE_KEY);
$commercialLicense->validate('invalid_license_key');
}

public function testValidateNotProviderLicenseKey(): void
Expand All @@ -70,6 +50,6 @@ public function testValidateNotProviderLicenseKey(): void
$this->expectException(LicenseException::class);
$this->expectExceptionMessage('License key not provided');

$commercialLicense->validate('https://shop.com', '');
$commercialLicense->validate('');
}
}

0 comments on commit b7a4dee

Please sign in to comment.