diff --git a/README.md b/README.md index 87f5d4f..b39f0a6 100644 --- a/README.md +++ b/README.md @@ -37,15 +37,24 @@ use LaravelReady\LicenseConnector\Services\ConnectorService; ... $licenseKey = '46fad906-bc51-435f-9929-db46cb4baf13'; -$licenseStatus = ConnectorService::validateLicense($licenseKey); +$connectorService = new ConnectorService($licenseKey); -if ($liceseStatus) { +$isLicenseValid = $connectorService->validateLicense(); + +if ($isLicenseValid) { // License is valid } else { // License is invalid } ``` +To validating with custom data + +```php +$customData = ['email' => 'testa@example.com']; +$isLicenseValid = $connectorService->validateLicense($customData); +``` + ⚠️ Don't forget this package just provides management of licenses and server communication. ⚠️ Please don't confuse it with ioncube or similar source code encryption tools. diff --git a/composer.json b/composer.json index a389151..9a36daa 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "Connector package for License Server", "type": "library", "license": "MIT", - "version": "1.0.1", + "version": "1.1.1", "keywords": [ "laravel", "license", diff --git a/src/Exceptions/AuthException.php b/src/Exceptions/AuthException.php new file mode 100644 index 0000000..d9062cf --- /dev/null +++ b/src/Exceptions/AuthException.php @@ -0,0 +1,9 @@ +licenseKey = $licenseKey; + + $this->accessToken = $this->getAccessToken($licenseKey); } /** - * Get access token for the given domain + * Check license status * * @param string $licenseKey + * @param array $data * - * @return string + * @return boolean */ - public static function getAccessToken(string $licenseKey): null | string + public function validateLicense(array $data = []): bool { - $accessTokenCacheKey = self::getAccessTokenKey($licenseKey); - - $accessToken = Cache::get($accessTokenCacheKey, null); - - if ($accessToken) { - return $accessToken; - } - - $url = Config::get('license-connector.license_server_url') . '/api/license-server/auth/login'; - - $response = Http::withHeaders([ - 'x-host' => Config::get('app.url'), - 'x-host-name' => Config::get('app.name'), - ])->post($url, [ - 'license_key' => $licenseKey - ]); + if ($this->accessToken) { + $url = Config::get('license-connector.license_server_url') . '/api/license-server/license'; - if ($response->ok()) { - $data = $response->json(); + $response = Http::withHeaders([ + 'x-host' => Config::get('app.url'), + 'x-host-name' => Config::get('app.name'), + 'Authorization' => "Bearer {$this->accessToken}", + 'Content-Type' => 'application/json', + 'Accept' => 'application/json', + ])->post($url, $data); - if (isset($data['status'])) { - if ($data['status'] === true) { - if (isset($data['access_token'])) { - $accessToken = $data['access_token']; + if ($response->ok()) { + $license = $response->json(); - Cache::put($accessTokenCacheKey, $accessToken, now()->addMinutes(60)); + $this->license = $license; - return $accessToken; - } - } else if ($data['status'] === true) { - if (isset($data['message'])) { - return $data['message']; - } - } + return $license && $license['status'] == 'active'; } } - return null; + return false; } /** - * Check license status + * Get access token for the given domain * * @param string $licenseKey * - * @return boolean + * @return string */ - public static function validateLicense(string $licenseKey) + private function getAccessToken(string $licenseKey): null | string { - $accessToken = self::getAccessToken($licenseKey); + $accessTokenCacheKey = $this->getAccessTokenKey($licenseKey); - $url = Config::get('license-connector.license_server_url') . '/api/license-server/license'; + $accessToken = Cache::get($accessTokenCacheKey, null); + + if ($accessToken) { + return $accessToken; + } + + $url = Config::get('license-connector.license_server_url') . '/api/license-server/auth/login'; $response = Http::withHeaders([ 'x-host' => Config::get('app.url'), 'x-host-name' => Config::get('app.name'), - 'Authorization' => 'Bearer ' . $accessToken, - ])->get($url); + 'Content-Type' => 'application/json', + 'Accept' => 'application/json', + ])->post($url, [ + 'license_key' => $licenseKey + ]); + + $data = $response->json(); if ($response->ok()) { - $data = $response->json(); + if ($data['status'] === true) { + if (!empty($data['access_token'])) { + $accessToken = $data['access_token']; + + Cache::put($accessTokenCacheKey, $accessToken, now()->addMinutes(60)); - return $data['status'] == 'active'; + return $accessToken; + } else { + throw new AuthException($data['message']); + } + } } - return false; + throw new AuthException($data['message']); } } diff --git a/src/Traits/CacheKeys.php b/src/Traits/CacheKeys.php index d750883..81e4edf 100644 --- a/src/Traits/CacheKeys.php +++ b/src/Traits/CacheKeys.php @@ -9,7 +9,7 @@ trait CacheKeys * * @return string */ - private static function getAccessTokenKey(string $licenseKey): string + private function getAccessTokenKey(string $licenseKey): string { return "license-connector:access-token-{$licenseKey}"; }