Make use of the `LicenseApi` to validate the license
authorAlexander Ebert <ebert@woltlab.com>
Tue, 26 Sep 2023 14:54:58 +0000 (16:54 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Tue, 26 Sep 2023 14:54:58 +0000 (16:54 +0200)
wcfsetup/install/files/lib/acp/form/LicenseEditForm.class.php
wcfsetup/install/files/lib/system/package/license/LicenseApi.class.php

index ae465e4a96d66580c23c4f2520d495666f766485..4a1d1a831b5cea737ecc201fc6e701d28b005076 100644 (file)
@@ -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 Laminas\Diactoros\Response\RedirectResponse;
 use Psr\Http\Client\ClientExceptionInterface;
 use wcf\data\option\Option;
@@ -23,9 +19,9 @@ 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\system\WCF;
 
 /**
  * Set up or edit the license data.
@@ -52,7 +48,7 @@ final class LicenseEditForm extends AbstractFormBuilderForm
      */
     public $templateName = 'licenseEdit';
 
-    private array $apiResponse;
+    private LicenseApi $licenseApi;
 
     private string $url;
 
@@ -109,13 +105,16 @@ final class LicenseEditForm 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'
@@ -161,43 +160,6 @@ final class LicenseEditForm 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<string, int>,
-                        },
-                        pluginstore: array<string, string>,
-                        woltlab: array<string, string>,
-                    }
-                    EOT,
-                Source::json($response->getBody())
-            );
-    }
-
     /**
      * @inheritDoc
      */
@@ -238,21 +200,20 @@ final class LicenseEditForm extends AbstractFormBuilderForm
             $objectAction->executeAction();
         }
 
-        if (isset($this->apiResponse) && isset($this->apiResponse['license']['authCode'])) {
-            $optionData = [
-                Option::getOptionByName('package_server_auth_code')->optionID => $this->apiResponse['license']['authCode'],
-            ];
+        $authCode = '';
+        if (isset($this->licenseApi)) {
+            $authCode = $this->licenseApi->getData()['license']['authCode'] ?? '';
         } else {
-            $optionData = [
-                Option::getOptionByName('package_server_auth_code')->optionID => '',
-            ];
+            LicenseApi::removeLicenseFile();
         }
 
         $objectAction = new OptionAction(
             [],
             'updateAll',
             [
-                'data' => $optionData,
+                'data' => [
+                    Option::getOptionByName('package_server_auth_code')->optionID => $authCode
+                ],
             ]
         );
         $objectAction->executeAction();
index 0b592ce7a8e20b09fdbcf9a9a9469bf788eb0237..80f059e592f51b1d7ad7a80923ea247d899365c9 100644 (file)
@@ -30,8 +30,6 @@ final class LicenseApi
     {
         $this->json = $json;
         $this->data = $this->parseLicenseData($this->json);
-
-        $this->updateLicenseFile();
     }
 
     public function getData(): array
@@ -39,7 +37,7 @@ final class LicenseApi
         return $this->data;
     }
 
-    private function updateLicenseFile(): void
+    public function updateLicenseFile(): void
     {
         @\file_put_contents(
             self::LICENSE_FILE,
@@ -83,13 +81,15 @@ final class LicenseApi
         return $result;
     }
 
-    public static function fetchFromRemote(): LicenseApi
+    public static function fetchFromRemote(array $authData = []): LicenseApi
     {
-        if (!self::hasLicenseCredentials()) {
-            throw new MissingCredentials();
-        }
+        if ($authData === []) {
+            if (!self::hasLicenseCredentials()) {
+                throw new MissingCredentials();
+            }
 
-        $authData = PackageUpdateServer::getWoltLabUpdateServer()->getAuthData();
+            $authData = PackageUpdateServer::getWoltLabUpdateServer()->getAuthData();
+        }
 
         $request = new Request(
             'POST',
@@ -106,7 +106,10 @@ final class LicenseApi
 
         $response = HttpFactory::makeClientWithTimeout(5)->send($request);
 
-        return new LicenseApi($response->getBody());
+        $licenseApi = new LicenseApi($response->getBody());
+        $licenseApi->updateLicenseFile();
+
+        return $licenseApi;
     }
 
     public static function readFromFile(): ?LicenseApi
@@ -124,6 +127,15 @@ final class LicenseApi
         }
     }
 
+    public static function removeLicenseFile(): void
+    {
+        if (!\file_exists(self::LICENSE_FILE)) {
+            return;
+        }
+
+        \unlink(self::LICENSE_FILE);
+    }
+
     public static function hasLicenseCredentials(): bool
     {
         $authData = PackageUpdateServer::getWoltLabUpdateServer()->getAuthData();