Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEXT-34133 - Remove validate shop url license #23

Merged
merged 1 commit into from
Nov 6, 2024
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
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('');
}
}
Loading