Skip to content

Commit

Permalink
Add custom data option
Browse files Browse the repository at this point in the history
  • Loading branch information
relliv committed Jun 20, 2022
1 parent 5af50dd commit 82fb3e6
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 50 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '[email protected]'];
$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.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 9 additions & 0 deletions src/Exceptions/AuthException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace LaravelReady\LicenseConnector\Exceptions;

use Exception;

final class AuthException extends Exception
{
}
104 changes: 58 additions & 46 deletions src/Services/ConnectorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,94 +2,106 @@

namespace LaravelReady\LicenseConnector\Services;

use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Http;

use LaravelReady\LicenseConnector\Traits\CacheKeys;
use LaravelReady\LicenseConnector\Exceptions\AuthException;

class ConnectorService
{
use CacheKeys;

public function __construct()
public $license;

private $licenseKey;
private $accessToken;

public function __construct(string $licenseKey)
{
$this->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']);
}
}
2 changes: 1 addition & 1 deletion src/Traits/CacheKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}";
}
Expand Down

0 comments on commit 82fb3e6

Please sign in to comment.