From 32a6b45582418c616248502f3f57f694a5991edd Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Fri, 22 Sep 2023 17:31:03 +0200 Subject: [PATCH] Move the handling of the license data into a separate API --- .../files/lib/acp/page/LicensePage.class.php | 73 +++-------------- .../package/license/LicenseApi.class.php | 81 +++++++++++++++++++ 2 files changed, 91 insertions(+), 63 deletions(-) create mode 100644 wcfsetup/install/files/lib/system/package/license/LicenseApi.class.php diff --git a/wcfsetup/install/files/lib/acp/page/LicensePage.class.php b/wcfsetup/install/files/lib/acp/page/LicensePage.class.php index ef23f57517..f7ffccdfa0 100644 --- a/wcfsetup/install/files/lib/acp/page/LicensePage.class.php +++ b/wcfsetup/install/files/lib/acp/page/LicensePage.class.php @@ -2,9 +2,6 @@ namespace wcf\acp\page; -use CuyZ\Valinor\Mapper\Source\Source; -use CuyZ\Valinor\MapperBuilder; -use GuzzleHttp\Psr7\Request; use Laminas\Diactoros\Response\RedirectResponse; use wcf\acp\form\LicenseEditForm; use wcf\data\package\Package; @@ -13,7 +10,7 @@ use wcf\data\package\update\PackageUpdateAction; use wcf\data\package\update\server\PackageUpdateServer; use wcf\page\AbstractPage; use wcf\system\database\util\PreparedStatementConditionBuilder; -use wcf\system\io\HttpFactory; +use wcf\system\package\license\LicenseApi; use wcf\system\request\LinkHandler; use wcf\system\WCF; @@ -31,9 +28,11 @@ final class LicensePage extends AbstractPage public $neededPermissions = ['admin.configuration.package.canInstallPackage']; + private LicenseApi $licenseApi; + private array $licenseData; - private string $licenseNumber; + private int $licenseNumber; private array $installedPackages; @@ -41,8 +40,6 @@ final class LicensePage extends AbstractPage private array $packageUpdates = []; - private PackageUpdateServer $updateServer; - private array $requiresLicenseExtension = []; private const CURRENT_MAJOR = '6.0'; @@ -51,9 +48,9 @@ final class LicensePage extends AbstractPage { parent::readData(); - $this->updateServer = PackageUpdateServer::getWoltLabUpdateServer(); + $this->licenseApi = new LicenseApi(); - if (!$this->hasLicenseCredentials()) { + if (!$this->licenseApi->hasLicenseCredentials()) { return new RedirectResponse( LinkHandler::getInstance()->getControllerLink( LicenseEditForm::class, @@ -66,7 +63,10 @@ final class LicensePage extends AbstractPage (new PackageUpdateAction([], 'refreshDatabase'))->executeAction(); - $this->licenseData = $this->fetchLicenseData(); + $this->licenseData = $this->licenseApi->fetchLicenseData(); + if (isset($this->licenseData['license']['licenseID'])) { + $this->licenseNumber = $this->licenseData['license']['licenseID']; + } $identifiers = \array_merge( \array_keys($this->licenseData['woltlab']), @@ -119,16 +119,6 @@ final class LicensePage extends AbstractPage } } - private function hasLicenseCredentials(): bool - { - $authData = $this->updateServer->getAuthData(); - if (empty($authData['username']) || empty($authData['password'])) { - return false; - } - - return true; - } - public function assignVariables() { parent::assignVariables(); @@ -625,47 +615,4 @@ final class LicensePage extends AbstractPage return $packageUpdates; } - - // This code was stolen from "FirstTimeSetupLicenseForm" and - // should propably be moved into a helper class. We might even want to refresh - // the data with requests to the package servers to implicitly fetch the - // latest purchases. - private function fetchLicenseData(): array|object - { - $authData = $this->updateServer->getAuthData(); - $this->licenseNumber = $authData['username']; - - $request = new Request( - 'POST', - 'https://api.woltlab.com/2.1/customer/license/list.json', - [ - 'content-type' => 'application/x-www-form-urlencoded', - ], - \http_build_query([ - 'licenseNo' => $this->licenseNumber, - 'serialNo' => $authData['password'], - 'instanceId' => \hash_hmac('sha256', 'api.woltlab.com', \WCF_UUID), - ], '', '&', \PHP_QUERY_RFC1738) - ); - - $response = HttpFactory::makeClientWithTimeout(5)->send($request); - return (new MapperBuilder()) - ->allowSuperfluousKeys() - ->mapper() - ->map( - <<<'EOT' - array { - status: 200, - license: array { - authCode?: string, - type: string, - expiryDates?: array, - }, - pluginstore: array, - woltlab: array, - } - EOT, - Source::json($response->getBody()) - ); - } } diff --git a/wcfsetup/install/files/lib/system/package/license/LicenseApi.class.php b/wcfsetup/install/files/lib/system/package/license/LicenseApi.class.php new file mode 100644 index 0000000000..eafd783589 --- /dev/null +++ b/wcfsetup/install/files/lib/system/package/license/LicenseApi.class.php @@ -0,0 +1,81 @@ + + * @since 6.0 + */ +final class LicenseApi +{ + private readonly PackageUpdateServer $packageUpdateServer; + + public function __construct() + { + $this->packageUpdateServer = PackageUpdateServer::getWoltLabUpdateServer(); + } + + public function fetchLicenseData(): array|object + { + if (!$this->hasLicenseCredentials()) { + // TODO + throw new \Exception("no credentials"); + } + + $authData = $this->packageUpdateServer->getAuthData(); + + $request = new Request( + 'POST', + 'https://api.woltlab.com/2.1/customer/license/list.json', + [ + 'content-type' => 'application/x-www-form-urlencoded', + ], + \http_build_query([ + 'licenseNo' => $authData['username'], + 'serialNo' => $authData['password'], + 'instanceId' => \hash_hmac('sha256', 'api.woltlab.com', \WCF_UUID), + ], '', '&', \PHP_QUERY_RFC1738) + ); + + $response = HttpFactory::makeClientWithTimeout(5)->send($request); + return (new MapperBuilder()) + ->allowSuperfluousKeys() + ->mapper() + ->map( + <<<'EOT' + array { + status: 200, + license: array { + authCode?: string, + licenseID?: int, + type: string, + expiryDates?: array, + }, + pluginstore: array, + woltlab: array, + } + EOT, + Source::json($response->getBody()) + ); + } + + public function hasLicenseCredentials(): bool + { + $authData = $this->packageUpdateServer->getAuthData(); + if (empty($authData['username']) || empty($authData['password'])) { + return false; + } + + return true; + } +} -- 2.20.1