From a5bec565435db4c439a0dad24d018a611cffc403 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Tue, 26 Sep 2023 17:05:55 +0200 Subject: [PATCH] Make use of the license API in the first time setup --- .../form/FirstTimeSetupLicenseForm.class.php | 57 ++++--------------- 1 file changed, 10 insertions(+), 47 deletions(-) diff --git a/wcfsetup/install/files/lib/acp/form/FirstTimeSetupLicenseForm.class.php b/wcfsetup/install/files/lib/acp/form/FirstTimeSetupLicenseForm.class.php index f5427d06f7..374e30ab7f 100644 --- a/wcfsetup/install/files/lib/acp/form/FirstTimeSetupLicenseForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/FirstTimeSetupLicenseForm.class.php @@ -2,11 +2,7 @@ namespace wcf\acp\form; -use CuyZ\Valinor\Mapper\MappingError; -use CuyZ\Valinor\Mapper\Source\Source; -use CuyZ\Valinor\MapperBuilder; use GuzzleHttp\Exception\ConnectException; -use GuzzleHttp\Psr7\Request; use Psr\Http\Client\ClientExceptionInterface; use wcf\data\option\Option; use wcf\data\option\OptionAction; @@ -21,7 +17,8 @@ use wcf\system\form\builder\field\dependency\EmptyFormFieldDependency; use wcf\system\form\builder\field\TextFormField; use wcf\system\form\builder\field\validation\FormFieldValidationError; use wcf\system\form\builder\field\validation\FormFieldValidator; -use wcf\system\io\HttpFactory; +use wcf\system\package\license\exception\ParsingFailed; +use wcf\system\package\license\LicenseApi; use wcf\system\request\LinkHandler; use wcf\util\HeaderUtil; @@ -40,7 +37,7 @@ final class FirstTimeSetupLicenseForm extends AbstractFormBuilderForm */ public $neededPermissions = ['admin.configuration.package.canEditServer']; - private array $apiResponse; + private LicenseApi $licenseApi; /** * @inheritDoc @@ -84,13 +81,16 @@ final class FirstTimeSetupLicenseForm extends AbstractFormBuilderForm \assert($licenseNo instanceof TextFormField); try { - $this->apiResponse = $this->getLicenseData($licenseNo->getValue(), $serialNo->getValue()); + $this->licenseApi = LicenseApi::fetchFromRemote([ + 'username' => $licenseNo->getValue(), + 'password' => $serialNo->getValue(), + ]); } catch (ConnectException) { $serialNo->addValidationError(new FormFieldValidationError( 'failedConnect', 'wcf.acp.firstTimeSetup.license.credentials.error.failedConnect' )); - } catch (ClientExceptionInterface | MappingError) { + } catch (ClientExceptionInterface | ParsingFailed) { $serialNo->addValidationError(new FormFieldValidationError( 'failedValidation', 'wcf.acp.firstTimeSetup.license.credentials.error.failedValidation' @@ -113,43 +113,6 @@ final class FirstTimeSetupLicenseForm extends AbstractFormBuilderForm ); } - private function getLicenseData(string $licenseNo, string $serialNo): array - { - $request = new Request( - 'POST', - 'https://api.woltlab.com/2.0/customer/license/list.json', - [ - 'content-type' => 'application/x-www-form-urlencoded', - ], - \http_build_query([ - 'licenseNo' => $licenseNo, - 'serialNo' => $serialNo, - '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()) - ); - } - /** * @inheritDoc */ @@ -189,8 +152,8 @@ final class FirstTimeSetupLicenseForm extends AbstractFormBuilderForm Option::getOptionByName('first_time_setup_state')->optionID => 1, ]; - if (isset($this->apiResponse) && isset($this->apiResponse['license']['authCode'])) { - $optionData[Option::getOptionByName('package_server_auth_code')->optionID] = $this->apiResponse['license']['authCode']; + if (isset($this->licenseApi) && isset($this->licenseApi->getData()['license']['authCode'])) { + $optionData[Option::getOptionByName('package_server_auth_code')->optionID] = $this->licenseApi->getData()['license']['authCode']; } $objectAction = new OptionAction( -- 2.20.1